-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
impr: added more english punctuation replacements
Also refactored the way these are stored - using an object instead of a json file
- Loading branch information
Showing
2 changed files
with
45 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file was deleted.
Oops, something went wrong.