-1

I answered my own question. Forgot to initialize count = 0

I have a bunch of sentences in a paragraph.

  1. a = "Hello there. this is the best class. but does not offer anything." as an example.
  2. To figure out if the first letter is capitalized, my thought is to .split the string so that a_sentence = a.split(".")
  3. I know I can "hello world".capitalize! so that if it was nil it means to me that it was already capitalized EDIT
  4. Now I can use array method to go through value and use '.capitalize!
  5. And I know I can check if something is .strip.capitalize!.nil?

But I can't seem to output how many were capitalized.

EDIT

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

It outputs:

1 capitalized

Thanks for all your help. I'll stick with the above code I can understand within the framework I only know in Ruby. :)

mythoslife
  • 203
  • 1
  • 6
  • 20
  • 1
    You're getting this error because you need to define `count` first. Try adding `count = 0` before using `count += 1`, outside the loop. – bmpasini Mar 29 '15 at 20:21

4 Answers4

1

Try this:

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."
bmpasini
  • 1,503
  • 1
  • 23
  • 43
1

Your post's title is misleading because from your code, it seems that you want to get the count of capitalized letters at the beginning of a sentence.

Assuming that every sentence is finishing on a period (a full stop) followed by a space, the following should work for you:

split_str = ". "
regex = /^[A-Z]/

paragraph_text.split(split_str).count do |sentence|
  regex.match(sentence)
end

And if you want to simply ensure that each starting letter is capitalized, you could try the following:

paragraph_text.split(split_str).map(&:capitalize).join(split_str) + split_str
SHS
  • 7,651
  • 3
  • 18
  • 28
1

There's no need to split the string into sentences:

str = "It was the best of times. sound familiar? Out, damn spot! oh, my." 

str.scan(/(?:^|[.!?]\s)\s*\K[A-Z]/).length
  #=> 2

The regex could be written with documentation by adding x after the closing /:

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

Instead of Array#length, you could use its alias, size, or Array#count.

Cary Swoveland
  • 106,649
  • 6
  • 63
  • 100
0

You can count how many were capitalized, like this:

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"] 

And then put it back together, like this:

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

EDIT

As @Humza sugested, you could use count:

a_sentence.count { |s| s.strip!; s.capitalize!.nil? }
# => 1
victorkt
  • 13,992
  • 9
  • 52
  • 51
  • 1
    Take a look at my response. You can simply pass a block to `count` and make things a lot easier. – SHS Mar 29 '15 at 20:24
  • @victorkohl yes I like a_sentence.count { |s| s.strip!; s.capitalize!.nil? } better :) thanks! – mythoslife Mar 29 '15 at 20:31