Giter Club home page Giter Club logo

spelly's Introduction

Spelly

Build Status

Pure JavaScript spellchecker that learns as you use it 1

Installation

npm install spelly

Usage

Get suggestions

const Spelly = require("spelly");

let options = {
  cache: {
    type: "configstore",
    store: new Configstore("foo")
    // optional, defaults to internal Configstore instance under "spelly"
  }
};

const spelly = new Spelly("/usr/share/dict/words", options);

let suggestions = spelly.check("wierd");

/**
 * {
 *   original: "wierd",
 *   suggestions: [{
 *     word: "wired",
 *     score: 5
 *   }, {
 *     word: "weird",
 *     score: 5
 *   }, {
 *     word: "wield",
 *     score: 4
 *   }]
 * }
 */

Results will be cached in the chosen store, making Spelly smarter and faster each time it is used.

Managing your own cache

You can manage your own cache of words by adding/removing them to/from the store. When adding a word, all suggestions beneath its score will be shifted down 1. (e.g. a score of 4 goes to 3). If the suggestion already exists, it will be moved appropriately according to the score given.

// add 'wired' with score '1' or move it to the bottom position
spelly.cache("weird", { word: "wired", score: 1 });

// remove a suggestion for a misspelling
spelly.clearCache("somthing", "something");

// remove all suggestions for a misspelling
spelly.clearCache("somthing");

// clear all cache
spelly.clearCache();


// retrieve cached suggestions for a misspelling
spelly.getCache("somthing");

// retrieve all cached suggestions
spelly.getCache();

Feeling lucky?

Grab the first suggestion out of the list

let suggestion = spelly.first("wierd");

/**
 *{
 *  original: "wierd",
 *  suggestion: {
 *    word: "weird",
 *    score: 5
 *  }
 *}
 */

Learning

Spelly uses configstore by default for the suggestion cache, helping make it both fast and smart. Each time a misspelled word is given to Spelly, it finds it in the store, or generates a suggestion, then adds it to the store.

API

Spelly(dictionary, options)

Spelly constructor.

  • dictionary Type: Array|String An array of words or path to a file containing a newline separated list of words.

  • options.cache Type: Object Spelly cache options.

    • cache.type Type: String Cache type (configstore).
    • cache.store Type: Object configstore instance (optional) for configstore.

check(word)

Spellcheck a word. returns a promise with the suggestions Object.

  • word: Type: String The word to spellcheck.

cache(misspelledWord, suggestion)

Add a suggestion to a mispelled word.

  • misspelledWord Type: String The misspelled word to cache.
  • suggestion Type: Object
    • suggestion.word Type: String The suggestion to add for the misspelled word.
    • suggestion.score Type: Number Score the suggestion. If the suggestion exists for the word, it will be re-scored based on the given score and all others below will be shifted down (or up depending on how you look at it) by 1.

clearCache([misspelledWord][,suggestion])

Clear the cache of a single suggestion, multiple suggestions or all suggestions.

  • missppelledWord Type: String(optional) The misspelled word to clear.
  • suggestion Type: String(optional) the suggestion to remove from the misspelled word.

getCache([misspelledWord])

Retrieve the cache for a mispelled word or all words.

  • misspelledWord Type: String(optional) The misspelled word to retrieve cache for.

first(misspelledWord)

Retrieve the first suggestion for a mispelled word

License

See LICENSE

spelly's People

Contributors

dylanfoster avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.