/
grammar.js
65 lines (61 loc) · 1.51 KB
/
grammar.js
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
var Grammar = (function() {
var PRONOUNS = {
male: {
himself: "himself",
his: "his",
him: "him",
he: "he"
},
female: {
himself: "herself",
his: "her",
him: "her",
he: "she"
},
neuter: {
himself: "itself",
his: "its",
him: "it",
he: "it"
},
you: {
himself: "yourself",
his: "your",
him: "you",
he: "you"
}
};
var utils = {
makePronounDict: function(gender, prefix) {
if (!prefix)
prefix = "";
if (!(gender in PRONOUNS))
throw new Error("invalid gender: " + gender);
var dict = {};
for (var name in PRONOUNS[gender])
dict[prefix + name] = PRONOUNS[gender][name];
return dict;
},
applyDict: function(string, dict) {
return string.replace(/%\(([a-zA-Z0-9_]+)\)s/g, function(str, term) {
return dict[term].toString();
});
},
parseString: function(string, isSquarePlural, isAnglePlural) {
var SQUARES = /\[(.*?)(\|(.+))?\]/g,
ANGLES = /\<(.*?)(\|(.+))?\>/g;
function replaceBrackets(string, regexp, isPlural) {
return string.replace(regexp, function(str, singular, plural) {
if (isPlural)
return plural ? plural.slice(1) : "";
return singular;
});
}
string = replaceBrackets(string, SQUARES, isSquarePlural);
return replaceBrackets(string, ANGLES, isAnglePlural);
}
};
return {
utils: utils
};
})();