17 lines
439 B
Dart
17 lines
439 B
Dart
|
class WordCount {
|
||
|
Map<String, int> countWords(String input) {
|
||
|
var exp = new RegExp(r"(\w+('\w+)?)");
|
||
|
return exp
|
||
|
.allMatches(input)
|
||
|
.map((e) => e.group(0)!.toLowerCase())
|
||
|
.fold(new Map<String, int>(), (Map<String, int> counts, String word) {
|
||
|
if (counts.containsKey(word)) {
|
||
|
counts[word] = counts[word]! + 1;
|
||
|
} else {
|
||
|
counts[word] = 1;
|
||
|
}
|
||
|
return counts;
|
||
|
});
|
||
|
}
|
||
|
}
|