Skip to content

Commit

Permalink
chore: Update special word
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Oct 29, 2017
1 parent 6442535 commit fe4337b
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 8 deletions.
17 changes: 16 additions & 1 deletion src/a.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,20 @@ export const APattern = [
/^e[uw]/i,
/^onc?e\b/i,
/^uni([^nmd]|mo)/i,
/^u[bcfhjkqrst][aeiou]/i
/^u[bcfhjkqrst][aeiou]/i,
"MBA",
"MA",
"MRI",
"MS",
"MTV",
"SGML",
"SOS",
"SMS",
"XML",
"SUV",
"STD",
"SPF",
"HB",
"RAF",
"IOU"
];
20 changes: 19 additions & 1 deletion src/an.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,23 @@ export const AnPatterns = [
/^honest/i,
/^hono/i,
/^8$/,
/^11$/
/^11$/,
// special
"FAQ",
"MBA",
"MA",
"MRI",
"MS",
"MTV",
"SGML",
"SOS",
"SQL",
"SMS",
"XML",
"SUV",
"STD",
"SPF",
"HB",
"RAF",
"IOU"
];
12 changes: 10 additions & 2 deletions src/english-article-classifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ export interface classifyArticleOptions {
forceAn?: string[];
}

const testWord = (text: string, pattern: string | RegExp): boolean => {
if (typeof pattern === "string") {
return text === pattern;
} else {
return pattern.test(text);
}
};

export function classifyArticle(phrase: string, options?: classifyArticleOptions): ReturnClassifyArticle {
// Getting the first word
const match = /[\w.-]+/.exec(phrase);
Expand Down Expand Up @@ -52,7 +60,7 @@ export function classifyArticle(phrase: string, options?: classifyArticleOptions
const specialAnCaseWords = AnPatterns;
for (let i = 0; i < specialAnCaseWords.length; i++) {
const specialAnCaseWordPattern = specialAnCaseWords[i];
if (specialAnCaseWordPattern.test(word)) {
if (testWord(word, specialAnCaseWordPattern)) {
return {
type: "an",
reason: "Specific start of words that should be proceeded by 'an'"
Expand All @@ -78,7 +86,7 @@ export function classifyArticle(phrase: string, options?: classifyArticleOptions
const specialACasePattern = APattern;
for (let i = 0; i < specialACasePattern.length; i++) {
const specialACaseWordPattern = specialACasePattern[i];
if (specialACaseWordPattern.test(word)) {
if (testWord(word, specialACaseWordPattern)) {
return {
type: "a",
reason: "Special cases where a word that begins with a vowel should be proceeded by 'a'"
Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/A.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,5 @@ Ph.D.
X-ray
Y-shaped pipe
UFO
UNESCO representative
UNESCO representative
# special words
6 changes: 5 additions & 1 deletion test/fixtures/An.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,4 +74,8 @@ yblent eye
yclad body
ypsiliform junction
yttrium atom
U-boat
U-boat
# special word
MTV channel
FAQ
SQL
2 changes: 0 additions & 2 deletions test/fixtures/Unknown.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ D.S.M.
DINNER
DNR
FACT
FAQ
GOD
GSM phone
HAL 9000
Expand Down Expand Up @@ -41,7 +40,6 @@ FSM
L.E.D.
LCD
M.I.A.
MTV channel
N.C.O.
NCO
O.K.
Expand Down

0 comments on commit fe4337b

Please sign in to comment.