Giter VIP home page Giter VIP logo

suffix-thumb's Introduction

find the optimal transormations between sets of words
npm install suffix-thumb

discover the minimal rules for mapping two sets of words to one another, according to changes in their suffix.

It was built for learning rules about verb conjugations, but in a way, it is just a generic compression algorithm.

The assumption is that a word's suffix is the most-often changed part of a word.

preview

Learn โ†’ Convert

import { learn, convert } from 'suffix-thumb'

let pairs = [
  ['walk', 'walked'],
  ['talk', 'talked'],
  ['go', 'went'],
]
let model = learn(pairs)
/* {
  rules: { k: [ [ 'alk', 'alked' ] ] },
  exceptions: { go: 'went' },
}*/

let out = convert('walk', model)
// 'walked'

you can pass-in options:

let opts={
  threshold:80, //how sloppy our initial rules can be
  min:0, //rule must satisfy # of pairs
  reverse:true, //compute backward transformation, too
}
let model = learn(pairs, opts)

Reverse

the model also works transforming the words the other way:

import { learn, reverse, convert } from 'suffix-thumb'

let pairs = [
  ['walk', 'walked'],
  ['talk', 'talked'],
  ['go', 'went'],
]
let model = learn(pairs)
let rev = reverse(model)
let out = convert('walked', rev)
// 'walk'

by default, the model ensures all two-way transformation - if you only require 1-way, you can do:

learn(pairs, {reverse: false})

you can expect the model to be 5% smaller or so - not much.

Compress

by default, the model is small, but remains human-readable (and human-editable). We can compress it further, turning it into a snowball inscrutible characters:

import { learn, compress, uncompress, convert } from 'suffix-thumb'

let pairs = [
  ['walk', 'walked'],
  ['talk', 'talked'],
  ['go', 'went'],
]
let model = learn(pairs)
// shrink it
model = compress(shrink)
// {rules:'LSKs3H2-LNL.S3DH'}
// pop it back
model = uncompress(model)
let out = convert('walk', model)
// 'walked'

The models must be uncompressed before they are used, or reversed.

Validation

sometimes you can accidentally send in an impossible set of transformations. This library quietly ignores duplicates, by default. You can use {verbose:true} to log warnings about this, or validate your input manually:

import { validate } from 'suffix-thumb'
let pairs = [
  ['left', 'right'],
  ['left', 'right-two'],
  ['ok', 'right'],
]
pairs = validate(pairs) //remove dupes (on both sides)

If you are just doing one-way transformation, and not reverse, you may want to allow duplicates on the right side:

let pairs = [
  ['left', 'right'],
  ['ok', 'right'],
]
let model = learn(pairs, {reverse: false})
let out = convert('ok', model)
// 'right'

How it works

For each word-pair, it generates all n-suffixes of the left-side, and n-suffixes of the right-side.

any good correlations between the two suffix pairs begins to pop out. Exceptions to these rules are remembered. It then exhaustively reduces any redundancies in these rules.

There are some compromises, magic-numbers, and opinionated decisions - in-order to allow productive, but imperfect rules.

  • The library is meant optimize for file-size of the model
  • compression is slow, uncompression is fast
  • it should always return a perfect result

The library drops case-information - and numbers and some characters1 will not compress properly.

There may be wordlists with few helpful patterns. Conjugation datasets in English and French tend to get ~85% filesize compression.

See also

  • efrt - trie-based prefix compression for JSON

MIT

suffix-thumb's People

Contributors

spencermountain avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.