【发布时间】:2018-03-04 12:07:36
【问题描述】:
我目前正在创建一个系统,它要求输入几个单词,然后在 XML 文件中找到该单词的同义词时替换它。
代码如下:
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)
当来自 wordproc 的单词被发送到 xmlparse 函数时,它不起作用。有什么指导吗?还是我错过了一个关键点?任何帮助都会很棒!
编辑:这是一个简短的 XML 文件
<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>
还有我想要的结果:
words = ["beloved", "sweetie","affection"]
与 XML 比较后的结果将是
words = ["love", "love", "love"]
【问题讨论】:
-
你能展示一个简单的xml吗?你期望的结果是什么?
-
完成,我添加了一个小的 XML 示例和所需的结果
-
还是不清楚,你贴了一段代码,很多功能使用我们不清楚,也试着提一下你要通过什么,你想得到什么,to be;你的陈述不清楚 - 你的愿望输出在列表中,但你想替换它但是在哪里?在 xml 中还是在运行时?
-
我将传递一个包含单词的列表(来自输入框),期望的结果是,列表中的单词将被 XML 文件中的新单词替换。我猜是运行时?