syllableCounter property Null safety
override
The English implementation of syllableCounter.
Algorithm:
- Trim leading and trailing white-space from term.
- Return 0 if resulting term is empty.
- Split the term using the termSplitter
Implementation
@override
SyllableCounter get syllableCounter => (term) {
term = term.trim();
// return 0 if term is empty
if (term.isEmpty) return 0;
// return 1 if term length is less than 3
if (term.length < 3) {
return 1;
} else {
// initialize the return value
var count = 0;
// split term into terms at non-word characters
final terms =
term.split(RegExp(Porter2StemmerConstants.rEnglishNonWordChars));
for (var e in terms) {
// stem the remaining term with the SyllableStemmer.
// DO NOT USE the analyzer stemmer as it may be overridden and we
// must make sure the trailing silent e's are removed and vowel "y"s
// are converted to "i"
e = SyllableStemmer().stem(e);
// count apostropied syllables like "d'Azure" and remove the
// apostrophied prefix
e = e.replaceAllMapped(RegExp(r"(?<=\b)([a-zA-Z]')"), (match) {
count++;
return '';
});
// check for terms with capitals or remaining punctuation
if (e.toUpperCase() == e) {
// this is more than likely an acronym, so add 1
count++;
} else {
// add all the single vowels, diphtongs and triptongs.
// As e has been stemmed by the Porter2 stemmer, we know trailing
// silent e's have been removed and vowel "y"s converted to "i"
e = e.toLowerCase();
if (e.contains(RegExp(r"[^aeiou\s\-\']+(?=\b)"))) {
// term ends in one or more consonants or ys
count += RegExp(r'[aeiouyà-æè-ðò-öø-ÿ]+').allMatches(e).length;
// check for stemmed words ending in 3 or more consonants
count +=
e.contains(RegExp(r"[^aeiouyà-æè-ðò-öø-ÿ\s\-\']{3,}(?=\b)"))
? 1
: 0;
} else {
count += RegExp(r'[aeiouyà-æè-ðò-öø-ÿ]+').allMatches(e).length;
}
}
}
// if count is 0, return 1 because a word must have at least one syllable
return count < 1 ? 1 : count;
}
};