0

description最大500語の長いテキスト文字列があります。私は次のことをしたいと思います:

  1. ループしてdescription、配列から多数の事前定義された単語を探しますkeywords。これには、単一の単語、単語のペア、および単語のトリプレットが含まれます。
  2. 一致するものが見つかるたびに、この一致を新しい配列にmatches追加し(プロセスの前半ですでに追加されている場合を除く)、一致した単語をから削除しdescriptionます。

私は解決策を探しましたが、それらのほとんどは、現在のニーズには複雑すぎる自然言語処理の最深部に飛び込むか、テキスト文字列をスペースで分割するだけのようです。その後、単語のペアを探すことは不可能です。

これを効率的に行う方法についてのアイデアをいただければ幸いです。

4

3 に答える 3

1
description = "The quick brown fox jumped over the lazy dog, and another brown dog"

keywords = ["brown", "lazy", "apple"]

matches = []

keywords.each do |keyword|
  matches << description.match(keyword).to_s if description.match(keyword)
end

puts matches
 #=> ["brown", "lazy"]

matches.each do |keyword|
  description.gsub!(Regexp.new(keyword), '')
end

description.gsub!('  ', ' ')

puts description
 #=> "The quick fox jumped over the dog, and another dog"
于 2012-10-18T18:04:33.113 に答える
0

配列内の各単語のしきい値頻度を設定できます

説明のテキストをループします

If word matches exactly with description text then increase the threshold frequency by 1 point

最後に、頻度が0より大きい単語は、新しい配列の一致に入れて、そこから削除します。description

例えば、

If any word repeated for 2 times,
It's frequency will be 0 + 2 and
Initially it should be 0.
于 2012-10-18T17:04:11.550 に答える
0

これは私に起こった大雑把なハックです:)

keywords.select do |keyword| 
  description =~ /\b#{Regexp.escape(keyword)}\b/
  # -or-
  description.gsub(/\b#{Regexp.escape(keyword)}\b/) do |match|
    # whatever
  end
end
于 2012-10-18T17:27:49.547 に答える