Ruby 1.9.2p320 で Rails 3.2.11 を使用しています。
summarized()
テキスト文字列として保存されている一連の 1,000 ~ 15,000 語の文書のどこに特定のキーワードが現れるかを示す方法を作成しました。各ドキュメントには、各キーワードを 0 ~ 100 回含めることができます。
私は持っている:
ActiveRecord::Schema.define(:version => 20130404000559) do
create_table "references", :force => true do |t|
t.text "source_text"
end
end
を呼び出すと、に 1 つしかない場合でも@reference.source_text.summarize(keywords)
、次のメソッドは非常に遅くなります。keyword
keywords
class String
def summarized(keywords)
safe_text = Array.new
result = String.new
keywords.each do |keyword|
safe_text << self.strip_tags.gsub(/\n|\r/, " ").gsub(" ", " ").scan(/\w*.{0,250}#{keyword}.{0,250}\w*/im)
end
return safe_text.flatten.uniq
end
end
どうすれば高速化できますか?
更新: 私は現在、それstrip_tags
が少なくとも犯人の 1 人である可能性を調べています。