diff --git a/lib/src/parse_num.dart b/lib/src/parse_num.dart index 27aeffe..ede562b 100644 --- a/lib/src/parse_num.dart +++ b/lib/src/parse_num.dart @@ -5,14 +5,15 @@ bool _isOtherNum(s) => _numAltRegex.hasMatch(s); bool isNum(String s) => num.tryParse(s) != null || _isStandardNum(s) || _isOtherNum(s); -num tryParseNum(String s) { +num? tryParseNum(String s) { try { return num.parse(s); - } catch (FormatException) { - if (_isStandardNum(s)) + } catch (e) { + if (_isStandardNum(s)) { return num.parse(s.replaceAll(',', '')); - else if (_isOtherNum(s)) + } else if (_isOtherNum(s)) { return num.parse(s.replaceAll('.', '').replaceFirst(',', '.')); + } return null; } } diff --git a/lib/src/rake_base.dart b/lib/src/rake_base.dart index 826a855..ff3d83d 100644 --- a/lib/src/rake_base.dart +++ b/lib/src/rake_base.dart @@ -26,7 +26,6 @@ RegExp buildStopWordRegExp(List stopWords) => /// Split and collect phrases in sentences List splitPhrases(List sentences, RegExp stopWords, {int minChars = 0}) { - minChars = minChars ?? 0; List phrases = []; for (final sentence in sentences) { phrases.addAll(sentence @@ -48,38 +47,41 @@ Map calculateWordScores(List phrases) { final wordListDegree = wordListCount - 1; for (final word in wordList) { wordFrequency.putIfAbsent(word, () => 0); - wordFrequency[word] += 1; + wordFrequency[word] = wordFrequency[word]! + 1; wordDegree.putIfAbsent(word, () => 0); - wordDegree[word] += wordListDegree; + wordDegree[word] = wordDegree[word]! + wordListDegree; } } - for (final word in wordFrequency.keys) - wordDegree[word] += wordFrequency[word]; + for (final word in wordFrequency.keys) { + wordDegree[word] = wordDegree[word]! + wordFrequency[word]!; + } Map wordScore = Map(); for (final word in wordFrequency.keys) { wordScore.putIfAbsent(word, () => 0); - wordScore[word] = wordDegree[word] / wordFrequency[word]; + wordScore[word] = wordDegree[word]! / wordFrequency[word]!; } return wordScore; } Map calculateKeywordScores( List phrases, Map wordScore, - {int minFrequency}) { + {int? minFrequency}) { Map keywordCandidates = Map(); - Map frequencies; + late Map frequencies; if (minFrequency != null) { frequencies = Map(); phrases.forEach((phrase) => frequencies.update(phrase, (v) => v + 1, ifAbsent: () => 1)); } for (final phrase in phrases) { - if (minFrequency != null && frequencies[phrase] < minFrequency) continue; + if (minFrequency != null && frequencies[phrase]! < minFrequency) continue; keywordCandidates.putIfAbsent(phrase, () => 0); final words = splitWords(phrase); double candidateScore = 0; - for (final word in words) candidateScore += wordScore[word]; + for (final word in words) { + candidateScore += wordScore[word]!; + } keywordCandidates[phrase] = candidateScore; } return keywordCandidates; @@ -93,22 +95,22 @@ class Rake { final RegExp _stopWordPattern; /// Extract keywords and sores found in a text - Map run(String text, {int minChars, int minFrequency}) { + Map run(String text, {int? minChars, int? minFrequency}) { final List sentences = splitSentences(text); final List phrases = - splitPhrases(sentences, _stopWordPattern, minChars: minChars); + splitPhrases(sentences, _stopWordPattern, minChars: (minChars ?? 0)); final Map wordScores = calculateWordScores(phrases); return calculateKeywordScores(phrases, wordScores, minFrequency: minFrequency); } /// Extract a list of keywords, ordered by score - List rank(String text, {int minChars, int minFrequency}) { + List rank(String text, {int? minChars, int? minFrequency}) { final keywordScores = run(text, minFrequency: minFrequency, minChars: minChars); final keywords = keywordScores.keys.toList(); - keywords.sort( - (a, b) => a == b ? 0 : (keywordScores[a] > keywordScores[b] ? -1 : 1)); + keywords.sort((a, b) => + a == b ? 0 : (keywordScores[a]! > keywordScores[b]! ? -1 : 1)); return keywords; } } diff --git a/pubspec.yaml b/pubspec.yaml index 3538e21..1944d38 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -5,8 +5,8 @@ homepage: https://github.com/mathisgerdes/dart-rake author: Mathis Gerdes environment: - sdk: '>=2.1.0 <3.0.0' + sdk: '>=2.12.0 <3.0.0' dev_dependencies: - pedantic: ^1.0.0 - test: ^1.0.0 + pedantic: ^1.11.0 + test: ^1.17.3