with(linalg): polychart:= [ [`05`,`18`,`26`,`38`,`45`,`54`,`62`,`84`], [`10`], [`28`,`06`,`80`], [`24`,`46`,`85`,`88`], [`15`,`16`,`23`,`31`,`44`,`61`,`69`,`77`,`83`,`87`,`91`,`95`], [`02`,`32`], [`17`,`52`], [`03`,`09`,`33`,`76`,`82`,`89`], [`27`,`47`,`66`,`73`,`74`,`81`,`90`], [`11`], [`43`], [`19`,`37`,`48`,`68`], [`00`,`35`], [`20`,`36`,`53`,`65`,`97`,`98`], [`22`,`30`,`34`,`60`,`64`,`67`,`72`], [`04`,`39`], [`59`], [`08`,`56`,`57`,`71`,`79`,`92`], [`21`,`42`,`49`,`63`,`70`,`94`], [`12`,`50`,`51`,`55`,`75`,`78`,`86`,`93`,`96`,`99`], [`29`,`01`,`58`], [`14`], [`13`,`25`], [`41`], [`40`], [`07`] ]: numbers:=proc(s) local alph,v,n,m,i,c; alph:=`abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ`; n:=length(s); v:=[]; for i from 1 to n do c:=substring(s,i..i); m:=searchtext(c,alph); if m > 0 then v:=[op(v), (m -1 mod 26) + 1] fi; od; RETURN(v) end: letters:=proc(v) local alph,i,msg; alph:=`ABCDEFGHIJKLMNOPQRSTUVWXYZ`; msg:=``; for i from 1 to nops(v) do msg:=cat(msg,substring(alph,v[i]..v[i]) ); od; RETURN(msg); end: upcase:=w->letters(numbers(w)): homophoneenc:=proc(s) local v,ans,n,i,m,x,randfunc; global polychart; randfunc:=[seq(rand(1..x), x=1..12)]; v:=numbers(s); n:=nops(v); ans:=``; for i from 1 to n do m:=randfunc[nops(polychart[v[i]])](); ans:=cat(ans, polychart[v[i]][m] ); od; RETURN(ans) end: homophonedec:=proc(s) local alph,v,n,i,c,k,l; global polychart; alph:=`ABCDEFGHIJKLMNOPQRSTUVWXYZ`; n:=length(s); v:=``; for i from 1 to n/2 do c:=substring(s,(2*i-1)..(2*i)); k:=select(has,polychart,c); member(op(k),polychart,'l'); v:=cat(v, substring(alph,l..l) ); od; RETURN(v) end: hfreq:=proc(s) local v,n,i,ans,bigram,x,y; n:=length(s); v:=sort([seq(substring(s,(2*i-1)..(2*i)),i=1..(n/2))]); ans:=[]; bigram:=v[1]; y:=1; for x from 2 to n/2 do if v[x] = bigram then y:=y+1; continue; else ans:=[op(ans),[bigram,y]]; bigram:=v[x]; y:=1; fi; od; ans:=[op(ans),[bigram,y]]; RETURN(ans) end: hfreqsort:=proc(s) local A,x,y; A:=hfreq(s); sort( A, (x,y)->evalb(x[2]>y[2])) end: hpairfreq:=proc(s) local v,n,i,ans,bigram,x,y; n:=length(s); v:=sort([seq(substring(s,(2*i-1)..(2*i+2)),i=1..(n/2)-1)]); ans:=[]; bigram:=v[1]; y:=1; for x from 2 to (n/2)-1 do if v[x] = bigram then y:=y+1; continue; else ans:=[op(ans),[bigram,y]]; bigram:=v[x]; y:=1; fi; od; ans:=[op(ans),[bigram,y]]; RETURN(ans) end: hpairfreqsort:=proc(s) local A,x,y; A:=hpairfreq(s); sort( A, (x,y)->evalb(x[2]>y[2])) end: readin:=proc(filename) local fd,q,ans,s; fd:=fopen(filename,READ,TEXT); q:=fscanf(fd,`%s`); ans:=``; while q<>0 do s:=upcase(op(q)); ans:=cat(ans,s); q:=fscanf(fd,`%s`); od; fclose(fd); RETURN(ans); end: