2

ngramsから抽出する必要がありtextます。私は使用しています:

from textblob import TextBlob
text = TextBlob('me king of python')
print(text.ngrams(n=3)

テキスト(Pythonの王様)をトライグラムに分割すると、次のようになります。

[WordList(['me', 'king', 'of']), WordList(['king', 'of', 'python'])]

次に、各 WordList の項目を次のように結合する必要があります。

x = {word for word in ' '.join(text.ngrams(n=3)) }
print x

そして、それは私に次のエラーを与えます:

TypeError: sequence item 0: expected string or Unicode, WordList found

解決策がばかげていることは知っていますが、Pythonが苦手で理解できませんwordlists

4

1 に答える 1

2

これを試して:

>>> from textblob import TextBlob
>>> blob = TextBlob('me king of python')
>>> trigram = blob.ngrams(n=3)
>>> for wlist in trigram:
...     print ' '.join(wlist)
me king of
king of python

さらに良いことに、テキストには複数のWordLists.

アップデート

純粋な Python を使用して同じことを達成することも可能です。次に例を示します。

>>> def ngrams(s, n=2, i=0):
...     while len(s[i:i+n]) == n:
...             yield s[i:i+n]
...             i += 1
...
>>> grams = ngrams('me king of Python'.split())
>>> list(grams)
[['me', 'king'], ['king', 'of'], ['of', 'Python']]
于 2015-10-22T16:38:39.357 に答える