0.0
Low commit activity in last 3 years
A long-lived project that still receives updates
Gem wrapper for the LanguageTool API (public or private). It allows to correct texts.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 3.12

Runtime

 Project Readme

Gem Version CircleCI

languagetool

A simple ruby wrapper for the LanguageTool HTTP API. This gem is only compatible with LanguageTool 3.4 and above.

Please visit the this link for more information about the public LanguageTool HTTP API, and its allowed parameters.

This project uses juwelier for managing and releasing this gem.

Installation

Add this line to your application's Gemfile:

gem 'languagetool'

And then execute:

$ bundle install

Or install it yourself as:

$ gem install languagetool

Usage

First, you need to init the API:

require 'languagetool'
api = LanguageTool::API.new # Public LanguageTool API by default

Please bear in mind that Ruby uses underscore names (long_code), while the LanguageTool API uses camelcase names (longCode).

The gem also works with the premium API too, just set the username and the API key when initializing the API:

LanguageTool::API.new(
  base_uri: 'https://api.languagetoolplus.com/v2',
  common_query_params: {
    username: '...',
    api_key: '...'
  }
)

Available languages

You can retrieve the available languages with languages method:

languages = api.languages
# Play with the result!
languages.last.name      # English (US)
languages.last.code      # en
languages.last.long_code # en-US

Check

A text can be checked an validated with the check method:

check = api.check text: 'this ish a invalid text', language: 'en-US'
# Play with the result!
check.software # LanguageTool::Resources::Software
check.language # LanguageTool::Resources::Language
check.matches  # Array of LanguageTool::Resources::Match

You can also let the gem try to auto-correct the errors:

check.auto_fix # "This is an invalid text."

Error handling

If something went wrong, then the api wrapper will raise an LanguageTool::APIError exception. For example:

begin
  api.check text: 'Text' # Missing required 'language' parameter. Will raise a LanguageTool::APIError
rescue LanguageTool::APIError => e
  puts e.exception
end

Self-host LanguageTool server

First, create your own LanguageTool server (3.4 or above) by following this instructions.

Then, use your custom local or remote url in your ruby code:

require 'languagetool'
api = LanguageTool::API.new base_uri: 'http://localhost:8081/v2'

Custom options

The api initialization accepts the following options:

Name Description Default
base_uri Base URI to make the HTTP requests. https://languagetool.org/api/v2

Authors

This project has been developed by:

Avatar Name Nickname Email
Daniel Herzog Wikiti wikiti.doghound@gmail.com