python - How to delete synonyms? -
i'm creating code on python 3.4.3. have linguistic program. part of code has delete next word if synonym of previous word. firstly, have create list of synonyms each word. transform our lists sets. eventually, have compare our lists check if have same synonyms. don't know how compare them. have keep 1 word if there synonym of next.
from nltk.corpus import wordnet text = ['','',''] text4 = [] def f4(text): global text4 synonyms = [] sentence in text: d = ' ' sentence = sentence.split(d) word in sentence: syn = [] syn in wordnet.synsets(word): lemma in syn.lemmas(): syn.append(lemma.name()) synonyms.append(syn) synonyms2 = [] x in synonyms: x = set(x) synonyms2.append(x)
my code has delete next word if synonym of previous word.
i suggest different algorithm. here's example:
text = 'run race stroll rush nice lovely mean kind' # example text synonyms = [] # contains list of synonym lists synonyms.append( ['run', 'race', 'rush'] ) # run synonyms synonyms.append( ['nice', 'lovely', 'kind'] ) # nice synonyms def in_synonyms(list_of_synonym_lists, word): """ returns index of synonym list word in; -1 if isn't found. """ index, synonym_list in enumerate(list_of_synonym_lists): if word in synonym_list: return index return -1 # algorithm split_text = text.split() index = 1 while index < len(split_text): if in_synonyms(synonyms, split_text[index]) != -1: # if word in synonyms list if in_synonyms(synonyms, split_text[index]) == in_synonyms(synonyms, split_text[index-1]): # if word before in same synonyms list current delete current # 1 , start on again del(split_text[index]) index = 1 # restart algorithm else: index += 1 # continue on forward text = ' '.join(split_text)
this code:
- creates list of synonyms lists
- iterates through words of text
- if previous word in same list of synonyms current one, delete current 1 , restart algorithm
- else continue on forward
i haven't tested yet hope idea.
Comments
Post a Comment