diff options
| author | Ben Culkin <scorpress@gmail.com> | 2018-06-07 15:44:16 -0400 |
|---|---|---|
| committer | Ben Culkin <scorpress@gmail.com> | 2018-06-07 15:44:16 -0400 |
| commit | fe80a37fbe5b8a996a949f6a2e9d24d0ee22d892 (patch) | |
| tree | 40dc1dab06748868923d0419d1204409a1b55ee1 | |
| parent | 7f16ae0286ab7492eee9f4019d976bc5ca95d556 (diff) | |
Add snippet
| -rw-r--r-- | indefinite.txt | 80 |
1 files changed, 79 insertions, 1 deletions
diff --git a/indefinite.txt b/indefinite.txt index dcf2997..0e94608 100644 --- a/indefinite.txt +++ b/indefinite.txt @@ -72,4 +72,82 @@ sub select_indefinite_article { return "a"; } - +public static String get(String phrase) { + Pattern pattern; + Matcher matcher; + String word, lowercaseWord; + + if (phrase.length() == 0) { + return "a"; + } + + // Getting the first word + pattern = Pattern.compile("(\\w+)\\s*.*"); + matcher = pattern.matcher(phrase); + if(matcher.matches() == true) { + word = matcher.group(1); + } else { + return "an"; + } + + lowercaseWord = word.toLowerCase(); + + // Specific start of words that should be preceded by 'an' + String [] altCases = { "euler", "heir", "honest", "hono" }; + for (String altCase : altCases) { + if (lowercaseWord.startsWith(altCase) == true) { + return "an"; + } + } + + if (lowercaseWord.startsWith("hour") == true && lowercaseWord.startsWith("houri") == false) { + return "an"; + } + + + // Single letter word which should be preceded by 'an' + if (lowercaseWord.length() == 1) { + if ("aedhilmnorsx".indexOf(lowercaseWord) >= 0) { + return "an"; + } else { + return "a"; + } + } + + // Capital words which should likely be preceded by 'an' + if (word.matches("(?!FJO|[HLMNS]Y.|RY[EO]|SQU|(F[LR]?|[HL]|MN?|N|RH?|S[CHKLMNPTVW]?|X(YL)?)[AEIOU])[FHLMNRSX][A-Z]")) { + return "an"; + } + + // Special cases where a word that begins with a vowel should be preceded by 'a' + String [] regexes = { "^e[uw]", "^onc?e\\b", "^uni([^nmd]|mo)", "^u[bcfhjkqrst][aeiou]" }; + + for (String regex : regexes) { + if (lowercaseWord.matches(regex+".*") == true) { + return "a"; + } + } + + // Special capital words (UK, UN) + if (word.matches("^U[NK][AIEO].*") == true) { + return "a"; + } else if (word == word.toUpperCase()) { + if ("aedhilmnorsx".indexOf(lowercaseWord.substring(0, 1)) >= 0) { + return "an"; + } else { + return "a"; + } + } + + // Basic method of words that begin with a vowel being preceded by 'an' + if ("aeiou".indexOf(lowercaseWord.substring(0, 1)) >= 0) { + return "an"; + } + + // Instances where y followed by specific letters is preceded by 'an' + if (lowercaseWord.matches("^y(b[lor]|cl[ea]|fere|gg|p[ios]|rou|tt).*")) { + return "an"; + } + + return "a"; + } |
