Hunspell Spellchecker in Javascript

1.0.2 · abandoned · verified Tue Apr 21

hunspell-spellchecker is a lightweight JavaScript library designed to parse and utilize Hunspell dictionaries (typically `.aff` and `.dic` files) within a JavaScript environment. It facilitates the conversion of these dictionary files into a JSON format, which can then be efficiently loaded and used for spell-checking. The library supports both Node.js for server-side processing and, with careful pre-processing, browser environments. The current stable version is 1.0.2, last published over 11 years ago, indicating it is no longer actively maintained. Its primary differentiator is the ability to work directly with Hunspell dictionary formats, converting them to a more JavaScript-friendly JSON structure for use in applications requiring custom or offline spell-checking capabilities.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the spellchecker, parse Hunspell dictionary files (.aff and .dic) from the filesystem, load the processed dictionary, and then check the spelling of a word. It assumes dictionary files are available locally.

const fs = require('fs');
const path = require('path');
const Spellchecker = require('hunspell-spellchecker');

// Initialize a spellchecker instance
const spellchecker = new Spellchecker();

// Example dictionary files (replace with actual paths)
// For demonstration, these files must exist in your project root
// or be accessible via `fs.readFileSync`.
// You would typically download official Hunspell dictionaries (e.g., from LibreOffice).
const affFilePath = path.join(__dirname, 'en_US.aff'); // Placeholder
const dicFilePath = path.join(__dirname, 'en_US.dic'); // Placeholder

// In a real application, ensure these files exist
// For testing, you might create dummy files or skip this part.
if (!fs.existsSync(affFilePath) || !fs.existsSync(dicFilePath)) {
  console.error('Error: Dictionary files (en_US.aff, en_US.dic) not found.');
  console.error('Please download them or provide valid paths for parsing.');
  console.error('For example, download from: https://extensions.libreoffice.org/extensions/english-dictionaries (rename .oxt to .zip)');
  process.exit(1);
}

// Parse an hunspell dictionary that can be serialized as JSON
const DICT = spellchecker.parse({
    aff: fs.readFileSync(affFilePath),
    dic: fs.readFileSync(dicFilePath)
});

// Load a serialized dictionary into the spellchecker
spellchecker.use(DICT);

// Check a word
const wordToCheck = 'typo';
const isRight = spellchecker.check(wordToCheck);

console.log(`Is '${wordToCheck}' spelled correctly? ${isRight}`); // Should be false

const correctWord = 'hello';
const isCorrect = spellchecker.check(correctWord);
console.log(`Is '${correctWord}' spelled correctly? ${isCorrect}`); // Should be true

view raw JSON →