String.prototype.replaceAll = function(search, replacement) { var target = this; return target.replace(new RegExp(search, 'g'), replacement); }; function print(val) { console.log(val) } function dictFormat(s, valueDict) { ret = s; s.match(/\{[^}]+\}/g).forEach(function(repName){ key = repName.replace(/[{}]/g, ""); ret = ret.replace(repName, valueDict[key]) }); return ret; } function get(url) { // Return a new promise. return new Promise(function(resolve, reject) { // Do the usual XHR stuff var req = new XMLHttpRequest(); req.open('GET', url); req.onload = function() { // This is called even on 404 etc // so check the status if (req.status == 200) { // Resolve the promise with the response text resolve(req.response); } else { // Otherwise reject with the status text // which will hopefully be a meaningful error reject(Error(req.statusText)); } }; // Handle network errors req.onerror = function() { reject(Error("Network Error")); }; // Make the request req.send(); }); } async function read_rules(filename) { // let rawdata = "" hidden = document.getElementById('x') if (hidden.value.length == 0 || filename != "") { // Existing Rules: rawdata = await get(filename); } else { // Upload Rules: rawdata = hidden.value } console.log(rawdata) const data = rawdata.split("\n"); let headers = data[0].split(",") // Get headers: for (var i=0; i 0 && data[i][0] != "#") { headers = data[i].split(",") break } } rule_list = [] // Get rules: for (var i=0; i 0 && data[i][0] != "#") { var split_rule = data[i].split(","); if (split_rule[0] != headers[0]) { // ignore header var rule_dict = {} for (var j=0; j { this.rule_list = rules; this.init(); }) } init() { this.classes = {} this.subs = new Set([]) this.ipasubs = new Set([]) this.words = {} this.matches = {} this.pre = [] this.NO_TRANSLATE = "@" // Iterate over rules: for (var i=0; i 1) { if (source.length > 1) { for (let letter in source) { var match = source[letter]; for (let item in this.matches) { var out = this.matches[item] if (match == item) { target_list.push(out); source_list.pop(); } } } } else { for (let item in this.matches) { var out = this.matches[item] if (source == item) { target_list.push(out); source_list.pop(); } } } } for (var i=0; i trans[0]) // Exclude translations that didn't apply, and sort by weight: if (translations.length > 0) { var translation = translations.sort(function(a,b) { return(b[0] - a[0]) })[0][1] if (translation.length > 0) { target_list.push(translation) } } else { target_list.push(this.NO_TRANSLATE) } } var target_string = (target_list).join(" ") print(target_string) var ipa_translations = [] this.ipasubs.forEach(function(ipasubrule) { let ipa_trans = [ipasubrule.weight, ipasubrule] ipa_translations.push(ipa_trans) }) ipa_translations = ipa_translations.sort(function(a,b) { return(b[0] - a[0]) }) for (var i=0; i