繁体   English   中英

如何将 Freeling 的命令行输出转换为可消费数组

[英]How to convert command line output from Freeling to consumable array

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

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

从您目前的解决方案来看:

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM