From e1e0af31c544f93cd9591ca53ec8738f63a07019 Mon Sep 17 00:00:00 2001 From: John Foerch Date: Fri, 13 Nov 2009 21:43:13 -0500 Subject: [PATCH] casual_spelling_hints_text_match: support multiple translations A single unicode character can now have multiple translations associated with it. This will be necessary for making a pinyin-based module for pattern-matching Chinese text, because some characters have multiple pronunciations. Also fix an error whereby a unicode ligature character appearing in the pattern could cause faulty math. --- modules/casual-spelling.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/modules/casual-spelling.js b/modules/casual-spelling.js index 4fe027e..063e0fa 100644 --- a/modules/casual-spelling.js +++ b/modules/casual-spelling.js @@ -63,16 +63,17 @@ function casual_spelling_translate (chr) { function casual_spelling_hints_text_match (text, pattern) { if (pattern == "") return [0, 0]; - var decoded = Array.map(text, casual_spelling_translate); - for (var i = 0, tlen = text.length; i < tlen; i++) { - for (var e = 0, j = 0, plen = pattern.length; - j < plen && i + e < tlen; - (j += decoded[i+e].length) && e++) - { - if (pattern[j] != text[i+e] && - pattern.substring(j, j+decoded[i+e].length) != decoded[i+e]) + var tlen = text.length; + var plen = pattern.length; + var decoded = Array.map( + text, function (x) Array.concat(x, casual_spelling_translate(x))); + var matched, mlen; + for (var i = 0; i < tlen; i++) { + for (var e = 0, j = 0; j < plen && i + e < tlen; e++) { + if (! decoded[i+e].some(function (x) (pattern.substring(j, j+(mlen = x.length)) == (matched = x)))) break; - if (pattern.substring(j) == decoded[i+e]) + j += mlen; + if (j == plen) return [i, i+e+1]; } } -- 2.11.4.GIT