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

該結果可用於對給定文字中此類詞對的頻率進行統計調查。這將與文字正文中描述的一般情緒相關。

廣告
© . All rights reserved.