diff --git a/src/a.ts b/src/a.ts index 34772ca..47c5e08 100644 --- a/src/a.ts +++ b/src/a.ts @@ -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" ]; diff --git a/src/an.ts b/src/an.ts index c2627df..cc5524b 100644 --- a/src/an.ts +++ b/src/an.ts @@ -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" ]; diff --git a/src/english-article-classifier.ts b/src/english-article-classifier.ts index 44179f4..7c85c62 100644 --- a/src/english-article-classifier.ts +++ b/src/english-article-classifier.ts @@ -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); @@ -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'" @@ -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'" diff --git a/test/fixtures/A.txt b/test/fixtures/A.txt index 8b3dd53..3f82243 100644 --- a/test/fixtures/A.txt +++ b/test/fixtures/A.txt @@ -127,4 +127,5 @@ Ph.D. X-ray Y-shaped pipe UFO -UNESCO representative \ No newline at end of file +UNESCO representative +# special words \ No newline at end of file diff --git a/test/fixtures/An.txt b/test/fixtures/An.txt index 0a57dde..94bb1b1 100644 --- a/test/fixtures/An.txt +++ b/test/fixtures/An.txt @@ -74,4 +74,8 @@ yblent eye yclad body ypsiliform junction yttrium atom -U-boat \ No newline at end of file +U-boat +# special word +MTV channel +FAQ +SQL \ No newline at end of file diff --git a/test/fixtures/Unknown.txt b/test/fixtures/Unknown.txt index 3d293bc..9622b70 100644 --- a/test/fixtures/Unknown.txt +++ b/test/fixtures/Unknown.txt @@ -9,7 +9,6 @@ D.S.M. DINNER DNR FACT -FAQ GOD GSM phone HAL 9000 @@ -41,7 +40,6 @@ FSM L.E.D. LCD M.I.A. -MTV channel N.C.O. NCO O.K.