grammarArticle method

String grammarArticle()

Returns the appropriate indefinite article ('a' or 'an') for this word.

Uses English grammar heuristics:

  • Silent 'h' words (hour, honest, honor, heir) → 'an'
  • "You"-sound words (uni, use, user, union, university) → 'a'
  • Words starting with 'one' (one-time, one-way) → 'a'
  • Vowel sounds (a, e, i, o, u) → 'an'
  • Consonant sounds → 'a'

The word is trimmed and compared case-insensitively.

Returns: 'a' or 'an' based on pronunciation heuristics, or empty string if input is empty.

Example:

'apple'.grammarArticle(); // 'an'
'hour'.grammarArticle(); // 'an' (silent h)
'user'.grammarArticle(); // 'a' (you-sound)
'university'.grammarArticle(); // 'a'
'one-time'.grammarArticle(); // 'a'
'elephant'.grammarArticle(); // 'an'

Implementation

String grammarArticle() {
  if (isEmpty) return '';
  final String word = trim();
  if (word.isEmpty) return '';
  final String lower = word.toLowerCase();

  const List<String> silentH = <String>['hour', 'honest', 'honor', 'heir'];
  for (final String ex in silentH) {
    if (lower.startsWith(ex)) return 'an';
  }

  const List<String> youSound = <String>['uni', 'use', 'user', 'union', 'university'];
  for (final String ex in youSound) {
    if (lower.startsWith(ex)) return 'a';
  }

  if (lower.startsWith('one')) return 'a';

  switch (lower[0]) {
    case 'a':
    case 'e':
    case 'i':
    case 'o':
    case 'u':
      return 'an';
    default:
      return 'a';
  }
}