mirror of
https://gitlab.com/octospacc/nekotsume-ime.git
synced 2025-01-19 07:59:42 +01:00
1 line
10 KiB
JavaScript
1 line
10 KiB
JavaScript
(function(a){function b(a,b){return a<b}function d(a,b,c){var d=a[b];a[b]=a[c],a[c]=d}var c=a.jsheap=function(a){this.cmp=a||b,this.heap=[null]};c.prototype.push=function(a){var b=this.heap,c=this.cmp,e=b.length,f;b.push(a);while(e>1&&c(b[e],b[f=e/2|0]))d(b,e,f),e=f;return this},c.prototype.top=function(){return this.heap[1]},c.prototype.pop=function(){if(this.empty())throw new Error("heap is empty");var a=this.heap,b=this.cmp,c,e=a.pop(),f=a.length;if(f==1)return this;c=1,a[1]=e;while(c*2<f){var g=c*2;g+1<f&&b(a[g+1],a[g])&&++g;if(!b(a[g],a[c]))break;d(a,c,g),c=g}return this},c.prototype.empty=function(){return this.heap.length<=1}})(this),typeof exports=="undefined"?igo={}:(igo=exports,jsheap=igo.jsheap),igo.Morpheme=function(a,b,c){this.surface=a,this.feature=b,this.start=c},igo.Tagger=function(a,b,c){this.wdc=a,this.unk=b,this.mtx=c},igo.Tagger.prototype={parse:function(a,b){b||(b=[]);var c=this.getBestPath(this.parseImpl(a));while(c){var d=a.substring(c.start,c.start+c.length),e=this.wdc.wordData(c.wordId).join("");b.push(new igo.Morpheme(d,e,c.start)),c=c.prev}return b},parseNBest:function(a,b,c){c||(c=[]);var d=this.getNBestPath(this.parseImpl(a),b);for(var e=0;e<d.length;++e){var f=d[e],g=[];while(f){var h=f.node;if(h.wordId!=0){var i=a.substring(h.start,h.start+h.length),j=this.wdc.wordData(h.wordId).join("");g.push(new igo.Morpheme(i,j,h.start))}f=f.next}c.push(g)}return c},wakati:function(a,b){b||(b=[]);var c=this.getBestPath(this.parseImpl(a));while(c){var d=a.substring(c.start,c.start+c.length);b.push(d),c=c.prev}return b},parseImpl:function(a){var b=a.length,c=new Array(b+1);c[0]=[igo.ViterbiNode.makeBOSEOS()];var d=this.wdc,e=this.unk,f=this,g=new igo.MakeLattice(c,function(a,b){return f.setMincostNode(a,b)}),h=function(a){g.call(a)};h.isEmpty=function(){return g.isEmpty()};for(var i=0;i<b;i++){if(!c[i])continue;g.set(i),d.search(a,i,h),e&&e.search(a,i,d,h)}return c[b+1]=[this.setMincostNode(igo.ViterbiNode.makeBOSEOS(),c[b])],c},getBestPath:function(a){var b=a[a.length-1][0].prev,c=undefined;while(b.prev){var d=b.prev;b.prev=c,c=b,b=d}return c},getNBestPath:function(a,b){var c=this.mtx,d=[],e=new jsheap(function(a,b){return a.predict_cost<b.predict_cost}),f=a[a.length-1][0];e.push({node:f,cost:0,predict_cost:f.cost,next:undefined});while(!e.empty()&&d.length<b){var g=e.top();e.pop();if(g.node.wordId==0&&g.next){d.push(g);continue}var h=g.node.leftId,i=g.node.prevs;for(var j=0;j<i.length;++j){var k=g.cost+c.linkCost(i[j].rightId,h)+g.node.nodecost;e.push({node:i[j],cost:k,predict_cost:k+i[j].cost,next:g})}}return d},setMincostNode:function(a,b){var c=this.mtx,d=a.leftId,e=b[0];a.prev=b[0];var f=e.cost+c.linkCost(e.rightId,d);for(var g=1;g<b.length;g++){var h=b[g],i=h.cost+c.linkCost(h.rightId,d);i<f&&(f=i,a.prev=h)}return a.prevs=b,a.cost+=f,a}},igo.MakeLattice=function(a,b){this.nodesAry=a,this.i=0,this.prevs=undefined,this.empty=!0,this.setMincostNode=b},igo.MakeLattice.prototype={set:function(a){this.i=a,this.prevs=this.nodesAry[a],this.nodesAry[a]=undefined,this.empty=!0},call:function(a){this.empty=!1;var b=this.nodesAry,c=this.i+a.length,d=b[c]||[];a.isSpace?d=d.concat(this.prevs):d.push(this.setMincostNode(a,this.prevs)),b[c]=d},isEmpty:function(){return this.empty}},igo.ViterbiNode=function(a,b,c,d,e,f,g){this.cost=d,this.nodecost=d,this.prev=undefined,this.prevs=[],this.wordId=a,this.start=b,this.length=c,this.leftId=e,this.rightId=f,this.isSpace=g},igo.ViterbiNode.makeBOSEOS=function(){return new igo.ViterbiNode(0,0,0,0,0,0,!1)},igo.CharCategory=function(a,b,c){this.categories=igo.CharCategory.readCategories(b,c);var d=new igo.ArrayBufferStream(a,c);this.char2id=d.getIntArray(d.size()/4/2),this.eqlMasks=d.getIntArray(d.size()/4/2)},igo.CharCategory.prototype={category:function(a){return this.categories[this.char2id.get(a.charCodeAt(0))]},isCompatible:function(a,b){return(this.eqlMasks.get(a.charCodeAt(0))&this.eqlMasks.get(b.charCodeAt(0)))!=0}},igo.CharCategory.readCategories=function(a,b){var c=igo.getIntArray(a,b),d=c.length/4,e=[];for(var f=0;f<d;f++)e.push(new igo.Category(c.get(f*4),c.get(f*4+1),c.get(f*4+2)==1,c.get(f*4+3)==1));return e},igo.Category=function(a,b,c,d){this.id=a,this.length=b,this.invoke=c,this.group=d},igo.Unknown=function(a){this.category=a,this.spaceId=this.category.category(" ").id},igo.Unknown.prototype.search=function(a,b,c,d){var e=this.category,f=a[b],g=e.category(f),h=a.length,i;if(!d.isEmpty()&&!g.invoke)return;var j=g.id==this.spaceId,k=Math.min(h,g.length+b);for(i=b;i<k;i++){c.searchFromTrieId(g.id,b,i-b+1,j,d);if(i+1!=k&&!e.isCompatible(f,a[i+1]))return}if(g.group&&k<h){for(i=k;i<h;i++)if(!e.isCompatible(f,a[i])){c.searchFromTrieId(g.id,b,i-b,j,d);return}c.searchFromTrieId(g.id,b,h-b,j,d)}},igo.Matrix=function(a,b){fmis=new igo.ArrayBufferStream(a,b),this.leftSize=fmis.getInt(),this.rightSize=fmis.getInt(),this.matrix=fmis.getShortArray(this.leftSize*this.rightSize)},igo.Matrix.prototype.linkCost=function(a,b){return this.matrix.get(b*this.leftSize+a)},igo.WordDic=function(a,b,c,d,e){this.trie=new igo.Searcher(a,e),this.data=igo.getCharArray(b,e),this.indices=igo.getIntArray(c,e);var f=new igo.ArrayBufferStream(d,e),g=f.size()/10;this.dataOffsets=f.getIntArray(g),this.leftIds=f.getShortArray(g),this.rightIds=f.getShortArray(g),this.costs=f.getShortArray(g)},igo.WordDic.prototype={search:function(a,b,c){function h(a,b,h){var i=g.get(h+1);for(var j=g.get(h);j<i;j++)c(new igo.ViterbiNode(j,a,b,d.get(j),e.get(j),f.get(j),!1))}var d=this.costs,e=this.leftIds,f=this.rightIds,g=this.indices;this.trie.eachCommonPrefix(a,b,h)},searchFromTrieId:function(a,b,c,d,e){var f=this.costs,g=this.leftIds,h=this.rightIds,i=this.indices.get(a+1);for(var j=this.indices.get(a);j<i;j++)e(new igo.ViterbiNode(j,b,c,f.get(j),g.get(j),h.get(j),d))},wordData:function(a){var b=Array(),c=this.dataOffsets.get(a),d=this.dataOffsets.get(a+1);for(var e=c;e<d;e++)b.push(String.fromCharCode(this.data.get(e)));return b}},igo.Node={Base:{ID:function(a){return-a-1}},Chck:{TERMINATE_CODE:0,VACANT_CODE:1,CODE_LIMIT:65535}},igo.Node.Chck.TERMINATE_CHAR=String.fromCharCode(igo.Node.Chck.TERMINATE_CODE),igo.KeyStream=function(a,b){this.s=a,this.cur=b||0,this.len=a.length},igo.KeyStream.compare=function(a,b){var c=a.rest(),d=b.rest();return c<d?-1:c>d?1:0},igo.KeyStream.prototype={startsWith:function(a,b,c){var d=this.cur,e=this.s;return this.len-d<c?!1:e.substring(d,d+c)==a.substring(b,b+c)},rest:function(){return this.s.substring(this.cur)},read:function(){if(this.eos())return igo.Node.Chck.TERMINATE_CHAR;var a=this.cur;return this.cur+=1,this.s.charAt(a)},eos:function(){return this.cur==this.len}},igo.Searcher=function(a,b){var c=new igo.ArrayBufferStream(a,b),d=c.getInt(),e=c.getInt(),f=c.getInt();this.keySetSize=e,this.begs=c.getIntArray(e),this.base=c.getIntArray(d),this.lens=c.getShortArray(e),this.chck=c.getCharArray(d),this.tail=c.getString(f)},igo.Searcher.prototype={size:function(){return this.keySetSize},search:function(a){var b=this.base,c=this.chck,d=b.get(0),e=new KeyStream(a),f=e.read();for(;;){var g=d+f;d=b.get(g);if(c.get(g)==f){if(d>=0)continue;if(e.eos()||this.keyExists(e,d))return igo.Node.Base.ID(d)}return-1}},eachCommonPrefix:function(a,b,c){var d=this.base,e=this.chck,f=d.get(0),g=-1,h=new igo.KeyStream(a,b);for(;;){var i=h.read().charCodeAt(0);g++;var j=f+igo.Node.Chck.TERMINATE_CODE;if(e.get(j)==igo.Node.Chck.TERMINATE_CODE){c(b,g,igo.Node.Base.ID(d.get(j)));if(i==igo.Node.Chck.TERMINATE_CODE)return}var k=f+i;f=d.get(k);if(e.get(k)==i){if(f>=0)continue;this.call_if_keyIncluding(h,f,b,g,c)}return}},call_if_keyIncluding:function(a,b,c,d,e){var f=igo.Node.Base.ID(b);a.startsWith(this.tail,this.begs.get(f),this.lens.get(f))&&e(c,d+this.lens.get(f)+1,f)},keyExists:function(a,b){var c=Node.Base.ID(b),d=this.begs.get(c),e=this.tail.substring(d,d+this.lens.get(c));return a.rest()==e}},igo.getServerFileToArrayBufffer=function(a,b){var c=new XMLHttpRequest;return c.onreadystatechange=function(){c.readyState==c.DONE&&(c.status!=200&&c.status!=304||!c.response?alert("Failed to download:"+c.status+" "+c.statusText):b(c.response))},c.open("GET",a,!0),c.responseType="arraybuffer",c.send(),c},igo.IntArray=function(a,b,c,d){this.buffer=a,this.pos=b,this.length=c,this.bigendian=d},igo.IntArray.readUInt=function(a,b,c){var d=igo.IntArray.readInt(a,b,c);return d>>>0},igo.IntArray.readInt=function(a,b,c){var d=0;return c?d=a[b]<<24|a[b+1]<<16|a[b+2]<<8|a[b+3]:d=a[b+3]<<24|a[b+2]<<16|a[b+1]<<8|a[b],d},igo.IntArray.prototype={get:function(a){var b=this.pos+a*4;return igo.IntArray.readInt(this.buffer,b,this.bigendian)}},igo.ShortArray=function(a,b,c,d){this.buffer=a,this.pos=b,this.length=c,this.bigendian=d},igo.ShortArray.readUShort=function(a,b,c){var d=0;return c?d=a[b]<<8|a[b+1]:d=a[b+1]<<8|a[b],d},igo.ShortArray.readShort=function(a,b,c){var d=igo.ShortArray.readUShort(a,b,c);return d>=32768&&(d-=65536),d},igo.ShortArray.prototype={get:function(a){var b=this.pos+a*2;return igo.ShortArray.readShort(this.buffer,b,this.bigendian)}},igo.CharArray=function(a,b,c,d){this.buffer=a,this.pos=b,this.length=c,this.bigendian=d},igo.CharArray.prototype={get:function(a){var b=this.pos+a*2;return igo.ShortArray.readUShort(this.buffer,b,this.bigendian)}},igo.ArrayBufferStream=function(a,b){typeof a=="ArrayBuffer"?this.buffer=new Uint8Array(a):this.buffer=a,this.bigendian=b,this.pos=0},igo.ArrayBufferStream.prototype={getInt:function(){var a=igo.IntArray.readInt(this.buffer,this.pos,this.bigendian);return this.pos+=4,a},getIntArray:function(a){var b=new igo.IntArray(this.buffer,this.pos,a,this.bigendian);return this.pos+=a*4,b},getShortArray:function(a){var b=new igo.ShortArray(this.buffer,this.pos,a,this.bigendian);return this.pos+=a*2,b},getCharArray:function(a){var b=new igo.CharArray(this.buffer,this.pos,a,this.bigendian);return this.pos+=a*2,b},getString:function(a){var b=new igo.CharArray(this.buffer,this.pos,a,this.bigendian),c="";for(var d=0;d<a;d++)c+=String.fromCharCode(b.get(d));return this.pos+=a*2,c},size:function(){return this.buffer.length}},igo.getIntArray=function(a,b){var c=new igo.ArrayBufferStream(a,b);return c.getIntArray(c.size()/4)},igo.getCharArray=function(a,b){var c=new igo.ArrayBufferStream(a,b);return c.getCharArray(c.size()/2)} |