【问题标题】:Is there a way to vectorize only words i.e not from a corpus or bag of words in python?有没有办法只矢量化单词,即不是来自python中的语料库或单词包?
【发布时间】:2019-04-02 13:16:07
【问题描述】:

我的用例是将两个列表中的单词向量化,如下所示。

ListA = [Japan, Electronics, Manufacturing, Science]

ListB = [China, Electronics, AI, Software, Science]

我知道 word2vecGlove 可以向量化单词,但它们是通过语料库或词袋来实现的,即我们必须传递被分解为标记的句子,然后将其向量化。

有没有办法对列表中的单词进行矢量化?

PS。我是 NLP 方面的新手,因此请原谅任何明显的观点。

【问题讨论】:

    标签: python nlp cosine-similarity


    【解决方案1】:

    您可能正在寻找的只是预训练的嵌入。是这样吗?如果是这样,你可以使用这个:

    import spacy
    
    nlp = spacy.load('en_core_web_md')
    tokens = nlp(' '.join(ListA+ListB))
    
    for token1 in tokens:
        for token2 in tokens:
            print(token1.text, token2.text, token1.similarity(token2))
    

    【讨论】:

    • 嗨@EthanKoch。感谢您的回答。 for 循环中的 token1 和 token 2 会遍历 ListA 和 ListB 并存储它们的值吗?当我运行这个 for 循环时,我收到一个错误“'str' object has no attribute 'text'。
    • @RidhimaKumar 非常抱歉——我忘了​​在字符串上调用 nlp
    • 嗨@Ethan,没有问题我想通了。它现在运行良好。只是最后一个问题。我得到了具有各自余弦值的单词对。如何按余弦值的降序对其进行排序。例如我有日本 - 中国(0.3),日本电子(0.5),日本 - 人工智能(0.2)。我希望它按降序排列并分别打印这对的后半部分,即中国、电子、人工智能。再次感谢您帮助像我这样的 NLP 新手。
    • 我在这个问题中添加了另一个答案。一定要点赞!乐于助人。
    【解决方案2】:

    这是您sort it in descending order of cosine values 在我的其他评论中回答您的问题的方式:

    import spacy
    
    nlp = spacy.load('en_core_web_md')
    tokens = nlp(' '.join(ListA+ListB))
    list_to_sort = []
    
    for token1 in tokens:
        for token2 in tokens:
            list_to_sort.append((token1.text, token2.text, token1.similarity(token2))
    
    sorted_list = sorted(list_to_sort, key=lambda x: x[2], reverse=True)
    print(sorted_list)
    

    【讨论】:

    • 嗨@Ethan。上面的代码通过降余弦值对单词对进行排序。如何获得每个词的前 3 个相似词。输出示例将是 inline 'china-Japan 0.7, china-electronics 0.6, china-science 0.6'
    • 嗨@Ethan。上面的代码通过降余弦值对单词对进行排序。如何获得每个词的前 3 个相似词。输出示例为“china-Japan 0.7, china-electronics 0.6, china-science 0.6”。基本上对于ListB中的每个词,都需要对应的top 3映射。
    【解决方案3】:

    我假设您希望查看 ListA 中与 ListB 中每个词最相似的前 3 个词。如果是这样,这是您的解决方案(如果您希望所有与 ListB 中的词最相似的词,我也为此添加了一个可选行):

    import spacy
    
    nlp = spacy.load('en_core_web_md')
    tokensA = nlp(' '.join(ListA))
    # use if wanting tokens in ListB compared to all tokens present: tokensA = nlp(' '.join(ListA+ListB))
    tokensB = nlp(' '.join(ListB))
    
    output_mapping = {tokenB.text: [] for tokenB in tokensB}
    for tokenB in tokensB:
        for tokenA in tokensA:
            # add the tuple to the current list & sort by similarity
            output_mapping[tokenB.text].append((tokenA.text, tokenB.similarity(tokenA)))
            output_mapping[tokenB.text] = list(sorted(output_mapping[tokenB.text], key=lambda x: x[1], reverse=True))
    
    for tokenB in sorted(output_mapping.keys()):
        # print token from listB and the top 3 similarities to list A, sorted
        print(tokenB, output_mapping[key][:3])
    

    【讨论】:

    • 嗨,有没有办法在 Spacy 的模型中为令牌添加异常?我的意思是我有两个列表 ListA = [Japan, Electronics, Manufacturing, Science, cloud] ListB = [China, Machine Learning, Artificial Intelligence, Software development, Science, cloud computing] 。相似性给了我(云,云)而不是(云,云计算)。有没有办法让“nlp”对象保留带有空格的标记。目前,如果令牌之间有空格,它似乎会将令牌分成两部分。
    猜你喜欢
    • 2023-03-06
    • 1970-01-01
    • 2020-08-05
    • 1970-01-01
    • 1970-01-01
    • 2018-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多