%begin
%const %string(255) Ident = "VetDict V0.0 (c) Graham Toal 1986"
%external %integer %fn %spec XName(%string(255) %name prog name)
%string (255) program name

%routine help
%integer string length, j
  string length = XName(program name)
  %for j = string length, -1, 1 %cycle
    charno(program name, j) = charno(program name, j-1)
  %repeat
  length(program name) = string length

  printstring("Usage: ".program name." dictdir".nl)
%end

%include "dictutils-imp"

%record %format dict fm(%byte %array ch(0:2000000))
%record (dict fm) %name %array dict(1:max word length)
%integer %array next free(1:max word length)
%integer word length, text length, ch, this word

  Decode standard params
  Printstring(program name." ".in param1.",".in param2." ".output param.nl) %c
    %if verbosity > 0

  Preload dicts(output param)

  %for word length = 1,1,max word length %cycle
    %if dict loaded(word length)=true %start
      dict(word length) == RECORD(dict start(word length))
      next free(word length) = dict next free(word length)-dict start(word length)
    %else
      dict(word length) == RECORD(0)
      next free(word length) = 0
    %finish
  %repeat

  ! Dict_ch(0:next free-1) contains the word list.

! #$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#


%external %integerfn %spec XOsByte(%integername y,c, %integer a,x,yin)

%routine pr(%byte ch)
  *movb_ch,1
  *svc_1
%end

%routine printer on
%integer Y, C, x = XOsByte(Y, C, 3, 0, 0)
  pr(2)
%end

%routine printer off
  pr(3)
%end

%routine b(%integer i)
%integer x, y, c, j
  i = i&255
  %if i=0 %start
    x = XOsByte(Y, C, 6, 255, 0)
    pr(1); pr(i)
    x = XOsByte(Y, C, 6, 0, 0)
  %else
    pr(1); pr(i)
  %finish
%end

%routine esc(%integer i)
  b(27); b(i)
%end

%routine litchar(%byte i)
  esc('I');b(1);b(i);esc('I');b(0)
%end


%routine print word(%byte %array %name s, %integer i)
%for i = 0,1,i-1 %cycle
  b(s(i))
%repeat
%end

%routine Correct(%byte %array %name ch, %integer word length)
%integer pos,original,alternative,i,printed
%byte %array ch2,ch3(0:max word length)

%predicate same(%bytearrayname s1,s2)
%integer i
%for i=0,1,word length-1 %cycle
  %if s1(i)#s2(i) %then %false
%repeat
%true
%end

  printed=false
  %return %if word length < 5
  ! Single char typos
  %for pos = 0,1,word length-1 %cycle
    ch2(pos)=ch(pos); ch3(pos)=ch(pos)
  %repeat
  %for pos = 1, 1, word length-2 %cycle
    original = ch3(pos)
    %for alternative = 'a',1,'z' %cycle
      ch3(pos)=alternative
      %if spell check(ch3, word length,
                      dict(word length)_ch, next free(word length)) %start
        %if %not same(ch3,ch2) %start
          %if printed=false %start
            print word(ch2, word length); printed=true
          %finish
          b('=');print word(ch3, word length)
        %finish
      %finish
    %repeat
    ch3(pos) = original
  %repeat
  ! transposition errors
  %for pos = 1, 1, word length-2 %cycle
    ! Swap(pos,pos+1)
    alternative = ch3(pos)
    ch3(pos) = ch3(pos+1)
    ch3(pos+1) = alternative
    %if spell check(ch3, word length,
                    dict(word length)_ch, next free(word length)) %start
      %if %not same(ch3,ch2) %start
        %if printed=false %start
          print word(ch2, word length); printed=true
        %finish
        b('=');print word(ch3, word length)
      %finish
    %finish
    ! Swap(pos,pos+1)
    alternative = ch3(pos)
    ch3(pos) = ch3(pos+1)
    ch3(pos+1) = alternative
  %repeat
  ! Letter missing {assumed not first or last letter}
  %for pos=0,1,word length-2 %cycle
    ! insert char between pos and pos+1
    %for i = 0,1,pos %cycle
      ch2(i)=ch(i)
    %repeat
    %for i = pos+1,1,word length %cycle
      ch2(i+1)=ch(i)
    %repeat
    %for alternative = 'a',1,'z' %cycle
      ch2(pos+1)=alternative
      %if spell check(ch2, word length+1,
                      dict(word length+1)_ch, next free(word length+1)) %start
        %if %not same(ch3,ch2) %start
          %if printed=false %start
            print word(ch, word length); printed=true
          %finish
          b('=');print word(ch2, word length+1)
        %finish
      %finish
    %repeat
  %repeat
  ! Letter inserted
  %for pos = 1,1,word length-2 %cycle
    ! remove char at pos
    %for i = 0,1,pos-1 %cycle
      ch2(i)=ch(i)
    %repeat
    %for i = pos+1,1,word length %cycle
      ch2(i-1)=ch(i)
    %repeat
    %if spell check(ch2, word length-1,
                    dict(word length-1)_ch, next free(word length-1)) %start
      %if %not same(ch3,ch2) %start
        %if printed=false %start
          print word(ch, word length); printed=true
        %finish
        b('=');print word(ch2, word length-1)
      %finish
    %finish
  %repeat
  ! Complex transposition (anagram) ?????
  %if printed=true %then b(nl) %and b(13)
%end


! #$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$#
%record (dict fm) %name temp
printer on
esc(51);b(16);esc(80);b(15);esc(83);b(48)
esc('U');b(1);esc('s');b(1)
%for word length = 5,1,max word length %cycle
  %if dict loaded(word length)=true %start
    this word = 0
    %cycle
      temp == RECORD(ADDR(dict(word length)_ch(this word)))
      Correct(temp_ch, word length)
      this word=this word+(word length+1)
    %repeat %until this word>=next free(word length)
  %finish
%repeat

printer off

terminate

%endofprogram
