2016-08-12 5 views
0

配列内の各単語の段落を検索しようとしていて、見つかった単語だけを新しい配列として出力しようとしています。Rubyで `match`を使って段落に文字列を追加する場合

これまでのところ、私は希望の出力形式を得ることができませんでした。

paragraph = "Japan is a stratovolcanic archipelago of 6,852 islands. 
The four largest are Honshu, Hokkaido, Kyushu and Shikoku, which make up about ninety-seven percent of Japan's land area. 
The country is divided into 47 prefectures in eight regions." 

words_to_find = %w[ Japan archipelago fishing country ] 

words_found = [] 

words_to_find.each do |w| 
    paragraph.match(/#{w}/) ? words_found << w : nil 
end 

puts words_found 

現在、出力は縦書きのリストです。

Japan 
archipelago 
country 

しかし、私は['Japan', 'archipelago', 'country']、のような何かをしたいと思います。

私は段落の中でテキストにマッチする経験はあまりなく、ここで何が間違っているのか分かりません。誰か助言を与えることができますか?

+1

を使用します1行に1つの要素を出力するのは 'puts'です。 – Aetherus

+0

ああありがとう。私は 'puts'と' p'を読み返す必要があります。 –

+0

P.S.それは本当に何であるかを見るために 'p words_found'することができます。 – Aetherus

答えて

0

これは、putsを使用して配列の要素を印刷しているためです。すべての要素「単語」の終わりに"\n"を追加:

#!/usr/bin/env ruby 
def run_me 



    paragraph = "Japan is a stratovolcanic archipelago of 6,852 islands. 
    the four largest are Honshu, Hokkaido, Kyushu and Shikoku, which make up about ninety-seven percent of Japan's land area. 
    the country is divided into 47 prefectures in eight regions." 

    words_to_find = %w[ Japan archipelago fishing country ] 


    find_words_from_a_text_file paragraph , words_to_find 



end 



def find_words_from_a_text_file(paragraph , *words_to_find) 
    words_found = [] 

    words_to_find.each do |w| 
       paragraph.match(/#{w}/) ? words_found << w : nil 
    end 

    # print array with enum . 
    words_found.each { |x| puts "with enum and puts : : #{x}" } 

    # or just use "print , which does not add anew line" 
    print "with print :"; print words_found "\n" 

    # or with p 
    p words_found 

end 


run_me 

出力:

za:ruby_dir za$ ./fooscript.rb 
with enum and puts : : ["Japan", "archipelago", "fishing", "country"] 
with print :[["Japan", "archipelago", "fishing", "country"]] 
0

ここではそれを行うにはいくつかの方法があります。どちらも大文字と小文字を区別しません。

は `words_found`が何をしたいすでにある。正規表現

r =/
    \b        # Match a word break 
    #{ Regexp.union(words_to_find) } # Match any word in words_to_find 
    \b        # Match a word break 
    /xi        # Free-spacing regex definition mode (x) 
            # and case-indifferent (i) 
    #=>/
    # \b        # Match a word break 
    # (?-mix:Japan|archipelago|fishing|country) # Match any word in words_to_find 
    # \b        # Match a word break 
    # /ix       # Free-spacing regex definition mode (x) 
            # and case-indifferent (i) 

paragraph.scan(r).uniq(&:itself) 
    #=> ["Japan", "archipelago", "country"] 

交差二つの配列

words_to_find_hash = words_to_find.each_with_object({}) { |w,h| h[w.downcase] = w } 
    #=> {"japan"=>"Japan", "archipelago"=>"archipelago", "fishing"=>"fishing", 
     "country"=>"country"} 

words_to_find_hash.values_at(*paragraph.delete(".;:,?'"). 
           downcase. 
           split. 
           uniq & words_to_find_hash.keys) 
    #=> ["Japan", "archipelago", "country"] 
関連する問題