JamSpell Documentation

repository·master·Indexed 20 days ago

https://github.com/bakwc/jamspell

A fast, accurate, multi-language spell checking library written in C++ that uses context to improve correction accuracy. It features a built-in HTTP API, a command-line tool for training and scoring, and provides bindings for Python and other languages via SWIG. Key components include the TSpellCorrector class for text correction and the TLangModel class for model lifecycle management.

Tokens
2.6K
Snippets
7
Records
12
Agent score
70%

What's inside JamSpell

  1. Train a custom JamSpell model

    master

    To train a custom model, you need a UTF-8 text file containing sentences (training data) and a text file containing the language alphabet.

    1. Build JamSpell from source using cmake.
    2. Run the ./main/jamspell train command providing the alphabet file, the training sentences file, and the desired output model filename.
    3. (Optional) Evaluate the model using the evaluate/evaluate.py script.
    # Training command
    ./main/jamspell train ../test_data/alphabet_en.txt ../test_data/sherlockholmes.txt model_sherlock.bin
    
    # Evaluation command
    python evaluate/evaluate.py -a alphabet_file.txt -jsp your_model.bin -mx 50000 your_test_data.txt
  2. Install and use JamSpell in Python

    master

    To use JamSpell in Python, you must first install swig3 via your system's package manager. Then, install the jamspell package using pip. You will need a pre-trained language model (e.g., en.bin) or a model you have trained yourself. Use jamspell.TSpellCorrector() to initialize the corrector and LoadLangModel() to load your model file.

    # 1. Install swig3 (via your distro package manager)
    # 2. Install jamspell
    pip install jamspell
    import jamspell
    
    # Initialize and load model
    corrector = jamspell.TSpellCorrector()
    corrector.LoadLangModel('en.bin')
    
    # Fix a text fragment
    print(corrector.FixFragment('I am the begt spell cherken!'))
    # Output: u'I am the best spell checker!'
    
    # Get top N candidates for a list of words
    print(corrector.GetCandidates(['i', 'am', 'the', 'begt', 'spell', 'cherken'], 3))
  3. Use the JamSpell HTTP API

    master

    JamSpell includes a built-in HTTP server. To use it, build the project using cmake and run the web_server executable. The server provides endpoints for fixing text and retrieving spelling candidates.

    Endpoints:

    • GET/POST /fix: Fixes the provided text.
    • GET/POST /candidates: Returns a JSON object containing misspelled words, their positions (pos_from), lengths (len), and a list of candidates.
    # Build and run the server
    git clone https://github.com/bakwc/JamSpell.git
    cd JamSpell && mkdir build && cd build
    cmake .. && make
    
    # Run server (example with English model on localhost:8080)
    ./web_server/web_server en.bin localhost 8080
    # Fix text via GET
    curl "http://localhost:8080/fix?text=I am the begt spell cherken"
    
    # Fix text via POST
    curl -d "I am the begt spell cherken" http://localhost:8080/fix
    
    # Get candidates
    curl -d "I am the begt spell cherken" http://localhost:8080/candidates
  4. Integrate JamSpell in C++

    master

    To use JamSpell in a C++ project, add the jamspell and contrib directories to your project structure. Include <jamspell/spell_corrector.hpp> and use the NJamSpell::TSpellCorrector class. The corrector requires a loaded language model via LoadLangModel().

    #include <jamspell/spell_corrector.hpp>
    
    int main(int argc, const char** argv) {
        NJamSpell::TSpellCorrector corrector;
        corrector.LoadLangModel("model.bin");
    
        // Fix a fragment
        corrector.FixFragment(L"I am the begt spell cherken!");
    
        // Get candidates
        corrector.GetCandidates({L"i", L"am", L"the", L"begt", L"spell", L"cherken"}, 3);
        
        return 0;
    }
  5. Use the /fix HTTP endpoint

    master

    The /fix endpoint returns the corrected version of the input text using the loaded JamSpell model.

    Request Methods

    • GET: Expects the text as a query parameter named text.
    • POST: Expects the text in the request body.

    Response

    • Returns the corrected text as text/plain.

    Examples

    GET Request: GET /fix?text=thiss is a tset

    POST Request: POST /fix with body thiss is a tset

  6. Use TLangModel for training and scoring

    master

    The TLangModel class is used for model lifecycle management, including training on datasets and scoring text.

    • void Train(const std::string& datasetFile, const std::string& alphabetFile): Trains the model using the specified dataset and alphabet files.
    • void Dump(const std::string& resultModelFile): Saves the trained model to a binary file.
    • bool Load(const std::string& modelFile): Loads a previously saved model from a binary file. Returns true if successful.
    • double Score(const std::wstring& wtext): Returns a score for the provided wide-string text based on the loaded model.
  7. Use TSpellCorrector for text correction

    master

    The TSpellCorrector class provides high-level spell correction capabilities using a loaded TLangModel.

    • bool LoadLangModel(const std::string& modelFile): Loads the language model required for correction. Returns true if successful.
    • std::wstring FixFragment(const std::wstring& text): Takes a wide-string fragment and returns the corrected version of that text.
  8. Use the /candidates HTTP endpoint

    master

    The /candidates endpoint returns a JSON object containing potential spelling corrections for words in the input text that the model identifies as errors.

    Request Methods

    • GET: Expects the text as a query parameter named text.
    • POST: Expects the text in the request body.

    Response Format

    The response is a JSON object with a results array. Each item in the array represents a detected error and contains:

    • pos_from: The starting position of the error in the input string.
    • len: The length of the error word.
    • candidates: An array of up to 7 suggested replacement strings.

    Example JSON Response

    {
        "results": [
            {
                "pos_from": 0,
                "len": 5,
                "candidates": [
                    "this",
                    "thiss"
                ]
            }
        ]
    }
  9. Reference: JamSpell CLI Commands and Arguments

    master

    The following table defines the command-line interface for the JamSpell tool.

    Usage: <executable> mode args
    
    | Mode | Arguments | Description |
    |------|-----------|-------------|
    | `train` | `alphabet.txt dataset.txt resultModel.bin` | Trains a model using the provided alphabet and dataset. |
    | `score` | `model.bin` | Reads sentences from stdin and returns a score for each. |
    | `correct` | `model.bin` | Reads sentences from stdin and returns the corrected text. |
    | `fix` | `model.bin input.txt output.txt` | Automatically fixes text in `input.txt` and writes to `output.txt`. |
  10. Use the JamSpell CLI

    master

    The JamSpell command-line tool provides four primary modes for managing and using spell-checking models: training, scoring, correcting, and batch fixing.

    Modes

    1. train: Trains a new model using an alphabet file and a dataset.

      • Usage: jamspell train <alphabetFile> <datasetFile> <resultModelFile>
    2. score: Reads sentences from standard input and outputs a score for each line based on the provided model.

      • Usage: jamspell score <modelFile>
    3. correct: Reads sentences from standard input and outputs the corrected version of each line.

      • Usage: jamspell correct <modelFile>
    4. fix: Automatically corrects all text within an input file and saves the result to an output file.

      • Usage: jamspell fix <modelFile> <inputFile> <outputFile>
    # Example: Train a model
    jamspell train alphabet.txt dataset.txt resultModel.bin
    
    # Example: Correct text from stdin
    echo "I am the best spell checker!" | jamspell correct resultModel.bin
    
    # Example: Fix a file
    jamspell fix resultModel.bin input.txt output.txt
  11. Run the JamSpell HTTP API server

    master

    The JamSpell HTTP API server allows you to interact with the spell corrector over HTTP. You can start the server by providing a trained language model file, a hostname, and a port number as command-line arguments.

    Usage:

    ./jamspell_server <model.bin> <hostname> <port>

    Arguments:

    • model.bin: Path to the trained JamSpell language model file.
    • hostname: The network interface to bind to (e.g., localhost or 0.0.0.0).
    • port: The port number to listen on (e.g., 8080).
    # Example command to start the server
    ./jamspell_server model.bin localhost 8080