【问题标题】:KeyError: "word 'restrictions' not in vocabulary" while generating word embedding vectors for text, read from a textfileKeyError:“词汇‘限制’不在词汇表中”,同时为文本生成词嵌入向量,从文本文件中读取
【发布时间】:2020-09-03 09:02:45
【问题描述】:

当我读取文本文件以生成词嵌入向量时,我收到此错误:“KeyError:单词'restriction'不在词汇表中”,而单词'restrictions'在文本文件中。我想知道我读取文本文件(一个简单的段落)的代码是否错误?

我的代码写在下面:

from gensim.models import Word2Vec
# define training data
with open('D:\\test.txt', 'r') as file:
sentences = ""
#read from textfile
for line in file:
    for word in line.split(' '):
        sentences += word + ' '
# train model
model = Word2Vec(sentences, min_count=1)
# summarize the loaded model
print(model)
# summarize vocabulary
words = list(model.wv.vocab)
# save model
model.save('model.bin')
# load model
new_model = Word2Vec.load('model.bin')
print(new_model)
print(str(model['restriction']))

当我在代码中使用预先写好的句子时,不会出现此错误,如下所示:

from gensim.models import Word2Vec
# define training data
sentences = [['this', 'is', 'the', 'first', 'sentence', 'for', 'word2vec'],  
                ['this', 'is', 'the', 'second', 'sentence'],  
                ['yet', 'another', 'sentence'],  
                ['one', 'more', 'sentence', 'with', 'restriction'],
                ['and', 'the', 'final', 'sentence']]
# train model
model = Word2Vec(sentences, min_count=1)
# summarize the loaded model
print(model)
# summarize vocabulary
words = list(model.wv.vocab)
print(words)
# access vector for one word
print(model['sentence'])
# save model
model.save('model.bin')
# load model
new_model = Word2Vec.load('model.bin')
print(new_model)
print('the model prints: ')
print(model['restriction'])

【问题讨论】:

  • 首先,我会尝试取消缩进整个文件读取代码块。

标签: python deep-learning text-files word2vec


【解决方案1】:

在您的代码中显示问题,在构建它之后,请仔细检查sentences 987654321,看看它是否是您期望的格式(或者在工作情况的sentences 987654322的格式)。我怀疑不是。

还,看看令人失望的模型学习的单词列表 - 你创建的words 5变量应该足够了。它也可能看起来不像你期望的话。

具体地,您的代码...

sentences = ""
for line in file:
    for word in line.split(' '):
        sentences += word + ' '

... make sentences一串长串,有很多空间分开的单词。如果您在工作代码中的sentences @ 987654326,您将不再有列表,其中每个项目是令牌列表。 (这是Word2Vec。)的良好输入格式,而是有一个巨大的run-on string:

sentences = 'this is the first sentence for word2vec this is the second sentence yet another sentence one more sentence with restriction and the final sentence'

尝试:

sentences = []  # empty list
# OOPS, DON'T DO: sentences = ""
for line in file:
    sentences.append(line.split(' '))

...那么你的@ 987654330将是一个字符串列表(如工作情况),而不是只是一个字符串(如破坏的情况)。

【讨论】:

  • 谢谢@gojomo。我想你是对的。 TextFile的句子不会以具有预期格式的可变句子保存,这是主要问题。但遗憾的是,您的建议阅读TextFile的代码没有工作。在将此错误添加到我的代码中获取此错误以阅读TextFile:AttributeError:'str'对象没有属性“附加”。当我将句子更改为Senyence [i]时,我得到了另一个错误:IndexError:字符串索引超出范围 span>
  • 抱歉,我只意味着只有线sentences = [](所以它是一个正确的列表),并从我的原始的复制和粘贴中无意中留下了错误的sentences = ""。不要做sentences = ""。 (我在答案中发表了警告的那条线。) span>
  • 非常感谢。您的建议解决了错误。一个更小的问题:您认为此代码是否会用于为非英语语言生成Word2Vec向量时,例如土耳其语或波斯语? span>
  • Word2Vec 987654334和相关算法在许多语言上工作。最重要的是:(1)“字”单位以不与英语不同的方式分割。 (没有空格之间的语言,如某些亚洲语言,或者像德语一样复合许多单词碎片,可以在Word2Vec工作井之前需要额外的预处理。)(2)大量变化的现实数据。所以:值得一试,但你可能需要注意数据卷/质量,预处理和模型调整,因为您的任何新语言或数据集的very 好的 i>向量。 span >
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-28
  • 1970-01-01
  • 1970-01-01
  • 2015-05-30
  • 2017-06-01
  • 2021-11-26
  • 1970-01-01
相关资源
最近更新 更多