Search code examples
pythonpandasnumpynlpwordnet

Pointwise Operation with a List of Strings


I'd like to do a pointwise Operation with a list of strings. The list contains words (nouns) and could look like this:

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

Now I want to do a pointwise operation with this list and get a matrix with the results. The size of the matrix depends on the size of the Input list. (In this case 4x4 values) The Operation is based within a function what compares the words for similarity and Returns a float number.

The function looks like that:

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)

I wasnt able to find a solution on Google so far but maybe someone can help me to solve this since I dont know what this is called what Im Looking for.

Thank you for your help.


Solution

  • I may not be understanding the problem correctly, but why not

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