Faster array approach:
(
~makeDict = { |stringPairs|
var dict = Array.newClear(27);
stringPairs.pairsDo { |str, hyphenation| ~putOneString.(dict, str, hyphenation) };
dict
};
~putOneString = { |dict, str, hyphenation|
var level = dict, sublevel;
str.do { |char|
char = char.toLower;
sublevel = level[char.ascii - 97];
if(sublevel.isNil) {
sublevel = Array.newClear(27);
level.put(char.ascii - 97, sublevel);
};
level = sublevel;
};
// level[\wholeString] = str;
level[26] = hyphenation;
};
~matchString = { |dict, str|
var level = dict;
var stream = CollStream(str);
var char;
while {
char = stream.next;
if(char.notNil) {
level = level[char.ascii - 97];
};
char.notNil and: { level.notNil }
};
// and here, retrieve hyphenation
level.tryPerform(\at, 26)
};
)
d = ~makeDict.([
"hello", "hel-lo",
"there", "there",
"thereupon", "there-up-on"
]);
d
looks really funny!
> [ nil, nil, nil, nil, nil, nil, nil, [ nil, nil, nil, nil, [ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, [ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, [ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, [ nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, hel-lo ], nil, nil, nil, nil, ... ]
But the ~matchString calls in my earlier post all still work this way.
Edit: This way might be a little trickier with accented characters though – it assumes 26 letters only. Accents would need to map onto indices… “exercise for the reader.”
hjh