gpt4 book ai didi

ruby - 将语素数组转换为 Ruby 中的句子

转载 作者:数据小太阳 更新时间:2023-10-29 07:18:34 25 4
gpt4 key购买 nike

我想转换由 PTB-style 生成的语素数组分词器:

["The", "house", "is", "n't", "on", "fire", "."]

一句话:

"The house isn't on fire."

实现此目标的明智方法是什么?

最佳答案

如果我们采纳@sawa 关于撇号的建议并将您的数组设为:

["The", "house", "isn't", "on", "fire", "."]

你可以得到你要找的东西(有标点符号支持!):

def sentence(array)
str = ""
array.each_with_index do |w, i|
case w
when '.', '!', '?' #Sentence enders, inserts a space too if there are more words.
str << w
str << ' ' unless(i == array.length-1)
when ',', ';' #Inline separators
str << w
str << ' '
when '--' #Dash
str << ' -- '
else #It's a word
str << ' ' unless str[-1] == ' ' || str.length == 0
str << w
end
end
str
end

关于ruby - 将语素数组转换为 Ruby 中的句子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15710601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com