I am parsing a call transcript. The content of the transcript comes back as a string formatted like the following:
"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...."
There is no new line when each new speaker starts speaking.
I would like to turn the above string into the an array of hash. Something like the following:
[ { 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...." }
]
I think I would need to use some sort of regular expression to parse this, but have no idea how even after spending the morning reading up on it. Any help here would be much appreciated.
Thanks
Update:
To others that may find this useful, here's what I ended up coming up with using the suggested solution below:
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