如何在文本中查找搭配,python

How to find collocations in text, python(如何在文本中查找搭配,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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)