What's a Rails Plugin, or Ruby Gem, to Automatically Fix English Grammar

What's a Rails plugin, or Ruby gem, to automatically fix English grammar?

I don't know of anything with those particular features.

However, you might look at Ruby LinkParser, which is a Ruby wrapper for the Link Grammar parser developed by academics and used by the Abiword project for grammar checking. (Note that "link" in Link Grammer parser doesn't refer to HTML links, but rather to a structure that describes English syntax as a set of links between words).

Here's another interesting checker, written in Ruby, which is designed to check LaTex files for some of the problems you mention (plus others).

Is there any built in method/gem to convert string into English sentence

I found gingerice gem that might apt for my question.

BTW thank you all for having time for me.

Its working as expected for me.
output

Regards,

Sreeraj

Grammar - How to match optional and required whitespaces before and after words?

I'm not familiar with Nearly nor Moo but the regex could be

whitespace : ([ \t]*,[ \t]*|[ \t])

and your grammar would become

word %whitespace word

Hopefully that makes sense and I didn't completely botch up the language.

Fixing sentences: add space after punctuation but not after decimal points or abbreviations

I understand you want to ignore the periods inside numbers and period-separated single-letter chunks with an optional period right after.

Here is a code snippet that implements the logic I described above:

import re

mystring = 'this is my first sentence with (brackets)in it. this is the second?What about this sentence with D.D.T. in it?or this with 4.5?'

def fix_punctuation(text):
def sentence_case(text):
# Split into sentences. Therefore, find all text that ends
# with punctuation followed by white space or end of string.
sentences = re.findall(r'(?:\d+\.\d+|\b[A-Z](?:\.[A-Z])*\b\.?|[^.!?])+[.!?](?:\s|\Z)', text)

# Capitalize the first letter of each sentence
sentences = [x[0].upper() + x[1:] for x in sentences]

# Combine sentences
return ''.join(sentences)

#add space after punctuation
text = re.sub(r'(\d+\.\d+|\b[A-Z](?:\.[A-Z])*\b\.?)|([.,;:!?)])\s*', lambda x: x.group(1) or f'{x.group(2)} ', text)
#capitalize sentences
return sentence_case(text)

print(fix_punctuation(mystring))
# => This is my first sentence with (brackets) in it. This is the second?
# What about this sentence with D.D.T. in it? Or this with 4.5?

See the Python demo.

The re.findall pattern, (?:\d+\.\d+|\b[A-Z](?:\.[A-Z])*\b\.?|[^.!?])+[.!?](?:\s|\Z), matches

  • (?:\d+\.\d+|\b[A-Z](?:\.[A-Z])*\b\.?|[^.!?])+ - one or more occurrences of
    • \d+\.\d+ - one or more digits, ., one or more digits
    • | - or
    • \b[A-Z](?:\.[A-Z])*\b\.? - a word boundary, an uppercase letter, zero or more repetitions of a period and an uppercase letter, a word boundary and an optional .
    • | - or
    • [^.!?] - a char other than ., ! and ?
  • [.!?] - ., ! or ?
  • (?:\s|\Z) - a whitespace or end of string.

The re.sub pattern, (\d+\.\d+|\b[A-Z](?:\.[A-Z])*\b\.?)|([.,;:!?)])\s*, matches and captures into Group 1 those patterns that we want to skip, and then matches and captures into Group 2 some punctuation chars and then matches any zero or more whitespace chars (to make sure we only have a single space after them), and a custom logic is used in the replacement argument, in the lambda expression.

Titileize method to capitalize large words in titles

Because q[0] is "the", so when i is "the", it satisfies the condition:

i == q[0]

A better way to do it is:

Little = %w[over the and]
def titleize s
s.gsub(/\w+/)
.with_index{|w, i| i.zero? || Little.include?(w).! ? w.capitalize : w}
end

titleize("the bridge over the river kwai")
# => "The Bridge over the River Kwai"


Related Topics



Leave a reply



Submit