Skip to content

Commit

Permalink
Make swapping letter cheaper.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason-Rev committed Jun 18, 2016
1 parent 923683b commit 7faa4e0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
8 changes: 6 additions & 2 deletions server/src/suggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export interface Trie {
}

const baseCost = 100;
const swapCost = 75;
const postSwapCost = swapCost - baseCost;

export interface SuggestionResult {
word: string;
Expand Down Expand Up @@ -104,8 +106,10 @@ export function suggestA(
let min = matrix[d][0];
for (let i = 1; i <= mx; ++i) {
const curLetter = x[i];
const subCost = (w === curLetter || (curLetter === lastSugLetter && w === lastLetter))
? 0 : baseCost;
const subCost =
(w === curLetter)
? 0 : ((curLetter === lastSugLetter && w === lastLetter)
? postSwapCost : baseCost);
matrix[d][i] = Math.min(
matrix[d - 1][i - 1] + subCost, // substitute
matrix[d - 1][i] + baseCost, // insert
Expand Down
20 changes: 20 additions & 0 deletions server/test/suggest_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,19 @@ describe('test suggestions', () => {
});
});

describe('matching hte', () => {
const words = [
'ate', 'hoe', 'hot', 'the', 'how', 'toe'
];

const trie = wordListToTrie(words);

it('checks best match', () => {
const results = suggest(trie, 'hte');
console.log(JSON.stringify(results, null, 4));
});
});

describe('test for duplicate suggestions', () => {
const words = [
'apple', 'ape', 'able', 'apples', 'banana', 'orange', 'pear', 'aim', 'approach', 'bear'
Expand Down Expand Up @@ -146,6 +159,13 @@ describe('test suggestions for large vocab', function() {
console.log(suggestions);
});
});

it('checks best match for "hte"', () => {
return pTrie.then(trie => {
const results = suggest(trie, 'hte');
console.log(JSON.stringify(results, null, 4));
});
});
});

/* */

0 comments on commit 7faa4e0

Please sign in to comment.