我是Word2Vec的新手,我正在尝试根据它们的相似性来聚类。首先,我使用nltk分隔句子,然后使用结果列表作为Word2Vec的输入。然而,当我打印词汇时,它只是一堆字母,数字和符号而不是单词。具体来说,其中一个字母的例子是“<gensim.models.keyedvectors.Vocab object at 0x00000238145AB438>,'L':”
# imports needed and logging
import gensim
from gensim.models import word2vec
import logging
import nltk
#nltk.download('punkt')
#nltk.download('averaged_perceptron_tagger')
with open('C:\\Users\\Freddy\\Desktop\\Thesis\\Descriptions.txt','r') as f_open:
text = f_open.read()
arr = []
sentences = nltk.sent_tokenize(text) # this gives a list of sentences
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s',level=logging.INFO)
model = word2vec.Word2Vec(sentences, size = 300)
print(model.wv.vocab)
由于tutorial和documentation类的Word2Vec
表明类的构造函数需要将单词列表列表作为第一个参数(或一般的单词迭代器的迭代器):
句子(迭代的迭代,可选) - 可迭代的句子可以只是一个令牌列表的列表,但对于更大的语料库,...
我相信在将sentences
喂入Word2Vec
之前你需要在每个句子上使用words_tokenize
来改变关键线:
sentences = [nltk.word_tokenize(sent) for sent in nltk.sent_tokenize(text)]
TL; DR
你得到字母作为你的“单词”,因为Word2Vec
将对应句子的字符串视为包含单词的迭代。迭代字符串会导致字母序列。这些字母用作模型学习的基础(而不是预期的单词)。
正如古语所说:垃圾进去 - 垃圾出来。