1

我为此使用 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]]][.]]

那么,我怎样才能得到想要的数组呢?

4

2 回答 2

2

从你的解决方案到目前为止:

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-08T12:50:45.347 回答
0

如果通过 API 从 Ruby 中调用 FreeLing,就可以随意获取树并遍历它。

如果您正在使用命令行程序的输出并将其作为字符串加载到 Ruby 中,则使用选项“--output conll”可能更容易调用它,这将生成更易于处理的表格格式。

于 2016-11-30T07:56:32.663 回答