mckoss / dawg

Directed Acyclic Word Graph
MIT License
41 stars 2 forks source link

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

Closed ghost closed 4 years ago

ghost commented 4 years ago

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?

ghost commented 4 years ago

After doing some additional debugging, it seems like the PTrie doesn't support checking uppercase words. I was able to get around this by calling toLowerCase on the words I added to the Trie, thereby making trie.isWord('fun') === ptrie.isWord('fun')