在一些 Udpipe 函数的帮助下,我刚刚进入了 NLP(语法)的基础知识。作为第一个任务,我决定将简单肯定句的树转换为疑问句的树:
我做很多不同的运动。→ 你做很多不同的运动吗?
原句的树:
tree = Conllu(filehandle=StringIO(parse)).read_tree()
writer = TextModeTrees(attributes="ord,form,lemma,upos,feats,deprel", layout="align")
writer.process_tree(tree)
# sent_id = 1
# text = I do many different sports.
─┮
│ ╭─╼ 1 I I PRON Case=Nom|Number=Sing|Person=1|PronType=Prs nsubj
╰─┾ 2 do do VERB Mood=Ind|Tense=Pres|VerbForm=Fin root
│ ╭─╼ 3 many many ADJ Degree=Pos amod
│ ┢─╼ 4 different different ADJ Degree=Pos amod
┡─┶ 5 sports sport NOUN Number=Plur obj
╰─╼ 6 . . PUNCT _ punct
首先,我尝试交换代词“I”和动词“Do”的位置,将动词放在句子的开头,然后是代词(Do I many different sports.):
from udapi.core.node import Node
pron_node, verb_node = tree.descendants[1], tree.descendants[2]
verb_node.shift_before_node(pron_node)
writer = TextModeTrees(attributes="ord,form,lemma,upos,feats,deprel", layout="align")
writer.process_tree(tree)
但这是我在树中得到的(看起来它交换了很多而不是我出于某种原因):
# sent_id = 1
# text = I do many different sports.
─┮
│ ╭─╼ 1 I I PRON Case=Nom|Number=Sing|Person=1|PronType=Prs nsubj
│ │ ╭─╼ 2 many many ADJ Degree=Pos amod
╰─┾ │ 3 do do VERB Mood=Ind|Tense=Pres|VerbForm=Fin root
│ ┢─╼ 4 different different ADJ Degree=Pos amod
┡─┶ 5 sports sport NOUN Number=Plur obj
╰─╼ 6 . . PUNCT _ punct
此外,如果我尝试重新运行代码的最后一部分,我会收到以下错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-137-565caed2b1dd> in <module>()
2
3 pron_node, verb_node = tree.descendants[1], tree.descendants[2]
----> 4 verb_node.shift_before_node(pron_node)
5
6 writer = TextModeTrees(attributes="ord,form,lemma,upos,feats,deprel", layout="align")
/usr/local/lib/python3.7/dist-packages/udapi/core/node.py in shift_before_node(self, reference_node, without_children, skip_if_descendant)
612 if skip_if_descendant:
613 return
--> 614 raise ValueError(f'{reference_node} is a descendant of {self}. Consider without_children=1.')
615 self._shift_before_ord(reference_node._ord, without_children=without_children)
616
ValueError: <1#2, many> is a descendant of <1#3, do>. Consider without_children=1.
我真的不知道为什么它与许多人而不是我一起运作以及我做错了什么。