For example, I have this list :
word1 = ['organization', 'community']
And I have a function to get a synonyms from the words of the list :
from nltk.corpus import wordnet as wn
def getSynonyms(word1):
synonymList1 = []
for data1 in word1:
wordnetSynset1 = wn.synsets(data1)
tempList1 = []
for synset1 in wordnetSynset1:
synLemmas = synset1.lemma_names()
for s in synLemmas:
word = s.replace('_', ' ')
if word not in tempList1:
tempList1.append(word)
synonymList1.append(tempList1)
return synonymList1
syn1 = getSynonyms(word1)
print(syn1)
and here's the output :
[
['organization', 'organisation', 'arrangement', 'system', 'administration',
'governance', 'governing body', 'establishment', 'brass', 'constitution',
'formation'],
['community', 'community of interests', 'residential district',
'residential area', 'biotic community']
]
^ the output above shows that each synsets for both 'organization' and 'community' are sublisted inside a list. and then I reduce the level of list :
newlist1 = [val for sublist in syn1 for val in sublist]
and here's the output :
['organization', 'organisation', 'arrangement', 'system', 'administration',
'governance', 'governing body', 'establishment', 'brass', 'constitution',
'formation', 'community', 'community of interests', 'residential district',
'residential area', 'biotic community']
^ now all the synsets remain the same strings without sublist. and what I'm trying to do now is to make all the synsets in newlist1 to be sublisted each other. I expect the output would be like this :
[['organization'], ['organisation'], ['arrangement'], ['system'],
['administration'], ['governance'], ['governing body'], ['establishment'],
['brass'], ['constitution'], ['formation'], ['community'],
['community of interests'], ['residential district'], ['residential area'],
['biotic community']]
I'm trying this code :
uplist1 = [[] for x in syn1]
uplist1.extend(syn1)
print(uplist1)
but the results is not what I expected:
[[], [],
['organization', 'organisation', 'arrangement', 'system', 'administration',
'governance', 'governing body', 'establishment', 'brass', 'constitution',
'formation'],
['community', 'community of interests', 'residential district',
'residential area', 'biotic community']]
It shows two empty lists and two lists of synsets for both 'organization' and 'community'
How to make each strings of synsets a sublist?