I want to print any text between pair of tags <en> as long as x='PERS', I tried that below, but the output was not what I wanted.
XML Sample
<Text>
<PHRASE>
<en x='PERS'> John </en>
<V> Went </V>
<prep> to </prep>
<V> meet </V>
<en x='PERS'> Alex </en>
</PHRASE>
<PHRASE>
<en x='PERS'> Mark </en>
<V> lives </V>
<prep> in </prep>
<en x='LOC'> Florida </en>
</PHRASE>
<PHRASE>
<en x='PERS'> Nick </en>
<V> visited</V>
<en x='PERS'> Anna </en>
</PHRASE>
</TEXT>
I want the output: John-Alex,Nick-Anna. but I got : Mark-Mark. Meaning that I only want to print 2 PERS when they appear in one phrase
This is the code I wrote, I used element tree.
import xml.etree.ElementTree as ET
tree = ET.parse('output.xml')
root = tree.getroot()
print("------------------------PERS-PERS-------------------------------")
PERS_PERScount=0
for phrase in root.findall('./PHRASE'):
ens = {en.get('x'): en.text for en in phrase.findall('en')}
if 'PERS' in ens and 'PERS' in ens:
print("PERS is: {}, PERS is: {} /".format(ens["PERS"], ens["PERS"]))
#print(ens["ORG"])
#print(ens["PERS"])
PERS_PERScount = PERS_PERScount + 1
print("Number of PERS-PERS relation", PERS_PERScount)
I am not sure if the problem is in print or the if condition, or both ?!