platform/packages/inflection3/lib/src/past.dart

70 lines
2 KiB
Dart
Raw Normal View History

2021-05-02 08:39:25 +00:00
//library inflection2.past;
import 'dart:convert';
import 'irregular_past_verbs.dart';
import 'verbs_ending_with_ed.dart';
import 'util.dart';
class PastEncoder extends Converter<String, String> {
final List<List> _inflectionRules = [];
PastEncoder() {
irregularPastVerbs.forEach((String presentOrParticiple, String past) {
addIrregularInflectionRule(presentOrParticiple, past);
});
[
[r'.+', (Match m) => '${m[0]}ed'],
[r'([^aeiou])y$', (Match m) => '${m[1]}ied'],
[r'([aeiou]e)$', (Match m) => '${m[1]}d'],
[r'[aeiou][^aeiou]e$', (Match m) => '${m[0]}d']
]
.reversed
.forEach((rule) => addInflectionRule(rule.first as String, rule.last));
}
void addInflectionRule(String presentOrParticiple, dynamic past) {
_inflectionRules
2021-05-17 15:10:07 +00:00
.add([RegExp(presentOrParticiple, caseSensitive: false), past]);
2021-05-02 08:39:25 +00:00
}
void addIrregularInflectionRule(String presentOrParticiple, String past) {
_inflectionRules.add([
2021-05-17 15:10:07 +00:00
RegExp(
2021-05-02 08:39:25 +00:00
r'^(back|dis|for|fore|in|inter|mis|off|over|out|par|pre|re|type|un|under|up)?' +
presentOrParticiple +
r'$',
caseSensitive: false),
(Match m) => (m[1] == null) ? past : m[1]! + past
]);
}
@override
String convert(String word) {
2021-05-17 15:10:07 +00:00
if (word.isNotEmpty) {
if (word.contains('ed', word.length - 2)) {
var reg = RegExp(
2021-05-02 08:39:25 +00:00
r'^(back|dis|for|fore|in|inter|mis|off|over|out|par|pre|re|type|un|under|up)(.+)$');
if (reg.hasMatch(word)) {
2021-05-17 15:10:07 +00:00
if (!verbsEndingWithEd.contains(reg.firstMatch(word)!.group(2))) {
2021-05-02 08:39:25 +00:00
return word;
2021-05-17 15:10:07 +00:00
}
2021-05-02 08:39:25 +00:00
} else if (!verbsEndingWithEd.contains(word)) {
return word;
}
}
for (var r in _inflectionRules) {
var pattern = r.first as RegExp;
if (pattern.hasMatch(word)) {
return word.replaceAllMapped(pattern, r.last as MatchToString);
}
}
}
return word;
}
}
2021-05-17 15:10:07 +00:00
final Converter<String, String> PAST = PastEncoder();