Generate bindings for other languages
masterjamspell.i interface file with the SWIG tool.repository·master·Indexed 20 days ago
https://github.com/bakwc/jamspellA 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.
jamspell.i interface file with the SWIG tool.To train a custom model, you need a UTF-8 text file containing sentences (training data) and a text file containing the language alphabet.
cmake../main/jamspell train command providing the alphabet file, the training sentences file, and the desired output model filename.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.txtTo 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 jamspellimport 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))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/candidatesTo 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;
}The /fix endpoint returns the corrected version of the input text using the loaded JamSpell model.
text.text/plain.GET Request:
GET /fix?text=thiss is a tset
POST Request:
POST /fix with body thiss is a tset
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.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.The /candidates endpoint returns a JSON object containing potential spelling corrections for words in the input text that the model identifies as errors.
text.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.{
"results": [
{
"pos_from": 0,
"len": 5,
"candidates": [
"this",
"thiss"
]
}
]
}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`. |The JamSpell command-line tool provides four primary modes for managing and using spell-checking models: training, scoring, correcting, and batch fixing.
train: Trains a new model using an alphabet file and a dataset.
jamspell train <alphabetFile> <datasetFile> <resultModelFile>score: Reads sentences from standard input and outputs a score for each line based on the provided model.
jamspell score <modelFile>correct: Reads sentences from standard input and outputs the corrected version of each line.
jamspell correct <modelFile>fix: Automatically corrects all text within an input file and saves the result to an output file.
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.txtThe 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