【问题标题】:How to convert command line output from Freeling to consumable array如何将命令行输出从 Freeling 转换为可消耗数组
【发布时间】:2017-03-22 02:21:51
【问题描述】:

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

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

【问题讨论】:

  • 你确定你使用的API不能输出token列表吗?会计。对于文档,如果您使用的是命令行,请尝试 --outf token
  • @WiktorStribiżew 它没有。 'shallow' 是结果选项之一,如果我执行另一个名为 'tagged' 的选项,它只会分别标记每个单词/命名实体,并且我没有得到“The Grasshopper Lies Heavy”这本书的树。
  • 使用正则表达式方法,您如何区分真正的_ 和 Freeling 树生成器引入的?您现在要删除所有下划线。还是输出树中没有下划线?
  • @WiktorStribiżew 不会有。但我认为这是一个小问题,我可以做 \w_\w 或类似的事情。但主要问题是将事物转换为可消耗的数组。

标签: arrays regex ruby nlp freeling


【解决方案1】:

到目前为止,您的解决方案:

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", "."]

【讨论】:

    【解决方案2】:

    如果你通过 API 从 Ruby 调用 FreeLing,你可以得到树并随意遍历它。

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

    【讨论】:

    • 我只是这样做了,但是没有用:这行得通 ` "#{analyzer_path} -f #{cfg_path} --inpf plain --outf shallow" ` 这不行 "#{analyzer_path} -f #{cfg_path} --inpf plain --outf shallow --output conll"
    • option "--output conll" 只适用于4.0更新的版本,看来你使用的是3.x
    猜你喜欢
    • 2012-02-04
    • 1970-01-01
    • 1970-01-01
    • 2017-10-19
    • 2012-08-27
    • 2021-03-01
    • 1970-01-01
    • 2017-07-23
    • 1970-01-01
    相关资源
    最近更新 更多