<> <> <> DIRECTORY FS USING [StreamOpen], IO USING [Close, STREAM, text, Put, PutChar, GetChar, EndOfStream, Backup, rope], Random USING [Create, ChooseInt, RandomStream], BitTableLookup USING [Read, Lookup, Table], RefText USING [TrustTextAsRope], SpellingCorrection USING [BruteForceCorrection], Rope USING [ROPE]; SpellingTestImpl: CEDAR PROGRAM IMPORTS FS, IO, BitTableLookup, Random, RefText, SpellingCorrection = BEGIN ROPE: TYPE = Rope.ROPE; STREAM: TYPE = IO.STREAM; Garbage: PROC [minLength, maxLength: CARDINAL, number: INT, file: ROPE, bitFile: ROPE _ "[ivy]spellingtool>experimental.bittable"] RETURNS [passed, failed: INT _ 0] = { trouble: STREAM _ FS.StreamOpen[file, create]; globalBitTable: BitTableLookup.Table _ NIL; length: INT; randomSequence: Random.RandomStream _ Random.Create[-1, 0]; word: REF TEXT _ NEW[TEXT[maxLength]]; s: STREAM _ FS.StreamOpen[bitFile]; globalBitTable _ BitTableLookup.Read[s]; s.Close[]; FOR i: INT IN [0..number) DO length _ Random.ChooseInt[randomSequence, minLength, maxLength]; word.length _ length; FOR j: INT IN [0..length) DO word[j] _ 'a + Random.ChooseInt[randomSequence, 0, 25]; ENDLOOP; IF globalBitTable.Lookup[word] THEN { trouble.Put[IO.text[word]]; trouble.PutChar['\n]; passed _ passed + 1; } ELSE failed _ failed + 1; ENDLOOP; trouble.Close[]; }; Garbage4: PROC [file: ROPE] RETURNS [passed, failed: INT _ 0] = { bitFile: ROPE _ "[ivy]spellingtool>experimental.bittable"; trouble: STREAM _ FS.StreamOpen[file, create]; globalBitTable: BitTableLookup.Table _ NIL; word: REF TEXT _ NEW[TEXT[4]]; s: STREAM _ FS.StreamOpen[bitFile]; globalBitTable _ BitTableLookup.Read[s]; s.Close[]; word.length _ 4; FOR c0: CHAR IN ['a..'z] DO FOR c1: CHAR IN ['a..'z] DO FOR c2: CHAR IN ['a..'z] DO FOR c3: CHAR IN ['a..'z] DO word[0] _ c0; word[1] _ c1; word[2] _ c2; word[3] _ c3; IF globalBitTable.Lookup[word] THEN { trouble.Put[IO.text[word]]; trouble.PutChar['\n]; passed _ passed + 1; } ELSE failed _ failed + 1; ENDLOOP; ENDLOOP; ENDLOOP; ENDLOOP; trouble.Close[]; }; Garbage3: PROC [file: ROPE] RETURNS [passed, failed: INT _ 0] = { bitFile: ROPE _ "[ivy]spellingtool>experimental.bittable"; trouble: STREAM _ FS.StreamOpen[file, create]; globalBitTable: BitTableLookup.Table _ NIL; word: REF TEXT _ NEW[TEXT[3]]; s: STREAM _ FS.StreamOpen[bitFile]; globalBitTable _ BitTableLookup.Read[s]; s.Close[]; word.length _ 3; FOR c0: CHAR IN ['a..'z] DO FOR c1: CHAR IN ['a..'z] DO FOR c2: CHAR IN ['a..'z] DO word[0] _ c0; word[1] _ c1; word[2] _ c2; IF globalBitTable.Lookup[word] THEN { trouble.Put[IO.text[word]]; trouble.PutChar['\n]; passed _ passed + 1; } ELSE failed _ failed + 1; ENDLOOP; ENDLOOP; ENDLOOP; trouble.Close[]; }; Garbage2: PROC [file: ROPE] RETURNS [passed, failed: INT _ 0] = { bitFile: ROPE _ "[ivy]spellingtool>experimental.bittable"; trouble: STREAM _ FS.StreamOpen[file, create]; globalBitTable: BitTableLookup.Table _ NIL; word: REF TEXT _ NEW[TEXT[2]]; s: STREAM _ FS.StreamOpen[bitFile]; globalBitTable _ BitTableLookup.Read[s]; s.Close[]; word.length _ 2; FOR c0: CHAR IN ['a..'z] DO FOR c1: CHAR IN ['a..'z] DO word[0] _ c0; word[1] _ c1; IF globalBitTable.Lookup[word] THEN { trouble.Put[IO.text[word]]; trouble.PutChar['\n]; passed _ passed + 1; } ELSE failed _ failed + 1; ENDLOOP; ENDLOOP; trouble.Close[]; }; Garbage1: PROC [file: ROPE] RETURNS [passed, failed: INT _ 0] = { bitFile: ROPE _ "[ivy]spellingtool>experimental.bittable"; trouble: STREAM _ FS.StreamOpen[file, create]; globalBitTable: BitTableLookup.Table _ NIL; word: REF TEXT _ NEW[TEXT[2]]; s: STREAM _ FS.StreamOpen[bitFile]; globalBitTable _ BitTableLookup.Read[s]; s.Close[]; word.length _ 2; FOR c0: CHAR IN ['a..'z] DO word[0] _ c0; IF globalBitTable.Lookup[word] THEN { trouble.Put[IO.text[word]]; trouble.PutChar['\n]; passed _ passed + 1; } ELSE failed _ failed + 1; ENDLOOP; trouble.Close[]; }; CloseOnes: PROC [outFile: ROPE _ "Spelling.CloseOnes", listFile: ROPE _ "[Indigo]Web2.dic", bitFile: ROPE _ "[ivy]SpellingTool>Experimental.bitTable"] RETURNS [passed, failed: INT _ 0] = { WordChecker: PROC [w: REF TEXT] RETURNS [inTable: BOOL] = { inTable _ globalBitTable.Lookup[w]; }; buffer: REF TEXT _ NEW[TEXT[100]]; wordBuffer: REF TEXT _ NEW[TEXT[100]]; outStream: STREAM _ FS.StreamOpen[outFile, create]; listStream: STREAM _ FS.StreamOpen[listFile]; bitStream: STREAM _ FS.StreamOpen[bitFile]; correctionList: LIST OF ROPE _ NIL; globalBitTable: BitTableLookup.Table _ BitTableLookup.Read[bitStream]; bitStream.Close[]; DO wordBuffer _ GetToken[listStream, wordBuffer ! IO.EndOfStream => EXIT]; [correctionList, buffer] _ SpellingCorrection.BruteForceCorrection[RefText.TrustTextAsRope[wordBuffer], WordChecker, buffer]; FOR l: LIST OF ROPE _ correctionList, l.rest UNTIL l = NIL DO outStream.Put[IO.rope[l.first]]; outStream.PutChar['\n]; ENDLOOP; ENDLOOP; outStream.Close[]; listStream.Close[]; }; GetToken: PROC [s: STREAM, word: REF TEXT] RETURNS [w: REF TEXT] = { <> UnPrintable: PROC [c: CHAR] RETURNS [unPrintable: BOOL] = INLINE { <> unPrintable _ c IN ['\000 .. '\040]; }; c: CHAR; w _ word; WHILE UnPrintable[c _ s.GetChar[]] DO ENDLOOP; s.Backup[c]; w.length _ w.maxLength; FOR i: CARDINAL _ 0, i+1 DO c _ s.GetChar[ ! IO.EndOfStream => GOTO done]; IF UnPrintable[c] THEN GOTO done; IF i >= w.maxLength THEN { w _ NEW[TEXT[2*w.maxLength+1]]; w.length _ w.maxLength; }; w[i] _ c; REPEAT done => w.length _ i; ENDLOOP; }; END. <<>> <> <>