Word2Vec 词汇只产生字母和符号

Word2Vec vocab results in just letters and symbols

我是 Word2Vec 的新手,我正在尝试根据单词的相似性对单词进行聚类。首先,我使用 nltk 来分隔句子,然后使用生成的句子列表作为 Word2Vec 的输入。但是,当我打印 vocab 时,它只是一堆字母、数字和符号,而不是单词。具体来说,其中一个字母的示例是“< 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 and the documentation for Word2Vec class 建议 class 的构造函数需要单词列表列表作为第一个参数(或单词迭代器的迭代器一般来说):

sentences (iterable of iterables, optional) – The sentences iterable can be simply a list of lists of tokens, but for larger corpora,...

我相信在将 sentences 输入 Word2Vec 之前,您需要在每个句子上使用 words_tokenize,将关键行更改为:

sentences = [nltk.word_tokenize(sent) for sent in nltk.sent_tokenize(text)]

TL;DR

你得到字母作为你的 "words" 因为 Word2Vec 将对应于句子的字符串视为包含单词的可迭代对象。迭代字符串会产生字母序列。这些字母用作模型学习的基础(而不是预期的单词)。

俗话说:垃圾进-垃圾出