【发布时间】:2021-11-03 19:04:17
【问题描述】:
我正在解析通话记录。成绩单的内容以字符串形式返回,格式如下:
"Operator: Hi, please welcome Bob Smith to the call. Bob Smith: Hello there, thank you for inviting me...Now I will turn the call over to Stacy. Stacy White: Thanks Bob. As he was saying...."
每个新发言者开始发言时没有换行。
我想把上面的字符串变成一个哈希数组。类似于以下内容:
[ { speaker: "Operator",
content: "Hi, please welcome Bob Smith to the call" },
{ speaker: "Bob Smith",
content: "Hello there, thank you for inviting me...Now I will turn the call over to Stacy." },
{ speaker: "Stacy White",
content: "Thanks Bob. As he was saying...." }
]
我想我需要使用某种正则表达式来解析它,但不知道如何在花了早上阅读它之后。在这里的任何帮助将不胜感激。
谢谢
更新:
对于可能觉得这很有用的其他人,这是我最终使用以下建议的解决方案得出的结论:
def display_transcript
transcript_pretty = []
transcript = self.content
transcript_split = transcript.split(/\W*([A-Z]\w*\W*\w+):\W*/)[1..-1]
transcript_split_2d = transcript_split.each_slice(2).to_a
transcript_split_2d.each do |row|
blurb = { speaker: row[0], content: row[1]}
transcript_pretty << blurb
end
return transcript_pretty
end
【问题讨论】:
标签: ruby-on-rails regex text-parsing