npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

spellchecker-wasm

v0.3.3

Published

An extremely fast multi-thread capable Spellchecker written in Rust compiled to WebAssembly for Node.

Downloads

729

Readme

Spellchecker + WebAssembly

When you absolutely, positively have to have the fastest spellchecker in the room, accept no substitutes.

Build Status Coverage Status

  • Fast - Based on SymSpell v6.6 with bigram support.
  • Plug and play - Ready to go out of the box (batteries included).

Spellcheck-wasm is an extremely fast spellchecker for WebAssembly complete with tooling for leveraging Worker threads to guarantee lightning fast processing of a single word or very large documents without the use of native Node plugins. Sub-millisecond benchmarks bring near native speeds to spellchecking in Node.

Spellcheck-wasm uses a zero dependency Rust port of the extremely popular SymSpell engine with several optimizations for WebAssembly.

| | | |---------------|-----------:| | Electron | ✓ | | Node | ✓ | | Browsers | ✓ | | Workers | ✓ | | Cli | ✓ |

Installation

npm i -s spellchecker-wasm

As an interactive CLI

npm i -g spellchecker-wasm

Then use spellcheck to enter interactive mode. For supported arguments, run spellcheck --help.

Usage in Electron

// Within the preload script of your BrowserWindow instance
const { webFrame } = require('electron');
const { SpellcheckerWasm }  = require('spellchecker-wasm');

const wasmPath = require.resolve('spellchecker-wasm/lib/spellcheck-wasm.wasm');
const dictionaryLocation = require.resolve('spellchecker-wasm/lib/frequency_dictionary_en_82_765.txt');
const spellchecker = new SpellcheckerWasm();

spellchecker.prepareSpellchecker(wasmPath, dictionaryLocation)
    .then(() => {
        let suggestions;
        spellchecker.resultsHandler = results => {
            suggestions = results;
        };

        webFrame.setSpellCheckProvider('en-US', {
            spellCheck(words, callback) {
                const misspelledWords = [];
                words.forEach(word => {
                    spellchecker.checkSpelling(word); // synchronous
                    if (suggestions.length) {
                        misspelledWords.push(word);
                    }
                });
                callback(misspelledWords);
            }
        })
    })

Usage in Node

import { SpellcheckerWasm } from 'spellchecker-wasm';
const wasmPath = require.resolve('spellchecker-wasm/lib/spellchecker-wasm.wasm');
const dictionaryLocation = require.resolve('spellchecker-wasm/lib/frequency_dictionary_en_82_765.txt');
// Optional bigram support for compound lookups - add only when needed
const bigramLocation = require.resolve('spellchecker-wasm/lib/frequency_bigramdictionary_en_243_342.txt');

const spellchecker = new SpellcheckerWasm(resultHandler);
spellChecker.prepareSpellchecker(wasmPath, dictionaryLocation, bigramLocation)
    .then(() => {
        ['tiss', 'gves', 'practiclly', 'instent', 'relevent', 'resuts'].forEach(word => spellchecker.checkSpelling(word));
        spellchecker.checkSpellingCompound('tiss cheks th entir sentance')
    });

function resultHandler(results) {
    // Results are given in the same order they are sent.
    // The most relevant results are order lower in the results index.
    process.stdout.write(results.map(r => r.term));
}

Usage as a Node Worker

const { SpellcheckerWasm } = require('../lib/nodejs/SpellcheckerWasm.js');

const wasmPath = require.resolve('spellchecker-wasm/lib/spellchecker-wasm.wasm');
const dictionaryLocation = require.resolve('spellchecker-wasm/lib/frequency_dictionary_en_82_765.txt');
// Optional bigram support for compound lookups - add only when needed
const bigramLocation = require.resolve('spellchecker-wasm/lib/frequency_bigramdictionary_en_243_342.txt');

let resultHandler = (results) => {process.stdout.write(results.map(r => r.term) + '\n');};
let spellcheckerWasm = new SpellcheckerWasm(resultHandler);
spellcheckerWasm.prepareSpellchecker(wasmPath, dictionaryLocation, bigramLocation)
    .then(() => {
        process.stdout.write('Ready\n');
        process.stdin.on('data', data => {
            spellcheckerWasm.checkSpelling('' + data);
        });
    })
    .catch((e) => {
        process.stdout.write(`Error initializing the SpellChecker\n${e}\n`);
    });

Usage in the Browser

import { SpellcheckerWasm } from 'spellchecker-wasm/lib/browser/index.js';

let resultHandler = (results) => console.log("Results : ", results.map(result => result.term));

async function initializeSpellchecker() {
    const wasm = await fetch('spellchecker-wasm/lib/spellchecker-wasm.wasm');
    const dictionary = await fetch('spellchecker-wasm/lib/frequency_dictionary_en_82_765.txt');
    const bigramLocation = await fetch('spellchecker-wasm/lib/frequency_bigramdictionary_en_243_342.txt'); // Optional

    const spellchecker = new SpellcheckerWasm(resultHandler);
    await spellchecker.prepareSpellchecker(wasm, dictionary, bigramLocation);
    return spellchecker;
}

initializeSpellchecker().then(spellchecker => {
    ['tiss', 'gves', 'practiclly', 'instent', 'relevent', 'resuts'].forEach(word => spellchecker.checkSpelling(word));
    spellchecker.checkSpellingCompound('tiss cheks th entir sentance');
});

Common use cases

Differentiating between a correct word and a word with no suggestions

By default, the spellchecker will return no results for both 'there' and 'thereeeee'. The former is correct and so will not produce suggestions. The latter word is obviously a mistake, but its distance from any word in the dictionary is greater than the maxEditDistance.

To distinguish between the two, one can use the includeUnknown option :

let lastResults;
const resultsHandler = results => {
    lastResults = results;
};

spellchecker.checkSpelling('there');
// lastResults.length === 0

spellchecker.checkSpelling('thereeeee');
// lastResults.length === 0

spellchecker.checkSpelling('thereeeee', {
    includeUnknown: true,
    maxEditDistance: 2,
    verbosity: 2,
    includeSelf: false
});
// lastResults.length === 1

Allowing for deeper word searches

Given that the default maxEditDistance, which controls up to which edit distance words from the dictionary should be treated as suggestions, is 2, words such as cofvvvfee will not return suggestions.

This can be remedied as follows:

let lastResults;
const resultsHandler = results => {
    lastResults = results;
};

spellchecker.checkSpelling('cofvvvfee');
// lastResults.length === 0

spellchecker.checkSpelling('cofvvvfee', {
    includeUnknown: false,
    maxEditDistance: 4,
    verbosity: 1,
    includeSelf: false
});
// lastResults.length === 1, lastResults[0] --> 'coffee'

Caveat : the maxEditDistance parameter that is passed to checkSpelling must be less-than-or-equal to the dictionaryEditDistance parameter of prepareSpellchecker. E.g. :

// BAD!
await spellchecker.prepareSpellchecker(wasmPath, dictionaryLocation); // Default value of dictionaryEditDistance is 2
let lastResults;
const resultsHandler = results => {
    lastResults = results;
};
spellchecker.checkSpelling('cofvvvfee', {
    includeUnknown: false,
    maxEditDistance: 4,
    verbosity: 1,
    includeSelf: false
});
// ERROR!
// Good
await spellchecker.prepareSpellchecker(wasmPath, dictionaryLocation, null, {countThreshold: 2, dictionaryEditDistance: 4});
let lastResults;
const resultsHandler = results => {
    lastResults = results;
};
spellchecker.checkSpelling('cofvvvfee', {
    includeUnknown: false,
    maxEditDistance: 4,
    verbosity: 1,
    includeSelf: false
});
// lastResults.length === 1

Controlling the amount and ordering of returned suggestions

The verbosity parameter to checkSpelling can be used to tweak the amount of suggestions returned. Its supported values are :

verbosity:
    0: (top) returns only the suggestion with the highest term frequency of the suggestions of smallest edit distance found,
    1: (closest) returns all suggestions of smallest edit distance found, suggestions ordered by term frequency,
    2: (all) returns all suggestions within maxEditDistance, suggestions ordered by edit distance, then by term frequency,

Building from source

Prerequisites

This project requires rust v1.30+ since it contains the wasm32-unknown-unknown target out of the box.

Install rust:

curl https://sh.rustup.rs -sSf | sh

Install the stable compiler and switch to it.

rustup install stable
rustup default stable

Install the wasm32-unknown-unknown target.

rustup target add wasm32-unknown-unknown --toolchain stable

Install node with npm then run the following command from the project root.

npm install

Install the wasm-bindgen-cli tool

cargo install wasm-bindgen-cli

The project can now be built using:

npm run build

The artifacts from the build will be located in the /libs directory.