Giter VIP home page Giter VIP logo

dawg's Introduction

A Directed Acyclic Word Graph implementation in TypeScript/JavaScript

Codeship Status for mckoss/dawg

This library takes a dictionary of (ascii) words as input, and generates a compressed datastructure based on a DAWG (like a Trie, but whose representation shares common suffixes as well as common prefixes).

Inspired by several blog posts by John Resig:

Ported from my 2011 experiment: lookups

You can try out (a previously) hosted version of this software at:

Usage

There are two classes exposed by this library:

  • Trie: This class takes a dictionary of words and can output a packed prepresentation of it.
  • PTrie: This class can read in a packed representation, and determine if a word is a member.

To get started:

$ npm install --save dawg-lookup

Creating a Packed Representation of a Dictionary

var Trie = require('dawg-lookup').Trie

var trie = new Trie("the rain in spain falls mainly in the plain " +
                    "main rains fall plainly " +
                    "peter piper picked a peck of pickled peppers " +
                    "pipers pickle pepper");
var packed = trie.pack();

// This packed representation would usually be stored or embedded
// in your program, for use later.
console.log(packed.split(';').join('\n'));
/*
a,fall8in,m6of,p0rain8spain,the
e3i0l5
ck0p3
ed,le0
!d
ck,pp0ter
er2
ain0
!ly
!s
*/

Using a Packed Dictionary to test for Membership

// This dependency will not load the Trie class, which is only needed
// for packing a dictionary, not interpreting it.
var PTrie = require('dawg-lookup/lib/ptrie').PTrie;

// Using 'packed' string from above.
var ptrie = new PTrie(packed);

console.log(ptrie.isWord('picked')); // true
console.log(ptrie.isWord('foobar')); // false
console.log(ptrie.isWord('ain'));    // false

console.log(ptrie.completions("pi"));
// [ 'picked', 'pickle', 'pickled', 'piper', 'pipers' ]

Packed Trie Encoding Format

A Packed Trie is an encoding of a textual Trie using 7-bit ascii. None of the characters need be quoted themselves when placed inside a JavaScript string, so dictionaries can be easily included in JavaScript source files or read via ajax.

Example

Suppose our dictionary contains the words:

cat cats dog dogs bat bats rat rats

The corresponding Packed Trie string is:

b0c0dog1r0
at0
!s

Visually, this looks like:

![DAWG diagram](https://g.gravizo.com/g? digraph DAWG { aize = "4, 4"; 0 [label="start"] 1 [label=""] 2 [label="bat, cat, rat, dog"] 3 [label="bats, cats, rats, dogs"] 0 -> 1 [label="b"] 0 -> 1 [label="c"] 0 -> 2 [label="dog"] 0 -> 1 [label="r"] 1 -> 2 [label="at"] 2 -> 3 [label="s"] } )

This Trie (actually, a DAWG) has 3 nodes. If we follow the path of "cats" through the Trie we get the squence:

node 0. match 'c': continue at node + 1
node 1. match 'at': continue at node + 1
node 2. match s: Found!

Or 'dog':

node 0. match 'dog': continue at node + 2
node 2. nothing left to match - '!' indicates Found!

While there are conceptually 4 nodes in this DAWG, we overload the terminal 's' in the 3rd node.

Nodes

A file consists of a sequence of nodes, which are nodes in a Trie representing a dictionary. Nodes are separated by ';' characters (you can split(';') to get an array of node strings).

A node string contains an optional '!' first character, which indicates that this node is a terminal (matching) node in the Trie if there are zero characters left in the pattern.

The rest of the node is a sequence of character strings. Each string is either associated with a node reference, or is a terminal string completing a match. Node references are base 36.1 encoded relative node numbers ('0' == +1, '1' == +2, ...). A comma follows each terminal string to separate it from the next string in the sequence.

A Node reference can also be a symbol - an absolute node reference, instead of a relative one.

Symbols

Large dictionaries can be further compressed by recognizing that node references to some common suffixes can be quite large (i.e., spanning 1,000's of nodes). While encoded as only 3 or 4 characters, we can reduce the file size by replacing selected row references with symbolic references.

To do so, we prepend the file with a collection of symbol definitions:

0:B9M
1:B9O
2:B6R
3:B6B
...
aA5Kb971c82Ud7FFe6Y5f6E5g5Y7h5IDi58Tj53Xk4XOl4J0m3WMn3N0o38Sp2E3q2BZr1QIs0JFtXHuLPvE2w4Kx41y24zS

When used in a Node, a symbol reference indicates the absolute row number as defined in it's symbol definition line (above).

For each symbol we define (up to 36), we shift the meaning of all relative references down by 1. E.g.,if we define 1 symbol ('0'), then the node reference 1 now means "+1 row", whereas it normally means "+2 rows".

Base 36.1 numbers

Unlike base 36 numbers (digits 0-9, A-Z), base "36.1" distinguished between leading zeros. The counting numbers are hence:

0, 1, 2, 3, ..., 9, A, B, C, ..., Y, Z, 00, 01, 02, ... AA, ...

so we eke out a bit more space by not ignoring leading zeros.

Building this Repo

$ source tools/use
$ configure-project
$ run-tests

dawg's People

Contributors

mckoss avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

dawg's Issues

Node string fragment conflicts with internal fields

The way Node stores string fragments alongside internal fields is problematic:

new Trie(['child']).optimize()
Uncaught TypeError: this.child is not a function
    at Node.isTerminalString (node.js:51)
    at Node.isTerminal (node.js:56)
    at Trie.collapseChains (trie.js:207)
    at Trie.optimize (trie.js:126)
    at <anonymous>:1:26

"child" is a property holding a Node reference, not the child method from the prototype.

This could possibly be worked around by usage of Symbols to insure internal fields/method never conflict with string fragment properties.

Words with underscores are not found

Words with underscores are reported as not present in a word collection with isWord

const trie = new Trie(['yes', 'yes_no']);
trie.isWord('yes'); // => true (which is correct)
trie.isWord('no'); // => false (which is correct)
trie.isWord('yes_no'); // => false (which is incorrect)

I guess this is because Node has special properties like _c that use _ and hence any prop starting with _ is excluded in node.props() https://github.com/mckoss/dawg/blob/master/src/node.ts#L88.

I guess a safer option would be to store props in their own dictionary like node.props. Otherwise the only option I can think of is to replace _ prior to creating the Trie and before isWord but that seems less ideal.

PTrie constructed from packed string does not yield same isWord result as original Trie

I constructed a Trie from a Scrabble dictionary which is then packed and used to construct a PTrie.
The PTrie itself appears to be populated, though methods such as isWord and completions are not returning expected results.

Example:

import { createReadStream, createWriteStream } from 'fs';
import { resolve } from 'path';
import { createInterface, Interface } from 'readline';

var Trie = require('dawg-lookup').Trie;
var PTrie = require('dawg-lookup/lib/ptrie').PTrie;

var trie = new Trie();
var scrabbleDictionary = createInterface({
   input: createReadStream(resolve(__dirname, '../lib', 'Collins Scrabble Words (2019).txt'), 'utf8')
});
var output = createWriteStream(resolve(__dirname, '../lib', 'dictionary.txt'));

scrabbleDictionary.on('line', (line: string) => {
        trie.insert(line);
    }).on('close', () => {
        console.log(trie.isWord('FUN'));

        let packed = trie.pack();
        let ptrie = new PTrie(packed);

        console.log(ptrie.isWord('FUN'));
        console.log(ptrie.completions('F'));

        output.write(packed);
        output.close();
    });

Result:

> true
> false
> []

Any ideas why this is occurring?

How does the PTrie format work for compressing the Trie?

Wondering how you arrived at this compression format? Is it based on a research paper or anything, or how much does it compress the basic JSON structure of the Trie class?

How about adding support for arbitrary other scripts, like Chinese text (70,000+ possible characters used by words), or Amharic (~200 or so characters), or other scripts with fewer characters than that. Would it still work out of the box or would the PTrie compression format/algorithm need to be modified?

When you say "Large dictionaries can be further compressed", does that feature come included in the library, or what do you need to do to make that happen?

Also a stretch question, wondering if you could explain more the compression format, for a complex/larger example. I still am not grasping it fully after reading the readme, I think I will have to play around with some data to test it out and learn from experience perhaps, not sure.

Thanks for your time in advance.

P.S. The PTrie algorithm seems so clever, that optimization seems necessary way further down the road from where I'm at in my journey, thanks so much for sharing!

Thoughts on how to architect a DAWG-like system for Turkish (an agglutinative language with possibly infinite number of words)?

You're probably familiar with agglutinative languages, like Turkish, which can have "sentence words", basically an infinite number of combination of base + n suffixes. Navajo is another example language, or Finnish, Inuktitut, etc.. Turkish words can be at least up to 70 characters, but you never probably want to serialize this word into a trie/dictionary because there would be way too many trie nodes I would imagine.

Sorry to bother you, but you seem like you are a master at this so I thought I'd ask you directly :). How would you architect such a DAWG-like system for Turkish, assuming let's say 10,000 base words, and 1000 suffixes, which can be concatenated infinitely.

Seems like you would not have to serialize/realize the actual TrieNodes, but make it virtualized somehow, and I'm having a tough time imagining how that might work/look. Maybe you have some thoughts or have done this sort of thing before. But it's like, all 11000 fragments (words + suffixes) would be in the central trie. The words would then have as leaves a marker that it was a word itself, plus link to the suffix nodes, so forming a loop. The suffixes would link to suffixes as well. How would that look? Is that something possible even, given the added thing of the "loop"? If it's possible, it doesn't seem straightforward to serialize anymore because of the loops, is it still possible to pack and unpack?

Thanks in advance for the help.

Best,
Lance

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.