我用Ruby来做这件事。Freeling (一个NLP工具)有一个浅解析器,当我运行一个浅解析命令时,它会为文本“我刚读过书,蚱蜢躺得很重”返回这样的字符串。
a = <<EOT
S_[
sn-chunk_[
+(I i PRP -)
]
adv_[
+(just just RB -)
]
vb-chunk_[
+(read read VB -)
]
sn-chunk_[
(the the DT -)
+n-chunk_[
(book book NN -)
+n-chunk_[
+(The_Grasshopper_Lies_Heavy the_grasshopper_lies_heavy NP -)
]
]
]
st-brk_[
+(. . Fp -)
]
]
EOT我想从这里得到以下数组:
["I", "just", "read", "the book The Grasshopper Lies Heavy","."](我希望合并树下的单词,并将其作为一个数组元素。)
到目前为止,我已经写了这么多:
b = a.gsub(/.*\[/,'[').gsub(/.*\+?\((\w+|.) .*/,'\1').gsub(/\n| /,"").gsub("_","")回传
[[I][just][read][the[book[The Grasshopper Lies Heavy]]][.]]那么,如何获得所需的数组呢?
发布于 2016-11-08 12:50:45
从你的解决方案到目前为止:
result = a.gsub(/.*\[/,'[').gsub(/.*\+?\((\w+|.) .*/,'\1').gsub(/\n| /,"").gsub("_"," ")
result.split('][').map { |s| s.gsub(/\[|\]/, ' ').strip } # ["I", "just", "read", "the book The Grasshopper Lies Heavy", "."]发布于 2016-11-30 07:56:32
如果您通过API从Ruby调用FreeLing,您可以得到树并随意遍历它。
如果您使用命令行程序的输出并将其作为字符串加载到Ruby中,则使用选项“- output conll”调用它可能会更容易,这将产生一种更容易处理的表格格式。
https://stackoverflow.com/questions/40487236
复制相似问题