|
| 1 | +function metaphone (str) { |
| 2 | + // The code below is based on description from Wikipedia (http://en.wikipedia.org/wiki/Metaphone) |
| 3 | + // There are some modifications applied, like |
| 4 | + // - changing the order of rules |
| 5 | + // - changing the rules to match PHP algorithm |
| 6 | + // modifications are based on PHP metaphone source code |
| 7 | + |
| 8 | + // changing the input string to lower case |
| 9 | + // all rules replace lower-case characters with upper-case, so following rules won't be applied to already computed parts |
| 10 | + str = ('' + str).toLowerCase(); |
| 11 | + |
| 12 | + var rules = [ |
| 13 | + // 1. Drop duplicate adjacent letters, except for G |
| 14 | + // php also doesn't remove 'C' duplicates |
| 15 | + /([^cg])\1+/g, '', |
| 16 | + |
| 17 | + // 2. If the word begins with 'KN', 'GN', 'PN', 'AE', 'WR', drop the first letter |
| 18 | + |
| 19 | + /^[gkp]n/, 'N', |
| 20 | + /^ae/, 'E', |
| 21 | + /^wr/, 'R', |
| 22 | + |
| 23 | + // 3. Drop 'B' if after 'M' and if it is at the end of the word |
| 24 | + // php ignores the "end of word" part of the rule |
| 25 | + /mb/g, '', |
| 26 | + |
| 27 | + // 9. 'CK' transforms to 'K' |
| 28 | + // Applying the rule here, cause rule 4) replaces all 'C's with 'K' |
| 29 | + /ck/g, 'K', |
| 30 | + |
| 31 | + // 4. 'C' transforms to 'X' if followed by 'IA' or 'H' (unless in latter case, it is part of '-SCH-', in which case it transforms to 'K'). 'C' transforms to 'S' if followed by 'I', 'E', or 'Y'. Otherwise, 'C' transforms to 'K' |
| 32 | + /\Bsch/g, 'SK', // check if replace both S and H or only the C. Check if '\B' works correctly for multiple words |
| 33 | + /c(?=ia)/g, 'X', |
| 34 | + /[cs]h/g, 'X', // PHP also drops the 'H' character |
| 35 | + /c(?=[eiy])/g, 'S', |
| 36 | + /c/g, 'K', |
| 37 | + |
| 38 | + // 5. 'DG' transforms to 'J' if followed by 'GE', 'GY', or 'GI'. Otherwise, 'D' transforms to 'T |
| 39 | + /dg(?=[eiy])/g, 'J', |
| 40 | + /d/g, 'T', |
| 41 | + |
| 42 | + // 6. Drop 'G' if followed by 'H' and 'H' is not at the end or before a vowel. Drop 'G' if followed by 'N' or 'NED' and is at the end |
| 43 | + /gh(?![aeiou]|$)/g, 'H', |
| 44 | + /g(?=n(ed)?)$/g, '', |
| 45 | + |
| 46 | + // 7. 'G' transforms to 'J' if before 'I', 'E', or 'Y', and it is not in 'GG'. Otherwise, 'G' transforms to 'K'. Reduce 'GG' to 'G' |
| 47 | + /(^|[^g])g(?=[eiy])/g, '$1J', |
| 48 | + /g+/g, 'K', // check if that's the way GG is replaced with K, or it should be G in result string |
| 49 | + |
| 50 | + // 8. Drop 'H' if after vowel and not before a vowel |
| 51 | + /([aeiou])h(?![aeiou])/g, '$1', |
| 52 | + |
| 53 | + // 10. 'PH' transforms to 'F' |
| 54 | + /ph/g, 'F', |
| 55 | + |
| 56 | + // 11. 'Q' transforms to 'K' |
| 57 | + /q/g, 'K', |
| 58 | + |
| 59 | + // 12. 'S' transforms to 'X' if followed by 'H', 'IO', or 'IA' |
| 60 | + // php also drops the 'H' in 'SH' case, which is done in 'CH' rule |
| 61 | + /s(?=i[oa])/g, 'X', |
| 62 | + |
| 63 | + // 13. 'T' transforms to 'X' if followed by 'IA' or 'IO'. 'T' transforms to '0' if followed by 'H'. Drop 'T' if followed by 'CH' |
| 64 | + // php replaces 'TH' with '0', not only the 'T' |
| 65 | + /t(?=i[ao])/g, 'X', |
| 66 | + /th/g, '0', |
| 67 | + /t(?=ch)/g, '', // check if this is valid, cause 'CH' case is already replaced with 'X' |
| 68 | + |
| 69 | + // 14. 'V' transforms to 'F' |
| 70 | + /v/g, 'F', |
| 71 | + |
| 72 | + // 15. 'WH' transforms to 'W' if at the beginning. Drop 'W' if not followed by a vowel |
| 73 | + // 17. Drop 'Y' if not followed by a vowel |
| 74 | + /^wh/, 'W', |
| 75 | + /[wy](?![aeiou])/g, '', |
| 76 | + |
| 77 | + // 16. 'X' transforms to 'S' if at the beginning. Otherwise, 'X' transforms to 'KS' |
| 78 | + /^x/, 'S', |
| 79 | + /x/g, 'KS', |
| 80 | + |
| 81 | + // 18. 'Z' transforms to 'S' |
| 82 | + /z/g, 'S', |
| 83 | + |
| 84 | + // 19. Drop all vowels unless it is the beginning |
| 85 | + /(^[aeiou])?[aeiou]?/g, '$1', |
| 86 | + |
| 87 | + // Remove all non-ascii characters |
| 88 | + // doing case-insensitive replace, so that upper-case ASCII characters remain untouched |
| 89 | + /[^a-z0]/ig, '' |
| 90 | + ]; |
| 91 | + |
| 92 | + for (var i = 0, l = rules.length; i < l; i += 2) { |
| 93 | + str = str.replace(rules[i], rules[i + 1]); |
| 94 | + } |
| 95 | + |
| 96 | + // transforming result to upper-case |
| 97 | + return str.toUpperCase(); |
| 98 | +} |
| 99 | + |
0 commit comments