问题描述
如何在文本中找到搭配?搭配是一个单词序列,它们不寻常地经常一起出现.python 具有返回单词对的内置 func bigrams.
How do you find collocations in text? A collocation is a sequence of words that occurs together unusually often. python has built-in func bigrams that returns word pairs.
>>> bigrams(['more', 'is', 'said', 'than', 'done'])
[('more', 'is'), ('is', 'said'), ('said', 'than'), ('than', 'done')]
>>>
剩下的就是根据单个单词的出现频率找出出现频率更高的二元组.任何想法如何将其放入代码中?
What's left is to find bigrams that occur more often based on the frequency of individual words. Any ideas how to put it in the code?
推荐答案
试试 NLTK.您将主要对 nltk.collocations.BigramCollocationFinder
感兴趣,但这里有一个快速演示,向您展示如何开始:
Try NLTK. You will mostly be interested in nltk.collocations.BigramCollocationFinder
, but here is a quick demonstration to show you how to get started:
>>> import nltk
>>> def tokenize(sentences):
... for sent in nltk.sent_tokenize(sentences.lower()):
... for word in nltk.word_tokenize(sent):
... yield word
...
>>> nltk.Text(tkn for tkn in tokenize('mary had a little lamb.'))
<Text: mary had a little lamb ....>
>>> text = nltk.Text(tkn for tkn in tokenize('mary had a little lamb.'))
这个小部分没有,但这里有:
There are none in this small segment, but here goes:
>>> text.collocations(num=20)
Building collocations list
这篇关于如何在文本中查找搭配,python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!