NLTK搭配文件对我来说似乎很好。 http://www.nltk.org/howto/collocations.html
您需要给评分员一些实际的可观语料库。这是使用NLTK中内置的Brown语料库的工作示例。运行大约需要30秒。
import nltk.collocations
import nltk.corpus
import collections
bgm = nltk.collocations.BigramAssocMeasures()
finder = nltk.collocations.BigramCollocationFinder.from_words(
nltk.corpus.brown.words())
scored = finder.score_ngrams( bgm.likelihood_ratio )
# Group bigrams by first word in bigram.
prefix_keys = collections.defaultdict(list)
for key, scores in scored:
prefix_keys[key[0]].append((key[1], scores))
# Sort keyed bigrams by strongest association.
for key in prefix_keys:
prefix_keys[key].sort(key = lambda x: -x[1])
print 'doctor', prefix_keys['doctor'][:5]
print 'baseball', prefix_keys['baseball'][:5]
print 'happy', prefix_keys['happy'][:5]
输出似乎是合理的,对棒球来说效果很好,对医生和快乐来说效果不那么理想。
doctor [('bills', 35.061321987405748), (',', 22.963930079491501),
('annoys', 19.009636692022365),
('had', 16.730384189212423), ('retorted', 15.190847940499127)]
baseball [('game', 32.110754519752291), ('cap', 27.81891372457088),
('park', 23.509042621473505), ('games', 23.105033513054011),
("player's", 16.227872863424668)]
happy [("''", 20.296341424483998), ('Spahn', 13.915820697905589),
('family', 13.734352182441569),
(',', 13.55077617193821), ('bodybuilder', 13.513265447290536)
0
背景:
我正在尝试比较成对的单词,以查看美式英语中哪一对比另一对更可能出现。我的计划是/将要使用NLTK中的搭配功能来对单词对进行评分,最有可能获得更高的评分对。
方法:
我使用NLTK在Python中编写了以下代码(为简洁起见,删除了几个步骤和导入):
结果:
然后,我使用2个单词对检查了结果,其中两个单词对很可能同时出现,而另一个单词对不应该出现(“烤腰果”和“汽油腰果”)。我惊讶地看到这些单词配对得分相同:
我本来希望在测试中“烤腰果”比“汽油腰果”得分更高。
问题:
非常感谢您提供任何信息或帮助!