-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
distanceAStarWeighted.test.ts
42 lines (40 loc) · 2.87 KB
/
distanceAStarWeighted.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { distanceAStarWeighted } from './distanceAStarWeighted';
import { levenshteinDistance } from './levenshtein';
import { buildWeightedMapTrie } from './weightedMaps';
describe('distanceAStar', () => {
test.each`
wordA | wordB
${''} | ${''}
${'apple'} | ${'apple'}
${'apple'} | ${''}
${'apple'} | ${'apples'}
${'apple'} | ${'maple'}
${'grapple'} | ${'maples'}
`('distanceAStar vs Levenshtein "$wordA" "$wordB"', ({ wordA, wordB }) => {
const expected = levenshteinDistance(wordA, wordB) * 100;
expect(distanceAStarWeighted(wordA, wordB, {})).toBe(expected);
expect(distanceAStarWeighted(wordB, wordA, {})).toBe(expected);
});
// cspell:ignore aeiou
test.each`
wordA | wordB | map | expected
${''} | ${''} | ${undefined} | ${0}
${'apple'} | ${'apple'} | ${{ map: 'ae', insDel: 75 }} | ${0}
${'apple'} | ${''} | ${{ map: 'ae', insDel: 75 }} | ${450}
${'apple'} | ${''} | ${{ map: 'ae|(ap)', insDel: 75 }} | ${350}
${'apple'} | ${''} | ${{ map: '(ap)', insDel: 1 }} | ${301}
${'apple'} | ${'apples'} | ${{ map: '(les)(le)', replace: 50 }} | ${50}
${'apple'} | ${'maple'} | ${{ map: '(pp)p', replace: 50 }} | ${150}
${'grapple'} | ${'maples'} | ${{ map: '(pp)p', replace: 50 }} | ${350}
${'bite'} | ${'bate'} | ${{ map: 'aei', replace: 25 }} | ${25}
${'receive'} | ${'recieve' /* cspell:ignore recieve */} | ${{ map: 'ei', swap: 25 }} | ${25}
${'airplane'} | ${'aeroplane'} | ${{ map: '(ai)(ae)', replace: 25 }} | ${125}
${'airplane'} | ${'aeroplane'} | ${{ map: '(air)(aero)|aeiou', replace: 25 }} | ${25}
${'airplane'} | ${'aeroplane'} | ${{ map: 'aeiou', replace: 25 }} | ${125}
${'plain'} | ${'plane'} | ${{ map: '(ane)(ain)', replace: 100 }} | ${100}
`('distanceAStar vs Levenshtein "$wordA" "$wordB" $map', ({ wordA, wordB, map, expected }) => {
const trie = map ? buildWeightedMapTrie([map]) : buildWeightedMapTrie([]);
expect(distanceAStarWeighted(wordA, wordB, trie)).toBe(expected);
expect(distanceAStarWeighted(wordB, wordA, trie)).toBe(expected);
});
});