How to get words frequency in efficient way with ruby?

前端 未结 7 1617
伪装坚强ぢ
伪装坚强ぢ 2021-02-05 17:50

Sample input:

\"I was 09809 home -- Yes! yes!  You was\"

and output:

{ \'yes\' => 2, \'was\' => 2, \'i\' => 1, \'home\         


        
相关标签:
7条回答
  • 2021-02-05 18:25
    class String
      def frequency
        self.scan(/[a-zA-Z]+/).each.with_object(Hash.new(0)) do |word, hash|
          hash[word.downcase] += 1
        end
      end
    end
    

    puts "I was 09809 home -- Yes! yes! You was".frequency

    0 讨论(0)
  • 2021-02-05 18:30

    This code will ask you for input and then find the word frequency for you:

        puts "enter some text man"
    text = gets.chomp
    words = text.split(" ")
    frequencies = Hash.new(0)
    words.each { |word| frequencies[word.downcase] += 1 }
    frequencies = frequencies.sort_by {|a, b| b}
    frequencies.reverse!
    frequencies.each do |word, frequency|
        puts word + " " + frequency.to_s 
    end
    
    0 讨论(0)
  • 2021-02-05 18:32
    def count_words(string)
      string.scan(/\w+/).reduce(Hash.new(0)){|res,w| res[w.downcase]+=1;res}
    end
    

    Second variant:

    def count_words(string)
      string.scan(/\w+/).each_with_object(Hash.new(0)){|w,h| h[w.downcase]+=1}
    end
    
    0 讨论(0)
  • 2021-02-05 18:32
    def count_words(string)
      Hash[
        string.scan(/[a-zA-Z]+/)
          .group_by{|word| word.downcase}
          .map{|word, words|[word, words.size]}
      ]
     end
    
    puts count_words 'I was 09809 home -- Yes! yes!  You was'
    
    0 讨论(0)
  • 2021-02-05 18:35

    This works but I am kinda new to Ruby too. There might be a better solution.

    def count_words(string)
      words = string.split(' ')
      frequency = Hash.new(0)
      words.each { |word| frequency[word.downcase] += 1 }
      return frequency
    end
    

    Instead of .split(' '), you could also do .scan(/\w+/); however, .scan(/\w+/) would separate aren and t in "aren't", while .split(' ') won't.

    Output of your example code:

    print count_words('I was 09809 home -- Yes! yes!  You was');
    
    #{"i"=>1, "was"=>2, "09809"=>1, "home"=>1, "yes"=>2, "you"=>1}
    
    0 讨论(0)
  • 2021-02-05 18:37

    This works, and ignores the numbers:

    def get_words(my_str)
        my_str = my_str.scan(/\w+/)
        h = Hash.new(0)
        my_str.each do |s|
            s = s.downcase
            if s !~ /^[0-9]*\.?[0-9]+$/ 
                h[s] += 1
            end
        end
        return h
    end
    
    print get_words('I was there 1000 !')
    puts '\n'
    
    0 讨论(0)
提交回复
热议问题