Skip to content

Commit

Permalink
Use prefix tree
Browse files Browse the repository at this point in the history
  • Loading branch information
veer66 committed Dec 3, 2018
1 parent 16e9074 commit 033c883
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
40 changes: 17 additions & 23 deletions lib/dict.js
Expand Up @@ -3,7 +3,7 @@ var LEFT = 0;
var RIGHT = 1;
var path = require("path");
var glob = require("glob");

var createPrefixTree = require("./prefixtree").createPrefixTree;
var WordcutDict = {


Expand Down Expand Up @@ -41,6 +41,7 @@ var WordcutDict = {

finalizeDict: function(){
this.dict = this.sortuniq(this.dict);
this.tree = createPrefixTree(this.dict.map(w => [w, null]))
},

addFiles: function(files, finalize){
Expand Down Expand Up @@ -93,38 +94,31 @@ var WordcutDict = {
},

createAcceptor: function () {
let dict = this;
return {
l: 0,
r: this.dict.length - 1,
nodeId: 0,
strOffset: 0,
isFinal: false,
dict: this,
transit: function (ch) {
return this.dict.transit(this, ch);
},
isError: false,
tag: "DICT",
w: 1,
type: "DICT"
type: "DICT",
dict: dict,

transit(ch) {
return this.dict.transit(this, ch)
}
};
},

transit: function (acceptor, ch) {
var l = this.dictSeek(acceptor.l,
acceptor.r,
ch,
acceptor.strOffset,
LEFT);
if (l !== null) {
var r = this.dictSeek(l,
acceptor.r,
ch,
acceptor.strOffset,
RIGHT);
acceptor.l = l;
acceptor.r = r;
acceptor.strOffset++;
acceptor.isFinal = this.isFinal(acceptor);
let child = this.tree.lookup(acceptor.nodeId, acceptor.strOffset, ch)
if (child !== null) {

let [nodeId, isFinal, payload] = child
acceptor.nodeId = nodeId
acceptor.strOffset++
acceptor.isFinal = isFinal
} else {
acceptor.isError = true;
}
Expand Down
1 change: 0 additions & 1 deletion test/tests.js
Expand Up @@ -133,5 +133,4 @@ describe("Wordcut", function() {
expect(segmentedResult).to.deep.equal(["ฉัน","ชอบ","กิน","ข้าว"])
});


});

0 comments on commit 033c883

Please sign in to comment.