How can I get my bigrams to actually print? I get an error: TypeError: sequence item 0: expected str instance, list found

How can I get my bigrams to actually print? I get an error: TypeError: sequence item 0: expected str instance, list found

我正在尝试为 Python 3.5 中的文本打印双字母组。文本已经过预处理并拆分为单独的单词。

我尝试了两种不同的方法(如下所示),均无效。

第一个:

ninety_seven=df.loc[97]
nine_bi=ngrams(ninety_seven,2)
print(nine_bi)

这输出:

< generator object ngrams at 0x0B4F9E70>

第二个是:

ninety_seven=df.loc[97]
bigrm = list(nltk.bigrams(ninety_seven))
print(*map(' '.join, bigrm), sep=', ')

这输出:

TypeError: sequence item 0: expected str instance, list found

df.loc[97][car, chip, indication, posted, flight, post, flight]

我希望它打印为:

car chip, chip indication, indication posted, posted flight, flight post, post flight

试试这个:

>>> ninety_seven=df.loc[97].loc['FSR Narrative']
>>> nine_bi=ngrams(ninety_seven,2)
>>> print(nine_bi)
<generator object ngrams at 0x7f879020f308>
>>> print([" ".join(t) for t in nine_bi])
['car chip', 'chip indication', 'indication posted', 'posted flight', 'flight post', 'post flight']

这是一个简单的例子:

>>> from nltk import ngrams

>>> test = ['car', 'chip', 'indication', 'posted', 'flight', 'post', 'flight']
>>> nine_bi=ngrams(test,2)
>>> print(nine_bi)
<generator object ngrams at 0x7f879020f308>
>>> print([" ".join(t) for t in nine_bi])
['car chip', 'chip indication', 'indication posted', 'posted flight', 'flight post', 'post flight']