From 20ec6679a7503181cb1d2e851ed8e85751f59389 Mon Sep 17 00:00:00 2001 From: DenelDuck <43882337+DenelDuck@users.noreply.github.com> Date: Tue, 5 Apr 2022 18:09:03 +0200 Subject: [PATCH] New punctuation functionality allowing words to end with -n't (#2792) from DenelDuck * Initial functionality and dictionary with pairs * functional (not optimal) solution * remove unused import * avoid promise callbacks, remove unnecessary else and use strict equals * camel case --- .../src/scripts/test/english-punctuation.ts | 36 +++++++++++++++++++ frontend/src/scripts/test/test-logic.ts | 17 +++++++-- .../static/languages/english_punctuation.json | 18 ++++++++++ 3 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 frontend/src/scripts/test/english-punctuation.ts create mode 100644 frontend/static/languages/english_punctuation.json diff --git a/frontend/src/scripts/test/english-punctuation.ts b/frontend/src/scripts/test/english-punctuation.ts new file mode 100644 index 000000000..f6bbdd02d --- /dev/null +++ b/frontend/src/scripts/test/english-punctuation.ts @@ -0,0 +1,36 @@ +let pairsList: string[] = []; + +export async function getList(): Promise { + if (pairsList.length === 0) { + return $.getJSON("languages/english_punctuation.json", function (data) { + pairsList = data; + return pairsList; + }); + } + return pairsList; +} + +// Check if word is in the group of pairs so it can be replaced +export async function check(word: string): Promise { + const list = await getList(); + if ( + list.find((a) => word.match(RegExp(`^([\\W]*${a[0]}[\\W]*)$`, "gi"))) === + undefined + ) { + return false; + } + return true; +} + +export async function replace(word: string): Promise { + const list = await getList(); + const replacement = list.find((a) => + word.match(RegExp(`^([\\W]*${a[0]}[\\W]*)$`, "gi")) + ); + return replacement + ? word.replace( + RegExp(`^(?:([\\W]*)(${replacement[0]})([\\W]*))$`, "gi"), + replacement[1] + ) + : word; +} diff --git a/frontend/src/scripts/test/test-logic.ts b/frontend/src/scripts/test/test-logic.ts index 2014b5ae9..e03a0573c 100644 --- a/frontend/src/scripts/test/test-logic.ts +++ b/frontend/src/scripts/test/test-logic.ts @@ -35,6 +35,7 @@ import * as Wordset from "./wordset"; import * as ChallengeContoller from "../controllers/challenge-controller"; import * as QuoteRatePopup from "../popups/quote-rate-popup"; import * as BritishEnglish from "./british-english"; +import * as EnglishPunctuation from "./english-punctuation"; import * as LazyMode from "./lazy-mode"; import * as Result from "./result"; import * as MonkeyPower from "../elements/monkey-power"; @@ -70,12 +71,12 @@ export function setNotSignedInUid(uid: string): void { } let spanishSentenceTracker = ""; -export function punctuateWord( +export async function punctuateWord( previousWord: string, currentWord: string, index: number, maxindex: number -): string { +): Promise { let word = currentWord; const currentLanguage = Config.language.split("_")[0]; @@ -229,11 +230,21 @@ export function punctuateWord( const specials = ["{", "}", "[", "]", "(", ")", ";", "=", "+", "%", "/"]; word = Misc.randomElementFromArray(specials); + } else if ( + Math.random() < 0.5 && + currentLanguage === "english" && + (await EnglishPunctuation.check(word)) + ) { + word = await applyEnglishPunctuationToWord(word); } } return word; } +async function applyEnglishPunctuationToWord(word: string): Promise { + return EnglishPunctuation.replace(word); +} + export function startTest(): boolean { if (PageTransition.get()) { return false; @@ -681,7 +692,7 @@ async function getNextWord( randomWord = applyFunboxesToWord(randomWord, wordset); if (Config.punctuation) { - randomWord = punctuateWord( + randomWord = await punctuateWord( TestWords.words.get(TestWords.words.length - 1), randomWord, TestWords.words.length, diff --git a/frontend/static/languages/english_punctuation.json b/frontend/static/languages/english_punctuation.json new file mode 100644 index 000000000..5a83ebe2c --- /dev/null +++ b/frontend/static/languages/english_punctuation.json @@ -0,0 +1,18 @@ +[ + ["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"], + ["must", "mustn't"], + ["should", "shoudln't"], + ["was", "wasn't"], + ["were", "weren't"], + ["will", "won't"], + ["would", "wouldn't"] +]