Expand description

LanguageTool-Rust

Rust bindings to connect with LanguageTool server API.

Crates.io docs.rs

  1. About
  2. CLI Reference
  3. API Reference
  4. CHANGELOG
  5. Related Projects
  6. Contributing

About

LanguageTool-Rust (LTRS) is both an executable and a Rust library that aims to provide correct and safe bindings for the LanguageTool API.

Disclaimer: the current work relies on an approximation of the LanguageTool API. We try to avoid breaking changes as much as possible, but we still highly depend on the future evolutions of LanguageTool.

CLI Reference

Screenshot from CLI

The command line interface of LTRS allows to very quickly use any LanguageTool server to check for grammar and style errors. You can install the latest version with cargo:

> cargo install languagetool-rust --all-features

The reference for the CLI can be accessed via ltrs --help.

By default, LTRS uses LanguageTool public API.

Example

> ltrs ping # to check if the server is alive
PONG! Delay: 110 ms
> ltrs languages # to list all languages
[
  {
    "name": "Arabic",
    "code": "ar",
    "longCode": "ar"
  },
  {
    "name": "Asturian",
    "code": "ast",
    "longCode": "ast-ES"
  },
]
> ltrs check --text "Some phrase with a smal mistake"
{
  "language": {
    "code": "en-US",
    "detectedLanguage": {
      "code": "en-US",
      "confidence": 0.99,
      "name": "English (US)",
      "source": "ngram"
    },
    "name": "English (US)"
  },
  "matches": [
    {
      "context": {
        "length": 4,
        "offset": 19,
        "text": "Some phrase with a smal mistake"
      },
      "contextForSureMatch": 0,
      "ignoreForIncompleteSentence": false,
      "length": 4,
      "message": "Possible spelling mistake found.",
      "offset": 19,
      "replacements": [
        {
          "value": "small"
        },
        {
          "value": "seal"
        },
      }
    ]
}
> ltrs --help # for more details

API Reference

If you would like to integrate LTRS within a Rust application or crate, then we recommend reading documentation.

To use LanguageTool-Rust in your Rust project, add to your Cargo.toml:

[dependencies]
languagetool_rust = "version"

Example

use languagetool_rust::{check::CheckRequest, server::ServerClient};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = ServerClient::default();

    let req = CheckRequest::default()
        .with_language("auto")
        .with_text("Some phrase with a smal mistake");

    println!(
        "{}",
        serde_json::to_string_pretty(&client.check(&req).await?)?
    );
    Ok(())
}

Feature Flags

Default Features
  • cli: Adds command-line related methods for multiple structures. This is feature is required to install the LTRS CLI.
Optional Feautres
  • annotate: Adds method(s) to annotate results from check request. If cli feature is also enabled, the CLI will by default print an annotated output.
  • unstable: Adds more fields to JSON responses that are not present in the Model | Example Value but might be present in some cases. All added fields are optional, hence the Option around them.

Here are listed some projects that use LTRS.

  • W.I.P.

Do you use LTRS in your project? Contact me so I can add it to the list!

Contributing

Contributions are more than welcome!

Future features

  • Use Cargo features to enable Clap and others only in bin.rs
  • Construct a good cli
  • Handle all possible responses from LT
  • Document installation procedure
  • Document functions
  • Test commands that need API keys
  • Build test for lib
  • Build automated testing with LT server (GitHub action?)
  • Parse “data” as input to check command
  • Parse parameters from env value with clap/env feature
  • Enhance annotated text

Re-exports

pub use crate::check::CheckRequest;
pub use crate::check::CheckResponse;
pub use crate::languages::LanguagesResponse;
pub use crate::server::ServerClient;
pub use crate::words::WordsAddRequest;
pub use crate::words::WordsAddResponse;
pub use crate::words::WordsDeleteRequest;
pub use crate::words::WordsDeleteResponse;
pub use crate::words::WordsRequest;
pub use crate::words::WordsResponse;

Modules

Structures for check requests and responses.

Structures for languages requests and responses.

Structures for words requests and responses.