Python 二元组


一些英语单词更频繁地一起出现。例如 - Sky High、do or die、best performance、大雨等。因此,在文本文档中,我们可能需要识别这样的一对词,这将有助于情感分析。首先,我们需要从现有句子中生成这样的词对,并保持它们当前的序列。这样的对被称为二元组。 Python 有一个二元函数作为 NLTK 库的一部分,它可以帮助我们生成这些对。

例子


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', '.')]

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