How to find collocations in text, python(如何在文本中查找搭配,python)
问题描述
如何在文本中找到搭配?搭配是一个单词序列,它们不寻常地经常一起出现.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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在文本中查找搭配,python


- 计算测试数量的Python单元测试 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01