UNPKG

@sutton-signwriting/core

Version:

a javascript package for node and browsers that supports general processing of the Sutton SignWriting script

3 lines (2 loc) 9.44 kB
/* Sutton SignWriting Core Module v2.0.0 (https://github.com/sutton-signwriting/core), author: Steve Slevinski (https://SteveSlevinski.me), license: MIT */ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});let e={null:"S00000",symbol:"S[123][0-9a-f]{2}[0-5][0-9a-f]",coord:"[0-9]{3}x[0-9]{3}",sort:"A",box:"[BLMR]"};e.nullorsymbol=`(?:${e.null}|${e.symbol})`,e.prefix=`(?:${e.sort}${e.nullorsymbol}+)`,e.spatial=`${e.symbol}${e.coord}`,e.signbox=`${e.box}${e.coord}(?:${e.spatial})*`,e.sign=`${e.prefix}?${e.signbox}`,e.sortable=`${e.prefix}${e.signbox}`;let t={colorize:"C",colorhex:"(?:[0-9a-fA-F]{3}){1,2}",colorname:"[a-zA-Z]+",padding:"P[0-9]{2}",zoom:"Z(?:[0-9]+(?:\\.[0-9]+)?|x)",classbase:"-?[_a-zA-Z][_a-zA-Z0-9-]{0,100}",id:"[a-zA-Z][_a-zA-Z0-9-]{0,100}"};t.colorbase=`(?:${t.colorhex}|${t.colorname})`,t.color=`_${t.colorbase}_`,t.colors=`_${t.colorbase}(?:,${t.colorbase})?_`,t.background=`G${t.color}`,t.detail=`D${t.colors}`,t.detailsym=`D[0-9]{2}${t.colors}`,t.classes=`${t.classbase}(?: ${t.classbase})*`,t.full=`-(${t.colorize})?(${t.padding})?(${t.background})?(${t.detail})?(${t.zoom})?(?:-((?:${t.detailsym})*))?(?:-(${t.classes})?!(?:(${t.id})!)?)?`;const o=e=>(new RegExp(`^${t.colorhex}$`).test(e)?"#":"")+e,s=e=>{const s=`^${t.full}`,n=("string"==typeof e?e.match(new RegExp(s)):[])||[];return a={colorize:n[1]?!!n[1]:void 0,padding:n[2]?parseInt(n[2].slice(1)):void 0,background:n[3]?o(n[3].slice(2,-1)):void 0,detail:n[4]?n[4].slice(2,-1).split(",").map(o):void 0,zoom:n[5]?"Zx"===n[5]?"x":parseFloat(n[5].slice(1)):void 0,detailsym:n[6]?n[6].match(new RegExp(t.detailsym,"g")).map((e=>{const t=e.split("_"),s=t[1].split(",").map(o);return{index:parseInt(t[0].slice(1)),detail:s}})):void 0,classes:n[7]?n[7]:void 0,id:n[8]?n[8]:void 0},Object.fromEntries(Object.entries(a).filter((([e,t])=>void 0!==t)));var a},n=e=>e.split("x").map((e=>parseInt(e))),a={symbol:o=>{const s=`^(${e.symbol})(${e.coord})?(${t.full})?`,a="string"==typeof o?o.match(new RegExp(s)):void 0;return{symbol:a?a[1]:void 0,coord:a&&a[2]?n(a[2]):void 0,style:a?a[3]:void 0}},sign:o=>{const s=`^(${e.prefix})?(${e.signbox})(${t.full})?`,a="string"==typeof o?o.match(new RegExp(s)):void 0;return a?{sequence:a[1]?a[1].slice(1).match(/.{6}/g):void 0,box:a[2][0],max:n(a[2].slice(1,8)),spatials:a[2].length<9?void 0:a[2].slice(8).match(/(.{13})/g).map((e=>({symbol:e.slice(0,6),coord:[parseInt(e.slice(6,9)),parseInt(e.slice(10,13))]}))),style:a[3]}:{}},text:o=>{if("string"!=typeof o)return[];const s=`(${e.sign}(${t.full})?|${e.spatial}(${t.full})?)`,n=o.match(new RegExp(s,"g"));return n?[...n]:[]}},l={symbol:o=>{if("string"==typeof o.symbol){const s=(o.symbol.match(e.symbol)||[""])[0];if(s){return s+((((o.coord&&o.coord[0]||"").toString()+"x"+(o.coord&&o.coord[1]||"").toString()).match(e.coord)||[""])[0]||"")+("string"==typeof o.style&&(o.style.match(t.full)||[""])[0]||"")}}},sign:o=>{let s="string"!=typeof o.box?"M":(o.box+"M").match(e.box);const n=(((o.max&&o.max[0]||"").toString()+"x"+(o.max&&o.max[1]||"").toString()).match(e.coord)||[""])[0]||"";if(!n)return;let a="";o.sequence&&Array.isArray(o.sequence)&&(a=o.sequence.map((t=>(t.match(e.nullorsymbol)||[""])[0])).join(""),a=a?"A"+a:"");let l="";o.spatials&&Array.isArray(o.spatials)&&(l=o.spatials.map((t=>{if("string"==typeof t.symbol){const o=(t.symbol.match(e.symbol)||[""])[0];if(o){const s=(((t.coord&&t.coord[0]||"").toString()+"x"+(t.coord&&t.coord[1]||"").toString()).match(e.coord)||[""])[0]||"";if(s)return o+s}}return""})).join(""));return a+s+n+l+("string"==typeof o.style&&(o.style.match(t.full)||[""])[0]||"")}},r=e=>{let t,o,n,l,r,i,c,p,d=a.sign(e);d.spatials?(l=Math.min(...d.spatials.map((e=>e.coord[0]))),r=d.max[0],t=r-l,i=Math.min(...d.spatials.map((e=>e.coord[1]))),c=d.max[1],o=c-i,n="sign",p=d.box):(d=a.symbol(e),p="M",d.coord?(l=d.coord[0],t=2*(500-l),i=d.coord[1],o=2*(500-i),n="symbol"):(l=490,t=20,i=490,o=20,n="none"));let m=s(d.style),u=m.zoom||1,g=m.padding||0;return{minX:l,minY:i,width:t,height:o,segment:n,lane:{B:0,L:-1,M:0,R:1}[p],padding:g,zoom:u}},i=[{index:0,name:"UNK",value:"[UNK]"},{index:1,name:"PAD",value:"[PAD]"},{index:2,name:"CLS",value:"[CLS]"},{index:3,name:"SEP",value:"[SEP]"}],c=(e,{sequence:t=!0,signbox:o=!0,sep:s="[SEP]"}={})=>{const n=e=>[e.slice(0,4),`c${e.charAt(4)}`,`r${e.charAt(5)}`],l=e=>e.map((e=>`p${e}`));return a.text(e).map((e=>{if(/[BLMR]/.test(e)){const r=a.sign(e),i=[];return r.sequence&&t&&i.push("A",...r.sequence.map((e=>n(e))).flat()),o&&i.push(r.box,...l(r.max),...r.spatials.flatMap((e=>[...n(e.symbol),...l(e.coord)]))),s?[...i,s]:i}{const r=a.symbol(e);if(!o&&!t)return[];let i=[];return i=!o&&t?["A",...n(r.symbol)]:["M",...l(r.coord.map((e=>1e3-e))),...n(r.symbol),...l(r.coord)],i.length>0&&s?[...i,s]:i}})).flatMap((e=>e))},p=(e,t=i)=>{const o=new Set(t.map((e=>e.value)));return e.filter((e=>!o.has(e))).join(" ").replace(/\bp(\d{3})\s+p(\d{3})/g,"$1x$2").replace(/ c(\d)\d? r(.)/g,"$1$2").replace(/ c(\d)\d?/g,"$10").replace(/ r(.)/g,"0$1").replace(/ /g,"").replace(/(\d)([BLMR])/g,"$1 $2").replace(/(\d)(AS)/g,"$1 $2").replace(/(A(?:S00000|S[123][0-9a-f]{2}[0-5][0-9a-f])+)( )([BLMR])/g,"$1$3")},d={height:500,width:150,offset:50,pad:20,margin:5,dynamic:!1,background:void 0,punctuation:{spacing:!0,pad:30,pull:!0},style:{detail:["black","white"],zoom:1}},m=e=>("object"!=typeof e&&(e={}),{...d,...e,punctuation:{...d.punctuation,...e.punctuation},style:{...d.style,...e.style}}),u=[256,517,759,767,877,895,903],g={all:[256,907],writing:[256,894],hand:[256,516],movement:[517,758],dynamic:[759,766],head:[767,876],hcenter:[767,876],vcenter:[767,885],trunk:[877,885],limb:[886,894],location:[895,902],punctuation:[903,907]},x=["#0000CC","#CC0000","#FF0099","#006600","#000000","#884411","#FF9900"];exports.category=u,exports.colorize=e=>{const t=a.symbol(e);let o="#000000";if(t.symbol){const e=parseInt(t.symbol.slice(1,4),16),s=u.findIndex((t=>t>e));o=x[s<0?6:s-1]}return o},exports.colors=x,exports.columnDefaults=d,exports.columnDefaultsMerge=m,exports.columns=(e,t)=>{if("string"!=typeof e)return{};const o=m(t);let s=a.text(e),n=0,l=[],i=[],c=0,p=parseInt(o.width/2),d=o.height-o.margin,u=!0,g=!1;for(let e of s){let t=r(e);n+=c,o.punctuation.spacing?n+="sign"==t.segment?o.pad:0:n+=o.pad,g=n+t.height>d,g&&"symbol"==t.segment&&o.punctuation.pull&&u&&(g=!1,u=!1),0==i.length&&(g=!1),g&&(n=o.pad,l.push(i),i=[],u=!0),i.push(Object.assign(t,{x:p+o.offset*t.lane-(500-t.minX)*t.zoom*o.style.zoom,y:n,text:e})),n+=t.height*t.zoom*o.style.zoom,c=o.punctuation.spacing?"sign"==t.segment?o.pad:o.punctuation.pad:o.pad}if(i.length&&l.push(i),o.punctuation.pull)for(let e of l){let t=e[e.length-1],s=t.y+t.height-(o.height-o.margin);if(s>0){let t=parseInt(s/e.length)+1;for(let o in e)e[o].y-=t*o+t}}let x=[];for(let e of l){let t=[p-o.offset-o.pad],s=[p+o.offset+o.pad];for(let n of e)t.push(n.x-o.pad),s.push(n.x+n.width+o.pad);t=Math.min(...t),s=Math.max(...s);let n=o.width,a=0;o.dynamic?(n=s-t,a=-t):a=p-parseInt((t+s)/2);for(let t of e)t.x+=a;x.push(n)}return{options:o,widths:x,columns:l}},exports.compose=l,exports.createTokenizer=(e=i,t=null)=>{const o=(e=>{const t={},o={},s={},n=new Set;return e.forEach((e=>{if(n.has(e.index))throw new Error(`Duplicate token index: ${e.index}`);n.add(e.index),t[e.index]=e,o[e.name]=e,s[e.value]=e})),{byIndex:t,byName:o,byValue:s,getByIndex:o=>t[o]||t[e.find((e=>"UNK"===e.name)).index],getByName:e=>o[e]||o.UNK,getByValue:e=>s[e]||o.UNK,getAllValues:()=>e.map((e=>e.value)),getAllIndices:()=>e.map((e=>e.index))}})(e),s=t??(o.getAllIndices().length>0?Math.max(...o.getAllIndices())+1:0),n=(()=>{const e=(e,t)=>Array.from({length:t-e},((t,o)=>e+o)),t=(t,o)=>e(t,o+1).map((e=>e.toString(16)));return["A","B","L","M","R","S000",...e(256,908).map((e=>`S${e.toString(16)}`)),...t(0,15).map((e=>`r${e}`)),...t(0,5).map((e=>`c${e}`)),...e(250,750).map((e=>`p${e}`))]})(),{i2s:a,s2i:l}=((e,t,o)=>{const s={},n={};return Object.values(t.byIndex).forEach((e=>{s[e.index]=e.value,n[e.value]=e.index})),e.forEach(((e,t)=>{const a=o+t;s[a]=e,n[e]=a})),{i2s:s,s2i:n}})(n,o,s),r={i2s:a,s2i:l,specialTokens:o,length:Object.keys(a).length,vocab:()=>Object.values(a),encodeTokens:e=>e.map((e=>void 0!==l[e]?l[e]:o.getByValue(e).index)),decodeTokens:e=>e.map((e=>a[e]||o.getByName("UNK").value)),encode:(e,t={})=>{const s=c(e,{...t,sep:o.getByName("SEP").value});return r.encodeTokens(s)},decode:t=>{if(0===t.length)return"";if(Array.isArray(t[0])){const o=t.map((e=>r.decodeTokens(e)));return p(o.flat(),e)}const o=r.decodeTokens(t);return p(o,e)},chunk:(e,t)=>((e,t,{cls:o="[CLS]",sep:s="[SEP]",pad:n="[PAD]"}={})=>{if(t<60)throw new Error("Chunk size must be at least 60 tokens to accommodate a typical sign");const a=[];let l=[],r=0;for(;r<e.length;){for(l=[o];r<e.length;){e[r];let o=r;for(;o<e.length&&e[o]!==s;)o++;const n=o-r+1;if(l.length+n>t-1)break;for(;r<=o;)l.push(e[r]),r++}for(;l.length<t;)l.push(n);a.push(l)}return a})(e,t,{cls:o.getByName("CLS").value,sep:o.getByName("SEP").value,pad:o.getByName("PAD").value})};return r},exports.detokenize=p,exports.group=[256,270,286,324,332,390,420,442,461,501,517,534,554,597,613,648,678,695,725,739,759,767,778,810,827,857,877,886,895,903],exports.info=r,exports.isType=(e,t)=>{const o=a.symbol(e);if(o.symbol){const e=parseInt(o.symbol.slice(1,4),16),s=g[t];if(s)return s[0]<=e&&s[1]>=e}return!1},exports.kind=[256,895,903],exports.parse=a,exports.ranges=g,exports.re=e,exports.tokenize=c;