【问题标题】:same words in different contexts with Word2VecWord2Vec 在不同上下文中的相同单词
【发布时间】:2022-01-05 09:06:54
【问题描述】:

我想用 Word2Vec 用向量来表示单词。

如果 Word2Vec 的输入中有 2 个相同的单词, 是否有可能为他们获得不同的表示?

有没有不同的方法来解决这个问题?

【问题讨论】:

    标签: python nlp stanford-nlp word2vec


    【解决方案1】:

    首先,很好的问题。 我找到了一个链接,该链接解释了如何在使用 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)
    

    【讨论】:

    • 谢谢,我想保存上下文。在我的任务中,我需要保存每个相同单词的上下文,这就是为什么我要保存每个单词的向量
    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    • 我已经修改了我之前的答案以包含相关代码。这会将字符串拆分为单个单词并删除重复项。
    • 我想保留单词的上下文以调用 word2vec。如何保存上下文?
    • 保留上下文是什么意思?通过上下文,您是指它在文本中的位置吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    • 2018-02-26
    • 2012-03-04
    • 2014-05-26
    • 1970-01-01
    相关资源
    最近更新 更多