SpellingTestImpl.mesa
Last Edited by: Nix, November 17, 1983 1:36 pm
Rick Beach, May 2, 1985 3:21:23 pm PDT
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]<nix>spellingtool>experimental.bittable"] RETURNS [passed, failed: INT ← 0] = {
trouble: STREAMFS.StreamOpen[file, create];
globalBitTable: BitTableLookup.Table ← NIL;
length: INT;
randomSequence: Random.RandomStream ← Random.Create[-1, 0];
word: REF TEXTNEW[TEXT[maxLength]];
s: STREAMFS.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]<nix>spellingtool>experimental.bittable";
trouble: STREAMFS.StreamOpen[file, create];
globalBitTable: BitTableLookup.Table ← NIL;
word: REF TEXTNEW[TEXT[4]];
s: STREAMFS.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]<nix>spellingtool>experimental.bittable";
trouble: STREAMFS.StreamOpen[file, create];
globalBitTable: BitTableLookup.Table ← NIL;
word: REF TEXTNEW[TEXT[3]];
s: STREAMFS.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]<nix>spellingtool>experimental.bittable";
trouble: STREAMFS.StreamOpen[file, create];
globalBitTable: BitTableLookup.Table ← NIL;
word: REF TEXTNEW[TEXT[2]];
s: STREAMFS.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]<nix>spellingtool>experimental.bittable";
trouble: STREAMFS.StreamOpen[file, create];
globalBitTable: BitTableLookup.Table ← NIL;
word: REF TEXTNEW[TEXT[2]];
s: STREAMFS.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]<SpellingToolData>Web2.dic", bitFile: ROPE ← "[ivy]<Nix>SpellingTool>Experimental.bitTable"] RETURNS [passed, failed: INT ← 0] = {
WordChecker: PROC [w: REF TEXT] RETURNS [inTable: BOOL] = {
inTable ← globalBitTable.Lookup[w];
};
buffer: REF TEXTNEW[TEXT[100]];
wordBuffer: REF TEXTNEW[TEXT[100]];
outStream: STREAMFS.StreamOpen[outFile, create];
listStream: STREAMFS.StreamOpen[listFile];
bitStream: STREAMFS.StreamOpen[bitFile];
correctionList: LIST OF ROPENIL;
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] = {
Utility for reading a token from the given stream into the given buffer.
UnPrintable: PROC [c: CHAR] RETURNS [unPrintable: BOOL] = INLINE {
How's this for device independence?
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.
CHANGE LOG
Created by Nix on September 8, 1983 11:17 am