有没有办法只向量化单词,即不是来自 python 中的语料库或词袋?
Is there a way to vectorize only words i.e not from a corpus or bag of words in python?
我的用例是向量化两个列表中的单词,如下所示。
ListA = [Japan, Electronics, Manufacturing, Science]
ListB = [China, Electronics, AI, Software, Science]
我知道 word2vec
和 Glove
可以对单词进行矢量化,但它们是通过语料库或词袋来实现的,即我们必须传递被分解为标记的句子,然后对其进行矢量化。
有没有办法只对列表中的单词进行矢量化?
PS。我是 NLP 方面的新手,因此请原谅任何明显的观点。
您可能正在寻找的只是预训练的嵌入。是这样吗?如果是这样,你可以使用这个:
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))
这是您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)
我假设您希望看到 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])
我的用例是向量化两个列表中的单词,如下所示。
ListA = [Japan, Electronics, Manufacturing, Science]
ListB = [China, Electronics, AI, Software, Science]
我知道 word2vec
和 Glove
可以对单词进行矢量化,但它们是通过语料库或词袋来实现的,即我们必须传递被分解为标记的句子,然后对其进行矢量化。
有没有办法只对列表中的单词进行矢量化?
PS。我是 NLP 方面的新手,因此请原谅任何明显的观点。
您可能正在寻找的只是预训练的嵌入。是这样吗?如果是这样,你可以使用这个:
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))
这是您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)
我假设您希望看到 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])