首页 > 解决方案 > 当应用 word2vec 时,只显示字符而不是单词 ؟

问题描述

这是我的代码,您可以看到我将句子转换为单词,但是当我在句子中应用 word2vec 模型时我仍然遇到问题我使用阿拉伯语文本 anaconda 版本 4.7.12

sentences = nltk.sent_tokenize(str(sentences1))
sentences = [nltk.word_tokenize(sentence) for sentence in sentences]
for i in range(len(sentences)):
sentences[i] = [word for word in sentences[i] if word not in stopwords.words('arabic')]

sentences = re.sub(r'[^\w\s]','',(str(sentences)))
sentences = re.sub("\d+", "", sentences)
sentences =sentences.strip()
sentences = nltk.word_tokenize(sentences)
from gensim.models import Word2Vec
model = Word2Vec(sentences, min_count=1)
words1 = model.wv.vocab

在 words1 中,词汇只是显示了字母

标签: pythonnlptext-miningword2vec

解决方案


您的标记化和预处理看起来有点混乱。在顶部进行单词标记后,您再次将其全部变回一条巨大的字符串就行了……

sentences = re.sub(r'[^\w\s]','',(str(sentences)))

对该字符串执行另一个词标记化仍然只留下一个列表,其中每个项目都是简单的词——这意味着每个假设的“句子”现在只是一个词,所以它是每个“句子”的字符列表而不是列表的话。

我建议在词标记化之前对字符串进行基于字符串的清理,并且仅将词标记化作为最后一步。并且,sentences在传递给Word2Vec. 例如,如果您要执行:

print(str(iter(sentences).next())

您应该看到 中的第一项sentences,它应该是一个单词列表,而不仅仅是一个字符串。(如果它只是一个字符串,你会看到你所看到的症状——Word2Vec只学习了单个字符。)


推荐阅读