Repository navigation
Expand file tree
/
Copy pathgrammar_engine.js
More file actions
87 lines (61 loc) · 1.7 KB
/
Copy pathgrammar_engine.js
File metadata and controls
87 lines (61 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// grammar_engine.js
function singulariseThenPluralise(word) {
let base;
if (word.endsWith('ies')) {
base = word.slice(0, -3) + 'y';
} else if (word.endsWith('es')) {
base = word.slice(0, -2);
} else if (word.endsWith('s')) {
base = word.slice(0, -1);
} else {
return undefined;
}
if (base in wordbook) {
return pluralise(wordbook[base]);
}
return undefined;
}
function pluralise(word) {
if (word.endsWith('y')) {
return word.slice(0, -1) + 'ies';
} else if (!word.endsWith('s')) {
return word + 's';
}
return undefined;
}
function presentContinuous(word) {
if (!word.endsWith('ing')) return undefined;
let base = word.slice(0, -3);
if (base in wordbook) {
return wordbook[base] + "ing";
}
if ((base + "e") in wordbook) {
let withoutE = wordbook[base + "e"].slice(0, -1);
return withoutE + "ing";
}
return undefined;
}
function toPastTense(word) {
if (word.endsWith('ed')) return undefined;
if (word.endsWith('y') && word.length > 1 && !isVowel(word[word.length - 2])) {
return word.slice(0, -1) + 'ied';
}
if (word.endsWith('e')) {
return word + 'd'; //
}
return word + 'ed';
}
function toPresentTenseThenPastTense(word) {
const originalWord = word;
if (!word.endsWith('ed')) return undefined;
let base = word.slice(0, -2);
if (!(base in wordbook) && originalWord.endsWith('d')) {
base = originalWord.slice(0, -1);
}
if (!(base in wordbook)) return undefined;
return toPastTense(wordbook[base]);
}
// helper
function isVowel(ch) {
return 'aeiou'.includes(ch.toLowerCase());
}