countWordsMatched function

int countWordsMatched (
  1. String matchAgainst,
  2. String matchFor,
  3. {bool ignoreArticles: false,
  4. bool matchCaseInsensitively: true,
  5. bool ignoreSpecialCharacters: true}
)

Returns how many words in matchFor matched against matchAgainst.

Implementation

int countWordsMatched(String matchAgainst, String matchFor,
    {bool ignoreArticles = false,
    bool matchCaseInsensitively = true,
    bool ignoreSpecialCharacters = true}) {
  if (ignoreArticles) {
    matchAgainst = removeArticles(matchAgainst);
    matchFor = removeArticles(matchFor);
  }
  if (matchCaseInsensitively) {
    matchAgainst = matchAgainst.toLowerCase();
    matchFor = matchFor.toLowerCase();
  }

  List<String> matchForWords = (ignoreSpecialCharacters
          ? removeSpecialCharactersAroundWords(matchFor)
          : matchFor)
      .split(' ');
  List<String> matchAgainstWords = (ignoreSpecialCharacters
          ? removeSpecialCharactersAroundWords(matchAgainst)
          : matchAgainst)
      .split(' ');
  var wordsMatched = 0;
  for (var word in matchAgainstWords) {
    if (matchForWords.contains(word)) {
      matchForWords.remove(word);
      ++wordsMatched;
    }
  }
  return wordsMatched;
}