[英]Iterate through different permutations of 4 functions in Python
OK I am using different taggers to tag a text. 好的,我正在使用不同的标记器来标记文本。 Default, unigram, bigram and trigram.
默认值,unigram,bigram和trigram。
I have to check which combination of three of those four taggers is the most accurate. 我必须检查这四个标记器中的三个的哪个组合是最准确的。
To do that i have to loop through all the possible combinations which i do like this: 为此,我必须遍历我所做的所有可能的组合:
permutaties = list(itertools.permutations(['default_tagger','unigram_tagger',
'bigram_tagger','trigram_tagger'],3))
resultaten = []
for element in permutaties:
resultaten.append(accuracy(element))
so each element is a tuple of three tagmethods like for example: ('default_tagger', 'bigram_tagger', 'trigram_tagger')
因此每个元素都是三个标记方法的元组,例如:(
('default_tagger', 'bigram_tagger', 'trigram_tagger')
In the accuracy function I now have to dynamically call the three accompanying methods of each tagger, the problem is: I don't know how to do this. 在准确性函数中,我现在必须动态调用每个标记器的三个随附方法,问题是:我不知道该怎么做。
The tagger functions are as follows: 标记器功能如下:
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')
So for the example the code should become: 因此,对于该示例,代码应变为:
t0 = nltk.DefaultTagger('NN')
t1 = nltk.BigramTagger(brown_train, backoff=t0)
t2 = nltk.TrigramTagger(brown_train, backoff=t1)
t2.evaluate(brown_test)
So in essence the problem is how to iterate through all 24 combinations of that list of 4 functions. 因此,本质上,问题是如何遍历4个功能列表的所有24个组合。
Any Python Masters that can help me? 有任何Python大师可以帮助我吗?
Not shure if I understood what you need, but you can use the methods you want to call themselves instead of strings - sou your code could become soemthing like: 如果我理解您的需求就不知道了,但是您可以使用自己调用的方法来代替字符串,因此您的代码可能会变得像这样:
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
Starting with jsbueno's code, I suggest writing a wrapper function for each of the taggers to give them the same signature. 从jsbueno的代码开始,我建议为每个标记器编写一个包装函数,以赋予它们相同的签名。 And since you only need them once, I suggest using a lambda.
而且由于您只需要一次,所以建议使用lambda。
permutaties = itertools.permutations([lambda: ntlk.DefaultTagger("NN"),
lambda: nltk.UnigramTagger(brown_train, backoff),
lambda: nltk.BigramTagger(brown_train, backoff),
lambda: nltk.TrigramTagger(brown_train, backoff)],3)
This would allow you to call each directly, without a special function that figures out which function you're calling and employs the appropriate signature. 这样,您就可以直接调用每个函数,而无需一个特殊的函数来弄清楚您要调用的函数并使用适当的签名。
basing on jsbueno code I think that you want to reuse evaluator as the backoff argument so the code should be 基于jsbueno代码,我认为您想将Evaluator用作后退参数,因此代码应为
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):
evaluator = "NN"
for e in element:
if evaluator == "NN":
evaluator = e("NN")
else:
evaluator = e(brown_train, backoff=evaluator) #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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.