diff --git a/lib/text_cleaner.rb b/lib/text_cleaner.rb index 05537f0affb..a74ee4ead69 100644 --- a/lib/text_cleaner.rb +++ b/lib/text_cleaner.rb @@ -28,8 +28,8 @@ class TextCleaner text.gsub!(/\?+/, '?') if opts[:deduplicate_question_marks] # Replace all-caps text with regular case letters text.tr!('A-Z', 'a-z') if opts[:replace_all_upper_case] && (text =~ /[A-Z]+/) && (text == text.upcase) - # Capitalize first letter - text.sub!(/\A([a-z])/) { |first| first.capitalize } if opts[:capitalize_first_letter] + # Capitalize first letter, but only when entire first word is lowercase + text.sub!(/\A([a-z]*)\b/) { |first| first.capitalize } if opts[:capitalize_first_letter] # Remove unnecessary periods at the end text.sub!(/([^.])\.+(\s*)\z/, '\1\2') if opts[:remove_all_periods_from_the_end] # Remove extraneous space before the end punctuation diff --git a/spec/components/text_cleaner_spec.rb b/spec/components/text_cleaner_spec.rb index 22170dc1980..07d51136541 100644 --- a/spec/components/text_cleaner_spec.rb +++ b/spec/components/text_cleaner_spec.rb @@ -57,15 +57,18 @@ describe TextCleaner do let(:lowercased) { "this is awesome" } let(:capitalized) { "This is awesome" } + let(:iletter) { "iLetter" } it "ignores first letter case by default" do TextCleaner.clean(lowercased).should == lowercased TextCleaner.clean(capitalized).should == capitalized + TextCleaner.clean(iletter).should == iletter end it "capitalizes first letter when enabled" do TextCleaner.clean(lowercased, capitalize_first_letter: true).should == capitalized TextCleaner.clean(capitalized, capitalize_first_letter: true).should == capitalized + TextCleaner.clean(iletter, capitalize_first_letter: true).should == iletter end end