I am currently creating a system that asks for a couple of words, then replaces it if a synonym of the word is found in the XML file.
Here's the code:
def wordproc(self, word):
lmtzr = nltk.WordNetLemmatizer()
tokens = nltk.word_tokenize(word)
tokens_lemma = [lmtzr.lemmatize(tokens) for tokens in tokens]
tagged = nltk.pos_tag(tokens)
chunking = nltk.chunk.ne_chunk(tagged)
important_words = []
unimportant_tags = ['MD', 'TO', 'DT', 'JJR', 'CC', 'VBZ']
for x in chunking:
if x[1] not in unimportant_tags:
important_words.append(x[0])
print(important_words)
self.words = (important_words)
print(self.words)
self.loop = len(self.words)
self.xmlparse(self.words, self.loop)
def xmlparse(self, words, loops):
root = ElementTree.parse('data/word-test.xml').getroot()
for i in range(loops):
syn_loc = [word for word in root.findall('word') if word.findtext('mainword') == words]
for nym in syn_loc:
print(nym.attrib)
word_loop = self.loop
new_word = (nym.findtext('synonym'))
words = new_word
print(words)
vf = videoPlay()
vf.moviepy(words)
When the words from wordproc is sent to the xmlparse function, it doesn't work. Any guidance? Or am i missing a crucial point? Any help would be great!
EDIT : Here's a short XML file
<synwords>
<word>
<mainword>affection</mainword>
<wordtag>N</wordtag>
<synonym>love</synonym>
</word>
<word>
<mainword>sweetie</mainword>
<wordtag>N</wordtag>
<synonym>love</synonym>
</word>
<word>
<mainword>appreciation</mainword>
<wordtag>N</wordtag>
<synonym>love</synonym>
</word>
<word>
<mainword>beloved</mainword>
<wordtag>N</wordtag>
<synonym>love</synonym>
</word>
<word>
<mainword>emotion</mainword>
<wordtag>N</wordtag>
<synonym>love</synonym>
</word>
And my desired results :
words = ["beloved", "sweetie","affection"]
the results, after comparing to the XML, will then be
words = ["love", "love", "love"]