计算每行字符串中的元音,代词的数量?

时间:2016-11-02 14:04:12

标签: ruby

我需要计算用户输入的字符串的每一行中的元音,单词,代词(“他,她,他们”)的数量。如果输入是“他们正在播放。他正在学习”输出预期是句子1有3个单词,有4个元音,1个代词。 \ nSentence 2有3个单词,4个元音,1个代词。我编写了以下代码,但收到错误意外 - 输入结束。

string = gets
string =string.chomp


sentencecount = 0
wordcount = 0
pronouns={"He"=>0,"She"=>0,"They"=>0,"Them"=>0}
procount=0;
string.split(".").each do |sentence|
  wordcount = 0
  sentencecount += 1     #tracking number of sentences
   vowels=sentence.scan(/[aeoui]/).count
   procount=0
  sentence.split(/\w+/).each do |word|
    pronouns.each do|key,value|
        if (key.eq word)
            procount++

    wordcount += 1  # tracking number of words
  end

  puts "Sentence #{sentencecount} has #{wordcount} words, has  #{vowels} vowels"
end 

3 个答案:

答案 0 :(得分:0)

  • 你不需要在行尾添加分号
  • if需要end(除非它是内嵌形式)
  • ++运算符不存在于Ruby
  • 您将两个字符串与==运算符进行比较(它实际上是一种方法)

    string = gets.chomp
    
    sentencecount = 0
    wordcount = 0
    pronouns = {"He"=>0, "She"=>0, "They"=>0, "Them"=>0}
    procount = 0
    string.split(".").each do |sentence|
      wordcount = 0
      sentencecount += 1     #tracking number of sentences
      vowels = sentence.scan(/[aeoui]/).count
      procount = 0
      sentence.split(/\w+/).each do |word|
        pronouns.each do|key, value|
          if key == word
            procount += 1
          end
          wordcount += 1  # tracking number of words
        end
      end
    
      puts "Sentence #{sentencecount} has #{wordcount} words, has  #{vowels} vowels"
    end 
    

答案 1 :(得分:0)

根据您的初步尝试,我对其进行了稍微优化,并使其在代码方面更具可读性。

string = gets.chomp
pronouns = ['he', 'she', 'they', 'them']
total_word_count = 0
total_procount = 0
total_vowel_count = 0

sentences = string.split(".")
total_sentence_count = sentences.size

sentences.each_with_index do |sentence, idx|
  # VOWELS
  vowel_count = sentence.scan(/[aeoui]/).count
  total_vowel_count += vowel_count

  # WORDS
  words = sentence.split
  sentence_word_count = words.size
  total_word_count += sentence_word_count

  # PRONOUNS
  sentence_procount = 0
  words.each do |word|
    sentence_procount += 1 if pronouns.include?(word.downcase)
  end

  total_procount += sentence_procount

  puts "Sentence #{idx + 1} has #{sentence_word_count} words, has  #{vowel_count} vowels"
end

puts "Input has #{total_sentence_count} sentences, #{total_word_count} words, #{total_vowel_count} vowels, and #{total_procount} pronouns"

答案 2 :(得分:0)

我建议你返回一个哈希数组,每行一个,每个哈希包含相关行的统计信息。然后,您可以使用该数组中的信息执行所需的操作。

VOWELS   = 'aeiou'
PRONOUNS = %w| he she they them |
  #=> ["he", "she", "they", "them"] 
PRONOUN_REGEX = /\b#{Regexp.union(PRONOUNS)}\b/
  #=> /\b(?-mix:he|she|they|them)\b/

def count_em(str)
  str.downcase.split(/\n/).map { |line|
    { vowels:   line.count(VOWELS),
      words:    line.scan(/[[:lower:]]+/).count,
      pronouns: line.scan(PRONOUN_REGEX).size } }
end

a = count_em "He thought that she thought that he did not know the truth\n" +
             "René knew she would torment them until they went bananas\n"
  #=> [{:vowels=>14, :words=>12, :pronouns=>3},
  #    {:vowels=>15, :words=>10, :pronouns=>3}] 

a.each.with_index(1) { |h,i|
  puts "In line #{i} there are %d vowels, %d words and %d pronouns" %
    h.values_at(:vowels, :words, :pronouns) }
  # In line 1 there are 14 vowels, 12 words and 3 pronouns
  # In line 2 there are 15 vowels, 10 words and 3 pronouns

puts "The total number of vowels in all lines is %d" %
  a.map { |h| h[:vowels] }.reduce(:+)
  # The total number of vowels in all lines is 29

这个解决方案不完整,因为它不处理收缩(“不要”),所有权(“玛丽”),缩写(“第一”),姓名首字母和数字部分(“J. Philip( 'Phil')Sousa III“)以及其他英语语言。