对字符串列表的点运算

问题描述 投票:1回答:1

我想对一个字符串列表做一个点运算,这个列表包含了单词(名词),可以是这样的。

lst_words = ['car', 'vehicle', 'boat', 'ship']

现在我想对这个列表做一个点向操作 然后得到一个结果的矩阵。矩阵的大小取决于输入列表的大小。(在这种情况下,4x4的值)操作是基于一个函数,比较单词的相似性,并返回一个浮点数。

这个函数看起来是这样的。

import nltk 
from nltk.corpus import wordnet 
# Compare words:
def get_synset(word_01, word_02):
    w1 = wordnet.synset(word_01 + '.n.01')
    w2 = wordnet.synset(word_02 + '.n.01') 
    return w1.wup_similarity(w2)

我无法在谷歌上找到解决方案 但也许有人能帮我解决这个问题 因为我不知道这叫什么 我在找什么。

谢谢你的帮助。

python pandas numpy nlp wordnet
1个回答
0
投票

我可能没有正确理解这个问题,但为什么不这样做呢?

np.array([[get_synset(x, y) for x in list_words] for y in list_words])

0
投票

你可以用 numpy.fromfunction 你唯一要做的改变是改变你的功能,用单词的符号而不是单词本身来工作。

WORDS = ["your", "list", "of", "words"]

def get_synset_by_index(i1, i2):
    return get_synset(WORDS[i1], WORDS[i2])

matrix = numpy.fromfunction(get_synset_by_index, (len(WORDS), len(WORDS))
© www.soinside.com 2019 - 2024. All rights reserved.