擅长:python、mysql、java
<p>试试这个!你知道吗</p>
<pre><code>from nltk import ngrams
def generate_ngrams(sentences,window_size =3):
for sentence in sentences:
yield from ngrams(sentence[0].split(), window_size)
sentences= [["my name is John"] , ["This PC is black"]]
for c in generate_ngrams(sentences,3):
print (c)
#output:
('my', 'name', 'is')
('name', 'is', 'John')
('This', 'PC', 'is')
('PC', 'is', 'black')
</code></pre>