簡體   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