我为此使用 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]]][.]]
那么,我怎样才能得到想要的数组呢?