In Example 11.8 the new field appeared at the bottom of the entry. Modify this program so that it inserts the new subelement right after the lx field. (Hint: create the new cv field using Element('cv'), assign a text value to it, then use the insert() method of the parent element.)
Доволі все просто.
import nltk,re,pprint
from nltk.corpus import toolbox
from nltk.etree.ElementTree import Element
lexicon = toolbox.xml('rotokas.dic')
def cv(s):
s = s.lower()
s = re.sub(r'[^a-z]', r'_', s)
s = re.sub(r'[aeiou]', r'V', s)
s = re.sub(r'[^V_]', r'C', s)
return (s)
def add_cv_field(entry):
for field in entry:
if field.tag == 'lx':
cv_field = Element('cv')
cv_field.text = cv(field.text)
entry.insert(1,cv_field)
print nltk.toolbox.to_sfm_string(lexicon[53])
add_cv_field(lexicon[53])
print nltk.toolbox.to_sfm_string(lexicon[53])
Немає коментарів:
Дописати коментар