擅长:python、mysql、java
<p>你需要给它一个条件。像这样:</p>
<pre><code>for x in range(0,len(data)):
titles.append(data[x]['title'])
paragraphs.append(data[x]['paragraphs'])
model = gensim.models.Word2Vec.load('/tmp/models/google2')
for y in range(95,96):
vocabulary.append(titles[y])
vocabulary.append(paragraphs[y][0])
vocabulary.append(paragraphs[y+1][0])
print vocabulary
for entry in vocabulary:
try:
row = tokenizer.tokenize(entry)
row = [word for word in row if word not in stopwords.words('english')]
# in your code, row will be overwritten several times, so I use new variables here
temp = []
temp1 = []
for item in row:
try:
model[item]
except KeyError, e:
continue
temp.append(model[item])
temp1.append(np.sum(item))
last.append(temp1)
except KeyError,e:
print "There is a word that does not exist in the vocabulary: ", e
</code></pre>
<p>希望它能起作用。在</p>