Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/src/parse_num.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
32 changes: 17 additions & 15 deletions lib/src/rake_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ RegExp buildStopWordRegExp(List<String> stopWords) =>
/// Split and collect phrases in sentences
List<String> splitPhrases(List<String> sentences, RegExp stopWords,
{int minChars = 0}) {
minChars = minChars ?? 0;
List<String> phrases = [];
for (final sentence in sentences) {
phrases.addAll(sentence
Expand All @@ -48,38 +47,41 @@ Map<String, double> calculateWordScores(List<String> 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<String, double> 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<String, double> calculateKeywordScores(
List<String> phrases, Map<String, double> wordScore,
{int minFrequency}) {
{int? minFrequency}) {
Map<String, double> keywordCandidates = Map();
Map<String, int> frequencies;
late Map<String, int> 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;
Expand All @@ -93,22 +95,22 @@ class Rake {
final RegExp _stopWordPattern;

/// Extract keywords and sores found in a text
Map<String, double> run(String text, {int minChars, int minFrequency}) {
Map<String, double> run(String text, {int? minChars, int? minFrequency}) {
final List<String> sentences = splitSentences(text);
final List<String> phrases =
splitPhrases(sentences, _stopWordPattern, minChars: minChars);
splitPhrases(sentences, _stopWordPattern, minChars: (minChars ?? 0));
final Map<String, double> wordScores = calculateWordScores(phrases);
return calculateKeywordScores(phrases, wordScores,
minFrequency: minFrequency);
}

/// Extract a list of keywords, ordered by score
List<String> rank(String text, {int minChars, int minFrequency}) {
List<String> 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;
}
}
6 changes: 3 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ homepage: https://github.com/mathisgerdes/dart-rake
author: Mathis Gerdes <mathisgerdes@gmail.com>

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