Gingerice

Gingerice is a simple ruby gem that corrects grammar and spelling errors in your sentences. To use it:

  • You provide it with a sentence as a string
  • Gingerice will examine this sentence to find problems
  • Gingerice will output a hash that contains the total number of mistakes found in the sentence, it describes likely mistakes, it suggests corrections for these mistakes, and provides a the corrected sentence.

The Gem can be found at: https://rubygems.org/gems/gingerice

Installing Gingerice

In your gem file add Gingerice

gem 'gingerice', '~> 1.2', '>= 1.2.2'

Then install your gem using the Terminal (OSX) or Command Prompt (Windows)

bundle install 

Usage

To use it, first require the gem at the top of your project.

require 'gingerice'

Next you can use the following code to parse a sentence


text = 'the assigment is soo difficult.'  
parser = Gingerice::Parser.new
output = parser.parse text

# print out the findings
puts output

The output is a hash and the correct sentence is stored as a value in the key “result”. It will look something like this:


output:
{
    "text" => "the assigment is soo difficult.",
    "result" => "The assignment is so difficult.",
    "corrections" => [
      [0] {
        "text" => "the",
        "correct" => "The",
        "definition" => nil,
        "start" => 0,
        "length" => 3
      },
      [1] {
        "text" => "assigment",
        "correct" => "assignment",
        "definition" => a duty that you are assigned to perform (especially in the armed forces)",
        "start" => 4,
        "length" => 9
      },
      [2] {
        "text" => "soo",
        "correct" => "so",
        "definition" => to a very great extent or degree,
        "start" => 17,
        "length" => 3
    }
  ]
}

When you have the output, you can pull the bits of information you want from the hash, for example:

output["corrections"][1]["text"]
#output "assigment"

Using it with Twilio

To use it with Twilio, you might do something like this:


get "/sms/incoming" do

	# Get the incoming SMS message content i.e. Body
	# and the who the SMS is From 
	body = params[:Body] || ""
	sender = params[:From] || ""

	# use the body as input to Gingerice
	
	parser = Gingerice::Parser.new
	output = parser.parse body

	# print out the findings
	puts output
	
	if output["corrections"].length == 0
		message = "Thank you for a well constructed sentence."
	else
		message = "I think you meant to say: " + output["result"]
	end
	
	
  # Build a twilio response object 
	# This will be sent back in XML 
  twiml = Twilio::TwiML::MessagingResponse.new do |r|
    r.message do |m|

      # add the text of the response
      m.body( message )
			
    end 
  end
	
  # send a response to twilio 
  content_type 'text/xml'
  twiml.to_s

end 


Credit:

Originally contributed by: Linyi Zhou, Online Prototypes, 2017

Modified to include additional examples.

×

Subscribe

The latest tutorials sent straight to your inbox.