python - 在 Python 中迭代 4 个函数的不同排列

标签 python nltk

好的,我正在使用不同的标记器来标记文本。默认、一元语法、二元语法和三元语法。

我必须检查这四个标记器中的三个的组合是最准确的。

为此,我必须循环遍历所有可能的组合,如下所示:

permutaties = list(itertools.permutations(['default_tagger','unigram_tagger',
                                              'bigram_tagger','trigram_tagger'],3))
resultaten = [] 
for element in permutaties:
        resultaten.append(accuracy(element))

因此每个元素都是三个标记方法的元组,例如:('default_tagger', 'bigram_tagger', 'trigram_tagger')

在准确性函数中,我现在必须动态调用每个标记器的三个附带方法,问题是:我不知道如何执行此操作。

标注器功能如下:

unigram_tagger = nltk.UnigramTagger(brown_train, backoff=backofff)

bigram_tagger = nltk.BigramTagger(brown_train, backoff=backofff)

trigram_tagger = nltk.TrigramTagger(brown_train, backoff=backofff)

default_tagger = nltk.DefaultTagger('NN')

因此,对于示例,代码应变为:

t0 = nltk.DefaultTagger('NN')
t1 = nltk.BigramTagger(brown_train, backoff=t0)
t2 = nltk.TrigramTagger(brown_train, backoff=t1)
t2.evaluate(brown_test)

所以本质上问题是如何迭代 4 个函数列表的所有 24 种组合。

有Python大师可以帮助我吗?

最佳答案

不确定我是否理解你的需要,但是你可以使用你想要调用自己的方法而不是字符串 - 所以你的代码可能会变成这样:

permutaties = itertools.permutations([nltk.UnigramTagger, nltk.BigramTagger, nltk.TrigramTagger, nltk.DefaultTagger],3)
resultaten = [] 
for element in permutaties:
     resultaten.append(accuracy(element, brown_Train, brown_element))

def accuracy(element, brown_train,brown_element):
     if element is nltk.DeafultTagger:
        evaluator = element("NN")
     else:
        evaluator = element(brown_train, backoff=XXX)  #maybe insert more elif
                    #clauses to retrieve the proper backoff parameter --or you could
                    # usr a tuple in the call to permutations so the apropriate backoff 
                    #is avaliable for each function to be called
     return  evaluator.evaluate(brown_test) # ? I am not shure  from your code if this is your intent

关于python - 在 Python 中迭代 4 个函数的不同排列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5194686/

相关文章:

python - 导入错误 : Could not find 'cudart64_100. dll

python - 推文分类器特征选择 NLTK

python - 处理 PDF 以进行信息提取

python - 如何在 NLTK 中进行依赖解析?

python - 值错误 : A ELE probability distribution must have at least one bin

python - 在格式字符串中使用参数

python - 什么相当于Django的auto_now,SQLAlchemy中的auto_now_add?

python - RFID应答卡没有唯一的atr

python - 在 Python 中从 HTML 中提取链接

python - 用 "SAD"或 "HAPPY"替换表情符号的代码无法正常工作