【问题标题】:Count capitalized of each sentence in a paragraph Ruby计算段落中每个句子的大写 Ruby
【发布时间】:2015-06-02 17:44:33
【问题描述】:

我回答了我自己的问题。忘记初始化count = 0

我在一个段落中有一堆句子。

  1. a = "Hello there. this is the best class. but does not offer anything." 为例。
  2. 要弄清楚第一个字母是否大写,我的想法是.split这个字符串,这样a_sentence = a.split(".")
  3. 我知道我可以"hello world".capitalize! 所以如果它是nil 这对我来说意味着它已经大写了 编辑
  4. 现在我可以使用数组方法来遍历值并使用'.capitalize!
  5. 而且我知道我可以检查是否有 .strip.capitalize!.nil?

但我似乎无法输出有多少是大写的。

编辑

 a_sentence.each do |sentence|
        if (sentence.strip.capitalize!.nil?)
            count += 1
            puts "#{count} capitalized"
        end
    end

它输出:

1 capitalized

感谢您的所有帮助。我将坚持使用我只能在 Ruby 中了解的框架内理解的上述代码。 :)

【问题讨论】:

  • 您收到此错误是因为您需要先定义count。尝试在循环外使用 count += 1 之前添加 count = 0

标签: ruby arrays split capitalize


【解决方案1】:

试试这个:

b = []
a.split(".").each do |sentence|
  b << sentence.strip.capitalize
end
b = b.join(". ") + "."
#  => "Hello there. This is the best class. But does not offer anything."

【讨论】:

    【解决方案2】:

    您的帖子标题具有误导性,因为从您的代码来看,您似乎想获取句子开头大写字母的数量。

    假设每个句子都以句点(句号)后跟一个空格结束,那么以下内容应该适合您:

    split_str = ". "
    regex = /^[A-Z]/
    
    paragraph_text.split(split_str).count do |sentence|
      regex.match(sentence)
    end
    

    如果您只想确保每个首字母大写,您可以尝试以下方法:

    paragraph_text.split(split_str).map(&:capitalize).join(split_str) + split_str
    

    【讨论】:

      【解决方案3】:

      你可以计算有多少是大写的,像这样:

      a = "Hello there. this is the best class. but does not offer anything."
      a_sentence = a.split(".")
      
      a_sentence.inject(0) { |sum, s| s.strip!; s.capitalize!.nil? ? sum += 1 : sum }
      # => 1 
      
      a_sentence
      # => ["Hello there", "This is the best class", "But does not offer anything"] 
      

      然后把它放回去,像这样:

      "#{a_sentence.join('. ')}."
      #  => "Hello there. This is the best class. But does not offer anything." 
      

      编辑

      正如@Humza 所建议的,您可以使用count

      a_sentence.count { |s| s.strip!; s.capitalize!.nil? }
      # => 1
      

      【讨论】:

      • 看看我的回复。你可以简单地将一个块传递给count,让事情变得更容易。
      • @victorkohl 是的,我喜欢 a_sentence.count { |s| s.strip!; s.capitalize!.nil? } 更好:) 谢谢!
      【解决方案4】:

      不需要将字符串拆分成句子:

      str = "It was the best of times. sound familiar? Out, damn spot! oh, my." 
      
      str.scan(/(?:^|[.!?]\s)\s*\K[A-Z]/).length
        #=> 2
      

      可以通过在结束 / 之后添加 x 来编写正则表达式:

      r = /
           (?:        # start a non-capture group
            ^|[.!?]\s # match ^ or (|) any of ([]) ., ! or ?, then one whitespace char
            )         # end non-capture group
            \s*       # match any number of whitespace chars
            \K        # forget the preceding match
            [A-Z]     # match one capital letter
          /x
      
      a = str.scan(r)
        #=> ["I", "O"]  
      a.length
        #=> 2
      

      您可以使用其别名sizeArray#count,而不是Array#count

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-03-06
        • 2013-08-13
        • 2014-02-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-27
        相关资源
        最近更新 更多