Skip to content

Commit

Permalink
impr: added more english punctuation replacements
Browse files Browse the repository at this point in the history
Also refactored the way these are stored - using an object instead of a json file
  • Loading branch information
Miodec committed Nov 1, 2023
1 parent 1c359f5 commit 2c74f75
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 40 deletions.
67 changes: 45 additions & 22 deletions frontend/src/ts/test/english-punctuation.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,59 @@
let pairsList: string[] = [];
import { randomElementFromArray } from "../utils/misc";

export async function getList(): Promise<string[]> {
if (pairsList.length === 0) {
return $.getJSON("languages/english_punctuation.json", function (data) {
pairsList = data;
return pairsList;
});
}
return pairsList;
}
type Pair = [string, string[]];

const pairs: Pair[] = [
["are", ["aren't"]],
["can", ["can't"]],
["could", ["couldn't"]],
["did", ["didn't"]],
["does", ["doesn't"]],
["do", ["don't"]],
["had", ["hadn't"]],
["has", ["hasn't"]],
["have", ["haven't"]],
["is", ["isn't"]],
["it", ["it's", "it'll"]],
["i", ["i'm", "i'll", "i've", "i'd"]],
["you", ["you'll", "you're", "you've", "you'd"]],
["that", ["that's", "that'll", "that'd"]],
["must", ["mustn't", "must've"]],
["there", ["there's", "there'll", "there'd"]],
["he", ["he's", "he'll", "he'd"]],
["she", ["she's", "she'll", "she'd"]],
["we", ["we're", "we'll", "we'd"]],
["they", ["they're", "they'll", "they'd"]],
["should", ["shouldn't", "should've"]],
["was", ["wasn't"]],
["were", ["weren't"]],
["will", ["won't"]],
["would", ["wouldn't", "would've"]],
["going", ["goin'"]],
];

// Check if word is in the group of pairs so it can be replaced
export async function check(word: string): Promise<boolean> {
const list = await getList();
if (
list.find((a) => word.match(RegExp(`^([\\W]*${a[0]}[\\W]*)$`, "gi"))) ===
undefined
pairs.find((pair) =>
word.match(RegExp(`^([\\W]*${pair[0]}[\\W]*)$`, "gi"))
) === undefined
) {
return false;
}
return true;
}

export async function replace(word: string): Promise<string> {
const list = await getList();
const replacement = list.find((a) =>
word.match(RegExp(`^([\\W]*${a[0]}[\\W]*)$`, "gi"))
const replacement = pairs.find((pair) =>
word.match(RegExp(`^([\\W]*${pair[0]}[\\W]*)$`, "gi"))
);

if (replacement === undefined) return word;

const randomReplacement = randomElementFromArray(replacement[1]);

return word.replace(
RegExp(`^(?:([\\W]*)(${replacement[0]})([\\W]*))$`, "gi"),
randomReplacement
);
return replacement
? word.replace(
RegExp(`^(?:([\\W]*)(${replacement[0]})([\\W]*))$`, "gi"),
replacement[1]
)
: word;
}
18 changes: 0 additions & 18 deletions frontend/static/languages/english_punctuation.json

This file was deleted.

0 comments on commit 2c74f75

Please sign in to comment.