Skip to content

Commit

Permalink
fix(test): fix search failed miss-fix bug (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
terasum authored Jun 23, 2021
1 parent f44d84f commit dd0356c
Show file tree
Hide file tree
Showing 21 changed files with 4,445 additions and 1,732 deletions.
14 changes: 3 additions & 11 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
{
"presets": [
["es2015"]
],
"plugins": [
"transform-strict-mode",
"transform-es2015-modules-commonjs",
"transform-es2015-spread",
"transform-es2015-destructuring",
"transform-es2015-parameters"
]
}
"presets": ["@babel/preset-env"],
"plugins": ["@babel/plugin-transform-arrow-functions"]
}
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ npm-debug.log
.DS_Store
build
.vscode
/.nyc_output
/coverage
/coverage/*
yarn-error.log
1 change: 0 additions & 1 deletion .nyc_output/a8633781-2c7a-4cb7-88b8-2481b8233fc4.json

This file was deleted.

1 change: 0 additions & 1 deletion .nyc_output/dc66a456-706e-4903-bd1f-bd389a74adbb.json

This file was deleted.

6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ console.log(mdict.lookup('hello'));
*/
console.log(mdict.prefix('hello'));
/*
[ { roffset: 64744840, key: 'he' },
{ roffset: 65513175, key: 'hell' },
{ roffset: 65552694, key: 'hello' } ]
[ { rofset: 64744840, key: 'he' },
{ rofset: 65513175, key: 'hell' },
{ rofset: 65552694, key: 'hello' } ]
*/

let word = 'informations';
Expand Down
72 changes: 0 additions & 72 deletions karma.conf.js

This file was deleted.

75 changes: 44 additions & 31 deletions lib/bktree.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,43 @@
'use strict';
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
exports["default"] = void 0;

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }

function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }

function triple_min(a, b, c) {
var temp = a < b ? a : b;
return temp < c ? temp : c;
}

// Damerau–Levenshtein distance implemention
} // Damerau–Levenshtein distance implemention
// ref: https://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance


function edit_distance(a, b) {
// create a 2 dimensions array
var m = a.length;
var n = b.length;
var dp = new Array(m + 1);

for (var i = 0; i <= m; i++) {
dp[i] = new Array(n + 1);
}
} // init dp array


// init dp array
for (var _i = 0; _i <= m; _i++) {
dp[_i][0] = _i;
}

for (var j = 0; j <= n; j++) {
dp[0][j] = j;
}
} // dynamic approach


// dynamic approach
for (var _i2 = 1; _i2 <= m; _i2++) {
for (var _j = 1; _j <= n; _j++) {
if (a[_i2 - 1] !== b[_j - 1]) {
Expand All @@ -45,21 +50,21 @@ function edit_distance(a, b) {
}
}
}
return dp[m][n];
}

// console.log(edit_distance("halleaa", "hello"));

return dp[m][n];
} // console.log(edit_distance("halleaa", "hello"));
// ----------------------------
// BK-tree Node definition
// ----------------------------

// Maxium word length


var LEN = 100;

function BKNode(w) {
this.word = w;
this.next = new Array(2 * LEN);

for (var i = 0; i < 2 * LEN; i++) {
this.next[i] = -1;
}
Expand All @@ -69,92 +74,99 @@ BKNode.prototype.set_word = function set_word(w) {
this.word = w;
};

var BKTree = function () {
var BKTree = /*#__PURE__*/function () {
function BKTree(word_num) {
_classCallCheck(this, BKTree);

this.tree = new Array(word_num);

for (var i = 0; i < this.tree.length; i++) {
this.tree[i] = new BKNode('');
}

this.rt = new BKNode('');
this.ptr = 0;
}

_createClass(BKTree, [{
key: '_add',
key: "_add",
value: function _add(idx, curr) {
if (this.rt.word === '') {
this.rt.set_word(curr.word);
this.tree[0] = this.rt;
return;
}
var dist = edit_distance(this.rt.word, curr.word);
// console.log(this.rt.word, idx, dist);

var dist = edit_distance(this.rt.word, curr.word); // console.log(this.rt.word, idx, dist);
// throw Error("stop");

if (this.tree[idx].next[dist] === -1) {
/* if no Node exists at this dist from root
* make it child of root Node */

// incrementing the pointer for curr Node
this.ptr++;
this.tree[this.ptr].set_word(curr.word); // curr as child of root Node

this.tree[this.ptr].set_word(curr.word);

// curr as child of root Node
this.tree[idx].next[dist] = this.ptr;
} else {
// recursively find the parent for curr Node
this._add(this.tree[idx].next[dist], curr);
}
}
}, {
key: '_sim_words',
key: "_sim_words",
value: function _sim_words(idx, word, TOL) {
var ret = [];

if (idx === -1) {
return ret;
}

if (this.rt.word === '') {
return ret;
}
var cur_rt = this.tree[idx];

// calculating editdistance of s from root
var dist = edit_distance(word, cur_rt.word);
// if dist is less than tolerance value
var cur_rt = this.tree[idx]; // calculating editdistance of s from root

var dist = edit_distance(word, cur_rt.word); // if dist is less than tolerance value
// add it to similar words

if (dist <= TOL) {
ret.push(cur_rt.word);
}

var start = dist - TOL;

if (start < 0) {
start = 1;
}

var end = dist + TOL;

while (start < end) {
var temp = this._sim_words(cur_rt.next[start], word, TOL);

ret = ret.concat(temp);
start++;
}

return ret;
}
}, {
key: 'add',
key: "add",
value: function add(words) {
var _this = this;

if (!Array.isArray(words)) {
throw new Error('words is not array');
}

words.forEach(function (element) {
_this._add(0, new BKNode(element));
});
}
}, {
key: 'simWords',
key: "simWords",
value: function simWords(src, TOL) {
return this._sim_words(0, src, TOL);
}
Expand All @@ -163,4 +175,5 @@ var BKTree = function () {
return BKTree;
}();

exports.default = BKTree;
var _default = BKTree;
exports["default"] = _default;
Loading

0 comments on commit dd0356c

Please sign in to comment.