0

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"]
4
  • could you show a simple xml and what's the result you are expecting? Commented Mar 4, 2018 at 12:09
  • Done, i have added a small XML sample and a desired result Commented Mar 4, 2018 at 12:15
  • still it's being unclear, you have posted a code where many functions use is not clear to us, also try to mention what are you going to pass and what you want to get, to be; it's unclear with your statements- your desire output is in list but you want to replace it but where? in xml or at runtime? Commented Mar 4, 2018 at 12:26
  • i am going to pass a list with words(from an input box), and the desired result is, the words inside the list would be replaced by the new words from the XML file. Runtime, i guess? Commented Mar 4, 2018 at 12:39

1 Answer 1

1

Instead of looking for word in xml and parse it every time I suggest you can map your word and synonym in python dictionary and then you can very easily lookup or manipulate as you desire. I have use beautifulsoup to parse xml below:

xml = """<synwords>
<word>
    <mainword>affection</mainword>
    <wordtag>N</wordtag>
    <synonym>love</synonym>
</word>

.
.
.

<synwords>"""

from bs4 import BeautifulSoup

soup = BeautifulSoup(xml, "html.parser")  # xml is your xml content
words = soup.find_all('word')
mapped_dict = {word.find("mainword").text: word.find("synonym").text for word in words}
print(mapped_dict)

Output:

{'sweetie': 'love', 'beloved': 'love', 'appreciation': 'love', 'affection': 'love', 'emotion': 'love'}
Sign up to request clarification or add additional context in comments.

8 Comments

can the xml variable be replaced with the XML file that i was using? It is one of the important things that is supposed to be used.
yes you can read file and store content of file in any variable you want
Okay thanks! Ill search on how to parse the file. Thank you so much!
One last thing, what is the result of the mapped_dict above? I am confused
check the answer I have added output of print statement
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.