花了点时间研究 nltk,也试着去写点代码,我有这样一段文字,
>>> text = "i would't have the Scotland Yarders know it for the world"
这段话里有一个短语, for the world, 有无论如何的意思,参考: https://baike.baidu.com/item/for%20the%20world/8417068
>>> import nltk
>>> from nltk.collocations import *
>>> bigram_measures = nltk.collocations.BigramAssocMeasures()
>>> trigram_measures = nltk.collocations.TrigramAssocMeasures()
>>> text = "i would't have the Scotland Yarders know it for the world"
>>> tokens = nltk.wordpunct_tokenize(text)
>>> finder = BigramCollocationFinder.from_words(tokens)
>>> scored = finder.score_ngrams(bigram_measures.raw_freq)
>>> sorted(bigram for bigram, score in scored)
[("'", 't'), ('I', 'would'), ('Scotland', 'Yarders'), ('Yarders', 'know'), ('for', 'the'), ('have', 'the'), ('it', 'for'), ('know', 'it'), ('t', 'have'), ('the', 'Scotland'), ('the', 'world'), ('would', "'")]
这代码是 google 来的,不是我写的。最后运行的效果,似乎无法辨别出 for the world 这个短语。
我想问,目前的 nltk 能准确识别出来这个短语吗?
1
bugcoder 2018-07-01 08:09:29 +08:00 1
bigram 是两个单词的短语,trigram 才是三个词的。你研究花的时间是 5 分钟?
|