3

原文があります

sent = "For 15 years photographer Kari Greer has been documenting wildfires and the men and women who battle them."

とフレーズ:

phrases = [
  "For 15 years",
  "wildfires and the men and women who battle them",
  "has been documenting wildfires",
  "been documenting wildfires and the men and women who battle them",
  "documenting wildfires and the men and women who battle them",
  "them",
  "and the men and women who battle them",
  "battle them",
  "wildfires",
  "the men and women",
  "the men and women who battle them",
  "15 years",
  "photographer Kari Greer"
]

フレーズから元の文を(単語を失うことなく)再構築し、選択したフレーズを新しい配列に格納して、次のように順序を維持したいと思います。

 result = [
   "For 15 years",
   "photographer Kari Greer",
   "has been documenting wildfires",
   "and the men and women who battle them"
]

編集result:要素の数が最小であることが重要です。

編集:これは、より複雑なケースで機能する回答コードのバージョンです。

 sent ="Shes got six teeth Pink says of her 13-month-old daughter but shes not a biter"      
 phrases = ["her 13-month-old daughter", "she", "says of her 13-month-old daughter", "a biter", "got six teeth", "Pink", "of her 13-month-old daughter", "s not a biter", "She", "six teeth", "s got six teeth", "Shes got six"] 

def shortest(string, phrases)
 string = string.gsub(/\.|\n|\'|,|\?|!|:|;|'|"|`|\n|,|\?|!/, '')
 best_result = nil
 phrases.each do |phrase|
  if string.match(/#{phrase}/)
    result = [phrase] + shortest(string.sub(/#{phrase}/, "").strip, phrases)
        best_result = result  if (best_result.nil? || result.size < best_result.size) # && string == result.join(" ")
      end
    end
  best_result || []
end
4

2 に答える 2

1
def solve arr
    len = arr.count
    (len - 1).downto(0) do |i|
        phrase = arr[0..i].join(" ")
        if $phrases.include?(phrase)
            return [phrase] if len - 1 == i
            ans = solve arr[(i + 1)..(len - 1)]
            return [phrase] + [ans] if ans.count != 0
        end
    end
    []
end

words = sent.gsub(".", "").split(" ")
res = solve words
puts res.flatten.inspect

これはうまくいくはずだと思います。一致する最大のフレーズを探し、フレーズの残りの部分をフレーズに分割できるかどうかを確認します。

おそらくもっと良い方法がありますが、ここでは午前中の4つです...

于 2012-07-02T22:52:12.937 に答える
1
def shortest(string, phrases)
  best_result = nil
  phrases.each do |phrase|
    if string.match(/\A#{phrase}/)
      result = [phrase] + shortest(string.sub(/\A#{phrase}/, "").strip, phrases)
      best_result = result if (best_result.nil? || result.size < best_result.size) && string.match(Regexp.new("\\A#{result.join("\\s?")}\\Z"))
    end
  end
  best_result || []
end
result = shortest(sent.gsub(/\./, ""), phrases)

編集:一部のフレーズの間にスペースを入れないようにアルゴリズムを更新しました。

于 2012-07-02T23:32:06.547 に答える