目录

Python - Bigrams

一些英语单词更频繁地出现在一起。 例如 - 天空高,做或死,最佳表现,大雨等。因此,在文本文档中,我们可能需要识别这样的一对词,这将有助于情绪分析。 首先,我们需要从现有句子生成这样的单词对来维持它们的当前序列。 这种对称为双字母。 Python有一个bigram函数作为NLTK库的一部分,它可以帮助我们生成这些对。

例子 (Example)

import nltk
word_data = "The best performance can bring in sky high success."
nltk_tokens = nltk.word_tokenize(word_data)  	
print(list(nltk.bigrams(nltk_tokens)))

当我们运行上面的程序时,我们得到以下输出 -

[('The', 'best'), ('best', 'performance'), ('performance', 'can'), ('can', 'bring'), 
('bring', 'in'), ('in', 'sky'), ('sky', 'high'), ('high', 'success'), ('success', '.')]

该结果可用于给定文本中此类对的频率的统计结果。 这将与文本正文中描述的一般情绪相关联。

↑回到顶部↑
WIKI教程 @2018