2016-11-02 113 views
0

我需要在用戶輸入的字符串的每一行中計算元音,單詞,代詞(「他,她,他們」)的數量。如果輸入是「他們在玩,他在學習」輸出的預期是句子1有3個單詞,有4個元音,1個代詞。 \ n句子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 

回答

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 
    
+0

它每次運行時給出0作爲代詞 –

+0

是的,在你的代碼中你不更新它們。 – Ursus

+0

給出了procount + = 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" 
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 

這個解決方案是不完整的,因爲它不收縮處理(「不」),所有格(「瑪麗」),縮寫(「1」),英文縮寫和名稱的數字部分(」 J.菲利普('菲爾')索薩三世)和其他英語語言的扭曲。

相關問題