Debian Codegraph @

terps/tads/tads3/test/data/strcomp2.t

raw
1 #include <tads.h>
2 #include <strcomp.h>
3 #include <dict.h>
4
5 dictionary cmdDict;
6
7 main(args)
8 {
9 "Hello, world!\n";
10 cmdDict.addWord(testObj, 'test', &noun);
11 cmdDict.addWord(initObj, 'init', &adjective);
12
13 foreach (local w in ['washington', 'lee', 'gutierrez',
14 'pfister', 'jackson', 'tymczak',
15 'ashcraft', 'robert', 'rupert', 'rubin',
16 'test', 'init', 'taste', 'isn\'t', 'innuit', 'intuit',
17 'track', 'too'])
18 {
19 local m = cmdDict.findWord(w);
20 "<<w>>: soundex=<<SoundexStringComparator.canonicalize(w)>>,
21 <<m.length() != 0 ? 'found ' + m[1].name : 'not found'>>\n";
22 }
23 }
24
25 testObj: object
26 name = 'test'
27 ;
28 initObj: object
29 name = 'init'
30 ;
31
32 test: InitObject
33 execute() {
34 // This works when we use StringComparator
35 // instead of SoundexStringComparator here:
36 local cmpObj =
37 new SoundexStringComparator(0, nil, [['a','a',0,0]]);
38 cmdDict.setComparator(cmpObj);
39 "...done with init...\n";
40 }
41 ;
42
43 class SoundexStringComparator: object
44 cmp = nil
45 construct(truncLen, caseSensitive, mappings)
46 {
47 cmp = new StringComparator(truncLen, caseSensitive, mappings);
48 }
49 calcHash(str)
50 {
51 ". calcHash(<<str>>)...\n";
52 return cmp.calcHash(canonicalize(str));
53 }
54 matchValues(inputStr, dictStr)
55 {
56 ". matchValues(<<inputStr>>, <<dictStr>>)...\n";
57 return cmp.matchValues(canonicalize(inputStr), canonicalize(dictStr));
58 }
59 soundexTab = ['b'->'1', 'f'->'1', 'p'->'1', 'v'->'1',
60 'c'->'2', 'g'->'2', 'j'->'2', 'k'->'2',
61 'q'->'2', 's'->'2', 'x'->'2', 'z'->'2',
62 'd'->'3', 't'->'3',
63 'l'->'4',
64 'm'->'5', 'n'->'5',
65 'r'->'6']
66 canonicalize(str)
67 {
68 str = rexReplace('[bfpv][wh]+[bfpv]+'
69 + '|[cgjkqsxz][wh]+[cgjkqsxz]+'
70 + '|[dt][wh]+[dt]+'
71 + '|l[wh]+l+'
72 + '|[mn][wh]+[mn]+'
73 + '|r[wh]+r+', str,
74 {m, i, o: m.substr(1, 2)}, ReplaceAll);
75 local first = str.substr(1, 1);
76 str = rexReplace('[bfpvcgjkqsxzdtlmnr]', str,
77 {m, i, o: SoundexStringComparator.soundexTab[m]},
78 ReplaceAll);
79 str = rexReplace('(<digit>)%1+', str, '%1', ReplaceAll);
80 str = str.splice(1, 1, first);
81 str = rexReplace('<^digit>', str, '', ReplaceAll);
82 str = first + str;
83 if (str.length() < 4)
84 str += '000'.substr(str.length());
85 else if (str.length() > 4)
86 str = str.substr(1, 4);
87
88 return str.toUpper();
89 }
90 ;
91