当前位置: 首页>>代码示例>>Python>>正文


Python Tagger.tagSentence方法代码示例

本文整理汇总了Python中tagger.Tagger.tagSentence方法的典型用法代码示例。如果您正苦于以下问题:Python Tagger.tagSentence方法的具体用法?Python Tagger.tagSentence怎么用?Python Tagger.tagSentence使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tagger.Tagger的用法示例。


在下文中一共展示了Tagger.tagSentence方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: evaluate

# 需要导入模块: from tagger import Tagger [as 别名]
# 或者: from tagger.Tagger import tagSentence [as 别名]
def evaluate(n, corpus):
    """Runs the n-fold validation on a corpus"""
    if n < 1:
        n = 10
        print("n was to low and has been set to 10\n")

    # Get all the data
    sentences, correctTags, tagData = corpusReader(corpus)
    allCor = []
    allIncor = []


    for check in range(1, n+1):
        # Divide all the data
        divSent = dividList(sentences, n, check)
        divTags = dividList(correctTags, n, check)
        divTrain = dividList(tagData, n, check)
        # To count the total of incorrect and correct tags
        correctlyTagged = []
        incorrectlyTagged = []

        print("Check {} doing {}-fold on {}\n".format(check, n, corpus))

        # For each part to evaluate
        for i in range(0, n):
            # Get the parts to train on
            trainingParts = divTrain[:i] + divTrain[i+1:]
            train = []

            # They need to be formatted so that we can use the Tagger
            for index in range(len(trainingParts)):
                train.extend(trainingParts[index])
        
                # Get the testing and evaluation data
                testingData = divSent[i]
                evaluationData = divTags[i]
        
                # Do some training
                uni, bi, tri, word = PB(train)
                tagger = Tagger(uni, bi, tri, word)
        
                # Reset counts
                correctTagCount = 0
                incorrectTagCount = 0

            # Go through each sentence and tag it
            for index in range(len(testingData)):
                tagged = tagger.tagSentence(testingData[index])
                for tag in range(len(tagged)):
                    # If correct
                    if evaluationData[index][tag] == tagged[tag]:
                        correctTagCount += 1
                    else:
                        incorrectTagCount += 1
            
            # Print to let you know I haven't forgotten about you.
            print("{}-fold was tagged {}% correctly.".format(i+1, round(correctTagCount / (correctTagCount + incorrectTagCount)*100,2 )))

            # Save n-fold counts
            correctlyTagged.append(correctTagCount)
            incorrectlyTagged.append(incorrectTagCount)

        allCor.extend(correctlyTagged)
        allIncor.extend(incorrectlyTagged)

        # Total in numbers..
        print("\n{} out of {} was correctly tagged.".format(sum(correctlyTagged), sum(correctlyTagged) + sum(incorrectlyTagged)))

        # .. and percentage
        print("\nFor a total of {}% correctness.".format(round(sum(correctlyTagged) / (sum(correctlyTagged) + sum(incorrectlyTagged))*100, 2)))

    # Total in numbers..
    print("\n{} out of {} was correctly tagged.".format(sum(allCor), sum(allCor) + sum(allIncor)))

    # .. and percentage
    print("\nFor a total of {}% correctness.".format(round(sum(allCor) / (sum(allCor) + sum(allIncor))*100, 2)))
开发者ID:rickardlofberg,项目名称:HMM-PoS-Tagger,代码行数:78,代码来源:n-fold.py


注:本文中的tagger.Tagger.tagSentence方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。