Skip to content

Instantly share code, notes, and snippets.

@phi16
Last active August 29, 2015 14:24
Show Gist options
  • Save phi16/5b5abd391338e9975612 to your computer and use it in GitHub Desktop.
Save phi16/5b5abd391338e9975612 to your computer and use it in GitHub Desktop.
Type
var Type = (function(){
var t = {};
function product(a,b){
var arr = [];
for(var i=0;i<a.length;i++){
for(var j=0;j<b.length;j++){
arr.push(a[i]+b[j]);
}
}
return arr;
}
function strEach(str,f){
for(var i=0;i<str.length;i++){
f(str.charAt(i));
}
}
var vowel = "aiueo";
var preset = product("xkstnhmrgzdbp",vowel);
preset.push("a","i","u","e","o");
preset.push("ya","yu","yo","xya","xyu","xyo");
preset.push("wa","wo","nn");
preset.push("xtu","-");
var map = {};
preset.forEach(function(e){
map[e] = [e];
if(e.charAt(0)=="x"){
map[e].push("l"+e.slice(1));
}
});
var spec = {
si:["shi"],
ti:["chi"],
tu:["tsu"],
hu:["fu"],
nn:["n"],
zi:["ji"]
};
for(var i in spec){
spec[i].forEach(function(v){
map[i].push(v);
});
}
var dupK = "kstnhmrgzdbpsc";
var dupV = "yyyyyyyyyyyyhh";
for(var i=0;i<dupK.length;i++){
var s = dupK.charAt(i);
var u = s+dupV.charAt(i);
if(s == "c")s="t"
if(!map[s+"ixya"]){
map[s+"ixya"]=[];
map[s+"ixyu"]=[];
map[s+"ixyo"]=[];
}
map[s+"ixya"].push(u+"a");
map[s+"ixyu"].push(u+"u");
map[s+"ixyo"].push(u+"o");
}
map["zixya"]=["ja"];
map["zixyu"]=["ju"];
map["zixyo"]=["jo"];
var heb = "bmp";
function sm(v){
return !(v == "i" || v == "e")
}
strEach(heb,function(c){
strEach(vowel,function(v){
map["nn"+c+v]=["m"+c+v];
if(sm(v))map["nn"+c+"ixy"+v]=["m"+c+"y"+v];
});
});
var dds = "ksthmrywgzdbpj";
for(var i in map){
var d = dds.match(i.charAt(0));
if(d){
map["xtu"+i]=[];
map[i].forEach(function(v){
map["xtu"+i].push(v.charAt(0)+v);
});
}
}
map["-"]=["-"];
t.split = function(str){
var arr = [];
while(str.length >= 0){
var f = false;
for(var i=1;i<=3;i++){
if(preset.indexOf(str.slice(0,i)) >= 0){
arr.push(str.slice(0,i));
str = str.substr(i);
f = true;
break;
}
}
if(!f)break;
}
if(str.length != 0)arr = null;
return arr;
};
t.invert = function(str){
var arr = [];
while(str.length >= 0){
var f = false;
for(var i in map){
map[i].forEach(function(v){
if(!f && str.indexOf(v) == 0){
arr.push(i);
str = str.substr(v.length);
f = true;
}
});
if(f)break;
}
if(!f)break;
}
if(str.length != 0)arr = null;
return arr;
};
t.normalize = function(str){
var a = t.invert(str);
var s = "";
for(var i=0;i<a.length;i++){
s += a[i];
}
var arr = [];
var b = t.split(s);
for(var i=0;i<b.length;){
var f = false;
for(var j=3;j>0;j--){
if(i+j>b.length)continue;
var cs = "";
for(var k=i;k<i+j;k++){
cs += b[k];
}
if(map[cs]){
arr.push(cs);
i+=j;
f=true;
break;
}
}
if(!f)break;
}
return arr;
};
t.graphize = function(arr){
var app = {};
function strf(str){
return JSON.stringify(str);
}
function traverse(a){
if(app[strf(a)])return app[strf(a)];
if(a.length == 0)return {};
var res = {};
var can = {};
for(var i=0;i<a.length;i++){
if(!can[a[i][0]])can[a[i][0]]=[];
if(a[i].length >= 2)can[a[i][0]].push(a[i].substr(1));
}
for(var i in can){
var v = app[i+can[i]];
if(v){
if(!res["eps"])res["eps"] = [];
res["eps"].push(v);
}else res[i] = traverse(can[i]);
}
return app[strf(a)]=res;
}
var nfa = traverse(arr);
function dispObj(x){
var t = "{";
var ks = Object.keys(x).sort();
for(var i=0;i<ks.length;i++){
t += ks[i];
if(ks[i]!="eps")t += dispObj(x[ks[i]]);
else t += strf(x[ks[i]]);
t+=",";
}
return t + "}";
}
function dispArr(arr){
var t = "[";
for(var i=0;i<arr.length;i++){
t += dispObj(arr[i]) + ",";
}
return t += "]";
}
function unify(arr){
arr.sort(function(x,y){
var s = dispObj(x);
var t = dispObj(y);
if(s < t)return -1;
else if(s > t)return 1;
else return 0;
});
return arr;
}
function reachable(a){
var d = [];
a.forEach(function(v){
d.push(v);
if(v["eps"]){
reachable(v["eps"]).forEach(function(w){
d.push(w);
});
}
});
return unify(d);
}
app = {};
function entangle(a){
var s = reachable(a);
if(app[dispArr(s)])return app[dispArr(s)];
if(s.length == 0)return {};
var res = {};
var can = {};
for(var i=0;i<s.length;i++){
for(var k in s[i]){
if(k!="eps"){
if(!can[k])can[k]=[];
can[k].push(s[i][k]);
}
}
}
for(var i in can){
res[i] = entangle(unify(can[i]));
}
return app[dispArr(s)]=res;
}
var dfa = entangle([nfa]);
return dfa;
};
var graph = {};
var c2 = {};
for(var i in map){
var cands = [];
map[i].forEach(function(v){
cands.push(v);
});
var a = t.split(i);
if(a.length == 1){
}else if(a.length == 2){
product(map[a[0]],map[a[1]]).forEach(function(v){
cands.push(v);
});
}else{
product(product(map[a[0]],map[a[1]]),map[a[2]]).forEach(function(v){
cands.push(v);
});
product(map[a[0]+a[1]],map[a[2]]).forEach(function(v){
cands.push(v);
});
product(map[a[0]],map[a[1]+a[2]]).forEach(function(v){
cands.push(v);
});
}
c2[i]=cands;
graph[i]=t.graphize(cands);
}
t.cand = c2;
t.map = map;
t.graph = graph;
return t;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment