0

我有一个带有两列(名称和描述)的数据库的 Rails 应用程序。我想运行一个脚本,该脚本将在任一列中找到所有唯一单词,并根据它们出现的频率对它们进行排名。这是为了生成索引。

我明白我需要排除某些词(例如“the”和“a”),并且由于复数,计数可能不完美。但我很高兴在后期处理中手动处理这个,我只是在寻找一个基本的脚本,它会给我所有的单词和它们的频率。

有没有人有任何代码可以做到这一点或任何指导如何去做?

4

1 回答 1

1
def unique_word_count
  @thing = Thing.all
  @hash = Hash.new(0)
  @thing.each do |thing|
    name_array = thing.name.split(' ')
    description_array = thing.description.split(' ')
  end
  name_array.each do |word|
    @hash[word] += 1
  end
  description_array.each do |word|
    @hash[word] += 1
  end
end

我还没有运行代码,但这样的东西可能是你正在寻找的。

于 2012-07-09T04:44:53.863 回答