-- Tools For the Cryptogram Solver -- This file contains stuff that might be of more general use. constant TRUE = 1, FALSE = 0 integer junk -- for message_box --------------------------------------------------------- global function isLetter(integer c) -- return 1 if letter, else 0 if (c >= 'A') and (c <= 'Z') then return(1) end if if (c >= 'a') and (c <= 'z') then return(1) end if return(0) end function -- isLetter --------------------------------------------------------- global function isHyphApos(integer c) -- return 1 if hyphen or apostrophe, else 0 if c = '\'' then return(1) end if if c = '-' then return(1) end if return(0) end function -- is HyphApos --------------------------------------------------------- global function isSOW(integer c,integer c_1,integer c_2,integer c_3) if isLetter(c_1) then return(0) elsif isHyphApos(c_1) = 0 then return(1) elsif isLetter(c_2) = 0 then return(1) end if return(0) end function -- isSOW --------------------------------------------------------- global function isEOW(integer c,integer c1,integer c2,integer c3) if isLetter(c1) then return(0) elsif isHyphApos(c1) = 0 then return(1) elsif isLetter(c2) = 0 then return(1) end if return(0) end function -- isEOW --------------------------------------------------------- function bfind(object x,sequence s) -- same as find, but binary integer left,right,middle integer comp left = 1 right = length(s) middle = 0 while left <= right do middle = floor((left+right)/2) comp = compare(x,s[middle]) if comp = 0 then return(middle) elsif comp = 1 then left = middle + 1 elsif comp = -1 then right = middle - 1 end if end while return(0) end function -- bfind --------------------------------------------------------- function bfind_insert(object x,sequence s) -- return where to insert or -1 integer left,right,middle integer comp if length(s) = 0 then return(0) end if left = 1 right = length(s) middle = 0 while left <= right do middle = floor((left+right)/2) comp = compare(x,s[middle]) if comp = 0 then return(-1) -- found it, no need to insert elsif comp = 1 then left = middle + 1 elsif comp = -1 then right = middle - 1 end if end while comp = compare(x,s[middle]) if comp = 1 then return(middle) elsif comp = -1 then return(middle - 1) else -- should never get here! end if end function -- bfind_insert --------------------------------------------------------- global function ExtractedWords(sequence s) -- return words from s integer len integer c,c1,c2,c3,c_1,c_2,c_3 integer first,last sequence words,words1,word integer bfi --integer bf words = {} for i = 1 to length(s) do len = length(s[i]) for j = 1 to len do c = s[i][j] if isLetter(c) then if j < len then c1 = s[i][j+1] else c1 = 0 end if if j < (len-1) then c2 = s[i][j+2] else c2 = 0 end if if j < (len-2) then c3 = s[i][j+3] else c3 = 0 end if if j > 1 then c_1 = s[i][j-1] else c_1 = 0 end if if j > 2 then c_2 = s[i][j-2] else c_2 = 0 end if if j > 3 then c_3 = s[i][j-3] else c_3 = 0 end if if isSOW(c,c_1,c_2,c_3) then first = j end if if isEOW(c,c1,c2,c3) then last = j word = s[i][first..last] bfi = bfind_insert(word,words) if bfi = 0 then words = prepend(words,word) elsif bfi > 0 then words1 = words[1..bfi] words1 = append(words1,word) words1 = words1 & words[bfi+1..length(words)] words = words1 end if end if end if end for end for return(words) end function -- ExtractedWords --------------------------------------------------------- global function ExtractedWords1(sequence s) -- return words from s integer c,c1,c2,first,last,eow sequence words,words1,word integer bf,bfi words = {} for i = 1 to length(s) do for j = 1 to length(s[i]) do c = s[i][j] if isLetter(c) then if (j = 1) then first = j elsif (isLetter(s[i][j-1]) = FALSE) then if (s[i][j-1] = '\'') or (s[i][j-1] = '-') then -- not new word else first = j end if end if eow = FALSE if (j = length(s[i])) then eow = TRUE else c1 = s[i][j+1] if (isLetter(c1) = 0) and (c1 != '\'') and (c1 != '-') then eow=TRUE elsif ((c1 = '\'') or (c1 = '-')) then if j = length(s[i]) - 1 then eow = TRUE elsif isLetter(s[i][j+2]) then eow = FALSE else eow = TRUE end if end if end if if eow = TRUE then last = j word = s[i][first..last] bfi = bfind_insert(word,words) if bfi = 0 then words = prepend(words,word) elsif bfi > 0 then words1 = words[1..bfi] words1 = append(words1,word) words1 = words1 & words[bfi+1..length(words)] words = words1 end if end if end if end for end for return(words) end function -- ExtractedWords1 --------------------------------------------------------- ---------------------------------------------------------