Python - 双字母

有些英语单词一起出现的频率更高。 例如 - Sky High、do or die、best performance、heavy rain 等。因此,在文本文档中,我们可能需要识别这样的词对,这将有助于情感分析。 首先,我们需要从现有句子中生成这样的词对,保持它们当前的序列。 这样的对称为双字母组。 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', '.')]

此结果可用于对给定文本中此类对的频率进行统计调查。