【发布时间】:2022-01-05 09:06:54
【问题描述】:
我想用 Word2Vec 用向量来表示单词。
如果 Word2Vec 的输入中有 2 个相同的单词, 是否有可能为他们获得不同的表示?
有没有不同的方法来解决这个问题?
【问题讨论】:
标签: python nlp stanford-nlp word2vec
我想用 Word2Vec 用向量来表示单词。
如果 Word2Vec 的输入中有 2 个相同的单词, 是否有可能为他们获得不同的表示?
有没有不同的方法来解决这个问题?
【问题讨论】:
标签: python nlp stanford-nlp word2vec
首先,很好的问题。 我找到了一个链接,该链接解释了如何在使用 python 用向量表示单词时避免重复。 https://towardsdatascience.com/learn-word2vec-by-implementing-it-in-tensorflow-45641adaf2ac
您可以通过以下方式拆分文本并避免重复单词:
corpus_raw = 'He is the king . The king is royal . She is the royal queen '
# convert to lower case
corpus_raw = corpus_raw.lower()
# ---- dictionary words -> int and int -> words ----
words = []
for word in corpus_raw.split():
if word != '.': # because we don't want to treat . as a word
words.append(word)
words = set(words) # so that all duplicate words are removed
word2int = {}
int2word = {}
vocab_size = len(words) # gives the total number of unique words
for i,word in enumerate(words):
word2int[word] = i
int2word[i] = word
print(words)
【讨论】: