Python nltk 如何解析出英语短语?

2018-07-01 00:28:16 +08:00
 elone

花了点时间研究 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 能准确识别出来这个短语吗?

3167 次点击
所在节点    Python
1 条回复
bugcoder
2018-07-01 08:09:29 +08:00
bigram 是两个单词的短语,trigram 才是三个词的。你研究花的时间是 5 分钟?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/467146

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX