class SayAn extends Say { private node = document.createTextNode("a "); private uppercase = true; public constructor (autoUppercase? : boolean) { super(); if (autoUppercase != undefined) { this.uppercase = autoUppercase; } } public async getPureElements (say : Say) : Promise> { let next = say.sequence[say.sequenceRunner + 1]; if (next == undefined || (next instanceof Thing && ( next).properlyNamed)) { this.node.nodeValue = ""; } else { let aan = AvsAn.query((await say.getTextOf(say.sequenceRunner + 1, next)).trim()); if (aan.prefix != "") { // avoid "a 52 coins" this.node.nodeValue = aan.article + " "; } if (this.uppercase && say.currentParagraph.length == 0) { this.node.nodeValue = this.node.nodeValue.charAt(0).toUpperCase() + this.node.nodeValue.substr(1, this.node.nodeValue.length - 1); } this.node.nodeValue = this.node.nodeValue + " "; } return [this.node]; } } //by Eamon Nerbonne (from http://home.nerbonne.org/A-vs-An), Apache 2.0 license var AvsAn = (function () { var dict = "p3ezz;4wrlg;2h;#2rg;22;2;a;7;;if;z;;&4h;1c;1;N;6;;*yp;6a;4;a2;q;;e1;q;;i1;h;;o;7;;/op;5n;9;a3;i;;e5;h;;h;;1;o5;;;i;r;;l;;1;/;6;;n;;1;o6;;;o1;a;;r;;1;e7;;;s;;1;/2;j;;09pa;y3;1;8e;10;;17qoq;qmm;2;12hp;7nw;a;0o4;45;1;0n;2w;;15r;1n;2;8;7;;9;5;;28s;x;;34q;z;1;7;5;;45n;n;;598;w;;65k;u;;74j;y;;850;y;;93x;g;;81ux;hgk;a;0zb;el;a;0p;4h;;11;16;;21;10;;32;15;;4;18;;54;v;;6;12;;7;s;;8;v;;9;17;;11k;bq;1; v;2;;229;f3;2; 1a;3;;–5;;;31x;jc;1; 12;2;;41w;kq;2; z;4;;–5;;;559;sp;5; 2k;1;;,h;;;h7;;;kd;;;m6;;;62p;rm;2; 1b;5;;k5;;;72n;y6;2; 14;;;–5;;;82r;192;2; 19;4;;,5;;;93i;1ig;2; 10;4;;–5;;;8ys;nsu;;=n[1]?"a":"an" } dict = dict.substr(1 + a.join(';').length); for (var i = 0; i < n[2]; i++) dict = fill(prefix + dict[0], node[dict[0]] = {}, dict.substr(1)); return dict; } fill("", root, dict); return { raw: root, //Usage example: AvsAn.query("example ") //Note that the terminal space indicates this is a complete word - this is sometimes significant, particularly for acronyms! //returns: { // prefix: "e", //the prefix sufficient to determine the article // aCount: 9682, //the number of times "a" was seen for this prefix // anCount: 1028246, //the number of times "an" was seen for this prefix // article: "an", //the most common article //} query: function (word) { var node : any = root, sI = 0, result, c; do { c = word[sI++]; } while ('"‘’“”$\'-('.indexOf(c) >= 0);//also terminates on end-of-string "undefined". while (1) { result = node.data || result; node = node[c]; if (!node) return result; c = word[sI++] || " "; } } }; })();