gpt4 book ai didi

ruby - 在 ruby​​ 中拆分句子的更好的正则表达式?

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

我正在做一些事情来计算一个词在一堆文本中出现的频率,告诉它出现在哪个句子中,并根据每个词的频率对结果进行排序。例如: sample input and out put

这是我目前所拥有的:

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 20

2.谁能给我一些提示,告诉我如何完成返回一个单词所在的句子数组的部分?

最佳答案

  1. 在句号(或像 ?! 这样的标点符号)之后要求一个空格,然后选择性地防止其前面出现某些众所周知的缩写怎么样? (例如 vs.Mr.Mrs.i.e.e.g.) ,或许还要求后面有一个大写字母?

  2. 给定一个句子字符串数组和一个将每个句子拆分为一个单词数组的方法(我会把它留给你),你可以这样做:

    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/

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