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
基础教程推荐
- 用于分类数据的跳跃记号标签 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 筛选NumPy数组 2022-01-01