Reputation: 3744
I wrote a code to download the synonyms of the words in a list, locations
. But since a word can have multiple meanings, I used another list, meaning
, to point to the serial number of the meaning I want for that word. Then calculate similarities between the words based on these synonyms found, and then save them in a file.
from nltk.corpus import wordnet as wn
from textblob import Word
from textblob.wordnet import Synset
locations = ['access', 'airport', 'amenity', 'area', 'atm', 'barrier', 'bay', 'bench', 'boundary', 'bridge', 'building', 'bus', 'cafe', 'car', 'coast', 'continue', 'created', 'defibrillator', 'drinking', 'embankment', 'entrance', 'ferry', 'foot', 'fountain', 'fuel', 'gate', 'golf', 'gps', 'grave', 'highway', 'horse', 'hospital', 'house', 'land', 'layer', 'leisure', 'man', 'market', 'marketplace', 'height', 'name', 'natural', 'exit', 'way', 'park', 'parking', 'place', 'worship', 'playground', 'police', 'station', 'post', 'mail', 'power', 'private', 'public', 'railway', 'ref', 'residential', 'restaurant', 'road', 'route', 'school', 'shelter', 'shop', 'source', 'sport', 'toilet', 'tourism', 'unknown', 'vehicle', 'vending', 'machine', 'village', 'wall', 'waste', 'waterway'];
meaning = [0, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 2, 5, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1, 11, 0, 1, 0, 0, 3, 0, 4, 0, 0, 3, 4, 0, 0, 0, 10, 0, 9, 1, 2, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]
ncols = len(locations)
nrows = len(locations)
matrix = [[0] * ncols for i in range(nrows)]
for i in range(0,len(locations)):
word1 = Word(locations[i])
SS1 = word1.synsets[meaning[i]]
for j in range(0,len(locations)):
word2 = Word(locations[j])
SS2 = word1.synsets[meaning[j]]
matrix[i][j] = SS1.path_similarity(SS2)
f = open('Similarities.csv', 'w')
print(matrix, file=f)
But the code gives the following error:
SS2 = word1.synsets[meaning[j]]
IndexError: list index out of range
When I printed out the values of i
and j
, I found that it prints till i=0 and j=36. That means that when j=36, the error arises. The word in the list at index 36 is man
, and the value at index 36 of meaning
is 11.
So, why is this error occuring and how do I fix it?
EDIT: The mistake was in SS2 = word1.synsets[meaning[j]]
. It should have been SS2 = word2.synsets[meaning[j]]
. Sorry.
Upvotes: 0
Views: 684
Reputation: 39
len(word1.synsets)
returns 8 and type(word1.synsets)
returns list. So it's a list with indexes 0 to 7.
your list 'meaning' contains 11 at index 36. so when your loop reaches word1.synsets[11]
you get the index out of range error.
Like Jose said, 7 is the max int you can have in 'meaning'.
Upvotes: 2