improve wee-bit-fuzzy

This commit is contained in:
lingdocs 2022-08-17 14:29:32 +04:30
parent c898dd6950
commit cd628f28ab
2 changed files with 24 additions and 2 deletions

View File

@ -4,9 +4,11 @@ const pMatches = [
["پیټی", "پېټی"],
["دوستی", "دوستي"],
["پته", "پټه"],
["تخلیه", "تحلیه"],
];
const fMatches = [
["tahliya", "takhliya"],
["sareyy", "saRey"],
["peyTey", "peTey"],
];
@ -17,12 +19,22 @@ pMatches.forEach((pair) => {
const result = pair[1].match(new RegExp(re, "i"));
expect(result).toBeTruthy();
});
test(`${pair[1]} should match ${pair[0]}`, () => {
const re = makeAWeeBitFuzzy(pair[1], "p");
const result = pair[0].match(new RegExp(re, "i"));
expect(result).toBeTruthy();
});
});
fMatches.forEach((pair) => {
test(`${pair[0]} should match ${pair[1]}`, () => {
test(`${pair[0]} should match ${pair[1]} both ways`, () => {
const re = makeAWeeBitFuzzy(pair[0], "f");
const result = pair[1].match(new RegExp(re, "i"));
expect(result).toBeTruthy();
});
test(`${pair[1]} should match ${pair[0]} both ways`, () => {
const re = makeAWeeBitFuzzy(pair[1], "f");
const result = pair[0].match(new RegExp(re, "i"));
expect(result).toBeTruthy();
});
});

View File

@ -35,6 +35,7 @@ const tSounds = "[ت|ط|ټ]";
const dSounds = "[د|ډ]";
const rSounds = "[ر|ړ|ڼ]";
const nSounds = "[ن|ڼ]";
const hKhSounds = "[خ|ح|ښ|ه]";
const pReplacer = {
"ی": fiveYeys,
@ -67,9 +68,15 @@ const pReplacer = {
"ن": nSounds,
"ڼ": nSounds,
"خ": hKhSounds,
"ح": hKhSounds,
"ښ": hKhSounds,
"ه": hKhSounds,
};
const fiveYeysF = "(?:eyy|ey|ee|e|uy)";
const hKhF = "(?:kh|h|x)";
const zSoundsF = "(?:z|dz)";
const fReplacer = {
@ -81,11 +88,14 @@ const fReplacer = {
"z": zSoundsF,
"dz": zSoundsF,
"x": hKhF,
"h": hKhF,
"kh": hKhF,
};
const pRepRegex = new RegExp(Object.keys(pReplacer).join("|"), "g");
const fRepRegex = /eyy|ey|uy|ee|e|z|dz/g;
const fRepRegex = /eyy|ey|uy|ee|e|z|dz|x|kh|h/g;
function makePAWeeBitFuzzy(s: string): string {
// + s.replace(/ /g, "").split("").join(" *");