add method

int? add(
  1. String? str, {
  2. bool? ignorePunctuation = false,
})

Add another string to be processed, returns the counts. ignorePunctuation facilitates checking words that include symbols.

Implementation

int? add(String? str, {bool? ignorePunctuation = false}) {
  if (str!.isEmpty) {
    _lastPos = 0;
    return _count;
  }

  _count = _count! + wordFrequency(str, _word!, lastPos: _lastPos!, ignorePunctuation: ignorePunctuation!)!;

  var start = str.length - _word!.length;
  start = start < 0 ? 0 : start;
  late var inWord;
  int? j;
  for (int? i = start; i! < str.length; i++) {
    inWord = false;
    for (j = 0; j! + i < str.length && j < _word!.length; j++) {
      // If first character in string is a space, skip
      var punctuationContinue = !ignorePunctuation && str[i].isPunctuation;
      if (i == 0 && (str[i].isWhiteSpace || punctuationContinue)) break;

      // If first letter in match and last letter was not a space or punctuation, skip
      if (j == 0 && i > 0 && !str[i - 1].isWhiteSpace && (!ignorePunctuation && !str[i - 1].isPunctuation)) break;

      // If the letters dont match in order, break
      if (_word![j] != str[j + i]) {
        break;
      }
      inWord = true;
    }
    i += j;
  }
  if (inWord) {
    _lastPos = j;
  } else {
    _lastPos = 0;
  }
  return _count;
}