我有以下示例数据框:
No category problem_definition_stopwords
175 2521 ['coffee', 'maker', 'brewing', 'properly', '2', '420', '420', '420']
211 1438 ['galley', 'work', 'table', 'stuck']
912 2698 ['cloth', 'stuck']
572 2521 ['stuck', 'coffee']
'problem_definition_stopwords' 字段已经被标记化,去除了停用词。
我想从“problem_definition_stopwords”字段创建 n-gram。具体来说,我想从我的数据中提取 n-gram 并找到具有最高点互信息 (PMI) 的那些。
本质上,我想找到同时出现的单词比我偶然期望的要多得多。
我尝试了以下代码:
import nltk
from nltk.collocations import *
bigram_measures = nltk.collocations.BigramAssocMeasures()
trigram_measures = nltk.collocations.TrigramAssocMeasures()
# errored out here
finder = BigramCollocationFinder.from_words(nltk.corpus.genesis.words(df['problem_definition_stopwords']))
# only bigrams that appear 3+ times
finder.apply_freq_filter(3)
# return the 10 n-grams with the highest PMI
finder.nbest(bigram_measures.pmi, 10)
我收到的错误是在第三块代码上... TypeError: join() argument must be str or bytes, not 'list'
编辑:DataFrame 的一种更便携的格式:
>>> df.columns
Index(['No', 'category', 'problem_definition_stopwords'], dtype='object')
>>> df.to_dict()
{'No': {0: 175, 1: 211, 2: 912, 3: 572}, 'category': {0: 2521, 1: 1438, 2: 2698, 3: 2521}, 'problem_definition_stopwords': {0: ['coffee', 'maker', 'brewing', 'properly', '2', '420', '420', '420'], 1: ['galley', 'work', 'table', 'stuck'], 2: ['cloth', 'stuck'], 3: ['stuck', 'coffee']}}