wordsRepetition function
Implementation
Map<String, int> wordsRepetition(
String text, {
bool sorted = false,
Order order = Order.descending,
}) {
Map<String, int> result = {};
final list = text.split(' ');
for (String word in list) {
if (result[word] == null) {
result[word] = 1;
} else {
result[word] = (result[word]! + 1);
}
}
if (sorted) {
result = Map.fromEntries(result.entries.toList()
..sort((e1, e2) => order == Order.ascending
? e1.value.compareTo(e2.value)
: e2.value.compareTo(e1.value)));
}
return result;
}