本文介绍了NLTK 2.0分类器批次分类器方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行此代码时,它将引发错误,我认为是由于NLTK 3.0中不存在batch_classify方法所致.我很好奇如何解决这种问题,旧版本中的某些内容在新版本中消失了.

When I run this code it throws an error which I believe is due to the batch_classify method not being present in NLTK 3.0. I curious how to resolve this type of issue where something from an older version disappears in the newer version.

def accuracy(classifier, gold):
    results = classifier.batch_classify([fs for (fs,l) in gold])
    correct = [l==r for ((fs,l), r) in zip(gold, results)]
    if correct:
        return float(sum(correct))/len(correct)
    else:
        return 0

def apr (classifier, gold):
    results = classifier.batch_classify([fs for (fs,l) in gold])
    #gold_class_dict = defaultdict(list)
    #classifier_class_dict = {}
    sys_correct_dict = Counter()
    num_guessed = Counter()
    gold_num = Counter()
    num_right = 0
    total = 0

推荐答案

该方法已重命名为classify_many()(我找不到NLTK 2.0的文档进行检查,但我很确定那是发生了什么).

The method was renamed to classify_many() (I couldn't find documentation of NLTK 2.0 to check it, but I'm pretty sure that's what happened).

您必须在代码中用classify_many(...)替换所有出现的batch_classify(...).从库的一个主要版本迁移到另一个主要版本时,您必须期望这种向后不兼容的更改;理想情况下,应将它们记录在变更日志中.但是,我必须承认,过去NLTK甚至在次要版本之间都引入了向后不兼容的更改,我认为这是不好的做法.

You have to replace all occurrences of batch_classify(...) with classify_many(...) in your code.When moving from one major version of a library to another, you have to expect this kind of backwards-incompatible changes; they should ideally be documented in the changelog. However, I have to admit that in the past NLTK introduced backwards-incompatible changes even between minor versions, which I think is bad practice.

这篇关于NLTK 2.0分类器批次分类器方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 03:51