【问题标题】:Simple filtering out of common words from a text description从文本描述中简单过滤掉常用词
【发布时间】:2011-06-07 00:18:17
【问题描述】:

诸如“a”、“the”、“best”、“kind”之类的词。我很确定有很好的方法可以实现这一目标

为了清楚起见,我正在寻找

  1. 可以实现的最简单的解决方案,最好是在 ruby​​ 中。
  2. 我对错误的容忍度很高
  3. 如果我需要一个常用短语库,我也非常满意

【问题讨论】:

  • 我认为您需要提供更多细节。你想要什么样的输入和输出?您要删除常用词并返回确切的内容吗?

标签: ruby text full-text-search taxonomy stop-words


【解决方案1】:

如果您有一个名为stop_words 的单词数组要删除,那么您会从这个表达式中得到结果:

description.scan(/\w+/).reject do |word|
  stop_words.include? word
end.join ' '

如果要保留每个单词之间的非单词字符,

description.scan(/(\w+)(\W+)/).reject do |(word, other)|
  stop_words.include? word
end.flatten.join

【讨论】:

    【解决方案2】:

    等一下,在删除停用词(也就是干扰词、垃圾词)之前,您需要进行一些研究。索引大小和处理资源并不是唯一的问题。很大程度上取决于最终用户是否会输入查询,或者您将使用长自动查询。

    所有搜索日志分析表明,人们倾向于在每个查询中输入一到三个单词。当这就是搜索的全部内容时,我们不能失去任何东西。例如,一个集合可能在许多文档上都有“版权”这个词——这使得它非常普遍——但如果索引中没有这个词,就不可能进行精确的短语搜索或邻近相关性排名。此外,搜索最常见的词有完全正当的理由:人们可能正在寻找“The Who”,或者更糟的是,“The The”。

    因此,虽然需要考虑技术问题,并且删除停用词是一种解决方案,但它可能不是您要解决的整体问题的正确解决方案。

    【讨论】:

      【解决方案3】:

      这是 DigitalRoss 答案的变体。

      str=<<EOF
      To be, or not to be: that is the question: 
        Whether 'tis nobler in the mind to suffer
        The slings and arrows of outrageous fortune,
        Or to take arms against a sea of troubles,
        And by opposing end them? To die: to sleep;
        No more; and by a sleep to say we end
        The heart-ache and the thousand natural shocks
        That flesh is heir to, 'tis a consummation
        Devoutly to be wish'd. To die, to sleep;
        To sleep: perchance to dream: ay, there's the rub;
        For in that sleep of death what dreams may come
      EOF
      
      common = {}
      %w{ a and or to the is in be }.each{|w| common[w] = true}
      puts str.gsub(/\b\w+\b/){|word| common[word.downcase] ? '': word}.squeeze(' ')
      

      也相关: What's the fastest way to check if a word from one string is in another string?

      【讨论】:

        【解决方案4】:
          Common = %w{ a and or to the is in be }
        Uncommon = %{
          To be, or not to be: that is the question: 
          Whether 'tis nobler in the mind to suffer
          The slings and arrows of outrageous fortune,
          Or to take arms against a sea of troubles,
          And by opposing end them? To die: to sleep;
          No more; and by a sleep to say we end
          The heart-ache and the thousand natural shocks
          That flesh is heir to, 'tis a consummation
          Devoutly to be wish'd. To die, to sleep;
          To sleep: perchance to dream: ay, there's the rub;
          For in that sleep of death what dreams may come
        }.split /\b/
        ignore_me, result = {}, []
          Common.each { |w| ignore_me[w.downcase] = :Common          }
        Uncommon.each { |w| result << w unless ignore_me[w.downcase[/\w*/]] }
        puts result.join
        


         ,  not  : that   question: 
        Whether 'tis nobler   mind  suffer
         slings  arrows of outrageous fortune,
          take arms against  sea of troubles,
         by opposing end them?  die:  sleep;
        No more;  by  sleep  say we end
         heart-ache   thousand natural shocks
        That flesh  heir , 'tis  consummation
        Devoutly   wish'd.  die,  sleep;
         sleep: perchance  dream: ay, there's  rub;
        For  that sleep of death what dreams may come
        

        【讨论】:

          【解决方案5】:

          这些常用词被称为“停用词” - 这里有一个类似的 stackoverflow 问题:"Stop words" list for English?

          总结一下:

          • 如果您要处理大量文本,则值得收集有关该特定数据集中单词频率的统计数据,并将最常用的单词作为停用词列表。 (您在示例中包含“种类”向我表明,您可能有一组非常不寻常的数据,例如有很多像“种类”这样的口语表达,所以也许您需要这样做。)
          • 既然您说您不太介意错误,那么仅使用其他人制作的英语停用词列表可能就足够了,例如fairly long one used by MySQLanything else that Google turns up

          如果您只是将这些单词放入程序中的散列中,那么过滤任何单词列表应该很容易。

          【讨论】:

            猜你喜欢
            • 2011-11-12
            • 1970-01-01
            • 1970-01-01
            • 2010-09-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-12-09
            • 2014-10-26
            相关资源
            最近更新 更多