我正在做一些事情来计算一个词在一堆文本中出现的频率,告诉它出现在哪个句子中,并根据每个词的频率对结果进行排序。例如:
这是我目前所拥有的:
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. 谁能给我一些提示,告诉我如何完成返回一个单词所在的句子数组的部分?
最佳答案
在句号(或像
?
或!
这样的标点符号)之后要求一个空格,然后选择性地防止其前面出现某些众所周知的缩写怎么样? (例如vs.
或Mr.
或Mrs.
或i.e.
或e.g.
) ,或许还要求后面有一个大写字母?给定一个句子字符串数组和一个将每个句子拆分为一个单词数组的方法(我会把它留给你),你可以这样做:
sentences_for_word = Hash.new{ |h,k| h[k] = [] } sentences.each do |sentence| words_for_sentence(sentence).each do |word| sentences_for_word[word] << sentence end end
关于ruby - 在 ruby 中拆分句子的更好的正则表达式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8351579/