googlesyntaxnet给出了一个输出 就像。。在
saw VBD ROOT
+-- Alice NNP nsubj
| +-- , , punct
| +-- reading VBG rcmod
| +-- who WP nsubj
| +-- had VBD aux
| +-- been VBN aux
| +-- about IN prep
| +-- SyntaxNet NNP pobj
+-- , , punct
+-- Bob NNP dobj
+-- in IN prep
| +-- hallway NN pobj
| +-- the DT det
+-- yesterday NN tmod
+-- . . punct
我想使用python来读取和解析这个输出(字符串数据)。 然后用“带标签的括号符号”打印出来 喜欢 ->
^{pr2}$你能帮我吗?在
不用解析树,您可以让SyntaxNet以更容易解析的conll格式输出所有内容。句子的conll格式如下:
每列的含义可以找到here。目前我们只关心第一列(单词的ID)、第二列(单词本身)和第七列(head,换句话说,父列)。根节点的父节点为0。在
为了得到conll格式,我们只需注释掉最后几行演示.sh(我想你以前经常得到你的输出):
^{pr2}$(别忘了注释掉前一行的反斜杠)
(where I got this trick from, see the comment)
当我跑的时候演示.sh我自己得到了很多我不需要的信息。你怎样才能摆脱我留给你的思考(让我知道:)。 现在,我将相关的部分保存到一个文件中,这样我就可以将它导入我将要编写的python程序中。如果你能把这些信息处理掉,你应该可以用管道演示.sh直接进入python程序。在
注意:我对python还比较陌生,所以请随时改进我的代码。在
首先,我们只想从输入中读取conll文件。我喜欢把每个词都放在一个好的班级里。在
不错,但还不是树形结构。我们得多做点工作。在
我可以把整张单子翻几遍,以便查找每个孩子的每一个单词,但这样做效率很低。我按它们的父对象对它们进行排序,然后它应该只是一个快速查找,以获取给定父对象的每个子对象。在
创建树结构:
为了能够以新格式打印树,可以向Word类添加一些方法重载:
现在您可以这样做:
然后像这样用管子吹:
或直接来自syntaxnet:
相关问题 更多 >
编程相关推荐