LanguageTool App for Gramex

LanguageTool

LanguageTool is an Open Source proofreading software for English, French, German, Polish, Russian, and more than 20 other languages. It finds many errors that a simple spell checker cannot detect. See the README.

Installation

To use LanguageTool, add the following to your gramex.yaml

import:
  languagetool:
    path: $GRAMEXAPPS/languagetool/gramex.yaml
    YAMLURL: $YAMLURL/languagetool/

This mounts the app at languagetool/.

Usage

LanguageTool accepts two query parameters:

The JSON response consists of an object containing two keys:

Play with LanguageTool

Autocorrect with LanguageTool

LanguageTool may suggest any number of corrections for each error that it finds in the input text. Gramex’s wrapper for LanguageTool automatically applies the first correction it finds for each error, thus generating the autocorrected version of the input text. This output corresponds to the correction key in the output payload. For finer control, users may explore the errors from the output. Each error object contains potential corrections and the offset and length of the erroneous substring from the input text.

The following snippet demonstrates usage of the LanguageTool app assuming the app is mounted at /languagetool and Gramex is running locally at port 9988.

Using LanguageTool with Ajax

The LanguageTool app in Gramex can be used with Ajax as follows:

function checkGrammar(text) {
  $.ajax('languagetool/', {data: { q:  text } })
    .done(function (result) {
      console.log('Did you mean', result.correction)
    })
}
checkGrammar('Tooo many spellng mistaes!')
// Did you mean "Too many spelling mistakes!"

LanguageTool API

LanguageTool can be used with a FunctionHandler as follows:

import json
from tornado.gen import coroutine, Return
from tornado.httpclient import AsyncHTTPClient
from six.moves.urllib_parse import urlencode

@coroutine
def check_grammar(handler):
    client = AsyncHTTPClient()
    url = '{protocol}://{host}/languagetool/?'.format(**vars(handler.request))
    resp = yield client.fetch(url + urlencode({'q': handler.get_argument('q')}))
    result = json.loads(resp.body.decode('utf8'))
    raise Return(result['correction'])

Examples

  1. The quick brown fox jamp over the lazy dog.
  2. how are you
  3. I is fine.

Configuration

The LanguageTool app provides the following configurable YAML variables.