ruby-on-rails
  • ruby
  • 2011-11-16 33 views 2 likes 
    2

    我有停止詞的數組:如何從句子中移除單詞的數組?

    myArray = ["","a","ago","also","am","an","and","ani","ar","aren't","arent","as","ask","at","did","didn't","didnt","do","doe","would","be","been","best","better"]

    我想從一個句子中刪除匹配的項目:

    str = 'A something and hello'

    所以就變成:

    'something hello'

    1.我怎樣才能做到這一點在紅寶石?

    2.我怎樣才能做到這一點的字符數組(刪除所有匹配的字符)?

    這裏的字符數組:

    ["(",")","@","#","^"]

    +0

    你想在第二種情況下輸出什麼? – Tilo

    回答

    9
    sentence = 'A something and hello' 
    array = ["","a","ago","also","am","an","and","ani","ar","aren't","arent", 
          "as","ask","at","did","didn't","didnt","do","doe","would", 
          "be","been","best","better"] 
    
    
    sentence.split.delete_if{|x| array.include?(x)}.join(' ') 
    
    => "A something hello" 
    

    你可能想比較之前downcase所有的話,擺脫在句子開頭的「A」的:

    sentence.split.delete_if{|x| array.include?(x.downcase)}.join(' ') 
    
    => "something hello" 
    

    如果你有一個字符串數組,更容易:

    (sentence.split - array).join(' ') 
    => "A something hello" # but note that this doesn't catch the "A" 
    

    也刪除特殊字符:

    special = ["(",")","@","#","^"] 
    
    sentence.split.delete_if{|x| array.include?(x.downcase) || special.include?(x) }.join(' ') 
    

    另一種方法來刪除單詞或短語是:

    array.each do |phrase| 
        sentence.gsub!(/#{phrase}/,'') 
    end 
    
    +0

    謝謝!有沒有類似的方式來做一個字符數組呢? (刪除任何匹配的字符) – user1049097

    +0

    @Tilo如果我不想刪除像「謝謝」這樣的詞並且我已經排在[[「thank you」,.....] –

    +0

    我如何添加示例這樣做 - 看到我的回答 – Tilo

    -1
    array.map {|s| s.gsub(keyword, '')} 
    
    +3

    你應該在你的答案周圍放置一些描述性文字來解釋它在做什麼。 – Spence

    0

    我的解決辦法:

    stop_words = ["","a","ago","also","am","an","and","ani","ar","aren't","arent","as","ask","at","did","didn't","didnt","do","doe","would","be","been","best","better"] 
    output = %w(A something and hello) - stop_words 
    
    +0

    結束很好,但輸入應該是一個字符串 – 2011-11-16 07:05:57

    +1

    also..output應該是另一個字符串,而不是一個數組 – rubyprince

    1

    一個單Tilo的答案的變體是乾淨的和不區分大小寫的(儘管它是重要的)變成全部小寫輸出,這可能不是所有用途的理想選擇):

    (sentence.downcase.split - array).join(' ') 
    
    相關問題