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 | |
---|---|---|---|
Daniel Herzog | Wikiti | wikiti.doghound@gmail.com |