我正在开发一个功能,用于计算一堆文本中单词出现的频率,并告知它们所在的句子,并按照每个单词的出现频率对结果进行排序。例如:
![sample input and out put](https://istack.dev59.com/LxnkG.webp)
目前我已经完成了以下内容:
File.open('sample_text.txt', 'r') do |f| # open a file named "sample_text.txt"
content = f.read # turn the content into a long string
# split the string by sentences
sentences = content.split(/\.|\?|\!/).each do |es|
es.split(/\W|\s/).each do |w|
#split into individual words
#and for each word, find matched words in the content
end
end
end
问题:
1.
是否有更好的正则表达式来分隔句子?现在使用split(/\.|\?|\!/)
会将web 2.0
视为两个句子web 2
和0
。
2. 有人能给我一些提示,如何返回包含某个单词的句子数组吗?