SpellingTestImpl.mesa
Copyright Ó 1983, 1987, 1990, 1992 by Xerox Corporation. All rights reserved.
Created by Nix on September 8, 1983 11:17 am
Last Edited by: Nix, November 17, 1983 1:36 pm
Jack Kent July 24, 1987 1:26:36 pm PDT
Tim Diebert: January 26, 1987 5:06:53 pm PST
Rick Beach, May 2, 1985 3:21:23 pm PDT
Bill Jackson (bj), March 7, 1990 9:50 pm PST
Doug Terry, March 4, 1991 12:45 pm PST
Willie-s, May 22, 1992 2:28 pm PDT
DIRECTORY
PFS USING [PathFromRope, StreamOpen],
IO,
Random USING [Create, ChooseInt, RandomStream],
BitTableLookup USING [Read, Lookup, Table],
RefText USING [TrustTextAsRope],
SpellingToolServices USING [BruteForceCorrection],
Rope USING [ROPE];
SpellingTestImpl: CEDAR PROGRAM
IMPORTS PFS, IO, BitTableLookup, Random, RefText, SpellingToolServices = BEGIN
ROPE: TYPE = Rope.ROPE;
experimental: ROPE ¬ "Experimental.BitTable";
web2: ROPE ¬ "Web2.dic";
Garbage: PROC [minLength, maxLength: CARDINAL, number: INT, file: ROPE]
RETURNS [passed, failed: INT ¬ 0] = {
s: IO.STREAM ¬ PFS.StreamOpen[PFS.PathFromRope[experimental]];
trouble: IO.STREAM ¬ PFS.StreamOpen[PFS.PathFromRope[file], create];
globalBitTable: BitTableLookup.Table ¬ NIL;
length: INT;
randomSequence: Random.RandomStream ¬ Random.Create[-1, 0];
word: REF TEXT ¬ NEW[TEXT[maxLength]];
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.Put1[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] = {
s: IO.STREAM ¬ PFS.StreamOpen[PFS.PathFromRope[experimental]];
trouble: IO.STREAM ¬ PFS.StreamOpen[PFS.PathFromRope[file], create];
globalBitTable: BitTableLookup.Table ¬ NIL;
word: REF TEXT ¬ NEW[TEXT[4]];
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.PutText[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] = {
s: IO.STREAM ¬ PFS.StreamOpen[PFS.PathFromRope[experimental]];
trouble: IO.STREAM ¬ PFS.StreamOpen[PFS.PathFromRope[file], create];
globalBitTable: BitTableLookup.Table ¬ NIL;
word: REF TEXT ¬ NEW[TEXT[3]];
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.PutText[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] = {
s: IO.STREAM ¬ PFS.StreamOpen[PFS.PathFromRope[experimental]];
trouble: IO.STREAM ¬ PFS.StreamOpen[PFS.PathFromRope[file], create];
globalBitTable: BitTableLookup.Table ¬ NIL;
word: REF TEXT ¬ NEW[TEXT[2]];
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.PutText[word];
trouble.PutChar['\n];
passed ¬ passed + 1;
}
ELSE
failed ¬ failed + 1;
ENDLOOP;
ENDLOOP;
trouble.Close[];
};
Garbage1: PROC [file: ROPE] RETURNS [passed, failed: INT ¬ 0] = {
s: IO.STREAM ¬ PFS.StreamOpen[PFS.PathFromRope[experimental]];
trouble: IO.STREAM ¬ PFS.StreamOpen[PFS.PathFromRope[file], create];
globalBitTable: BitTableLookup.Table ¬ NIL;
word: REF TEXT ¬ NEW[TEXT[2]];
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.PutText[word];
trouble.PutChar['\n];
passed ¬ passed + 1;
}
ELSE
failed ¬ failed + 1;
ENDLOOP;
trouble.Close[];
};
CloseOnes: PROC [outFile: ROPE ¬ "Spelling.CloseOnes"]
RETURNS [passed, failed: INT ¬ 0] = {
bitStream: IO.STREAM ¬ PFS.StreamOpen[PFS.PathFromRope[experimental]];
listStream: IO.STREAM ¬ PFS.StreamOpen[PFS.PathFromRope[web2]];
outStream: IO.STREAM ¬ PFS.StreamOpen[PFS.PathFromRope[outFile], create];
buffer: REF TEXT ¬ NEW[TEXT[100]];
wordBuffer: REF TEXT ¬ NEW[TEXT[100]];
correctionList: LIST OF ROPE ¬ NIL;
globalBitTable: BitTableLookup.Table ¬ BitTableLookup.Read[bitStream];
WordChecker: PROC [w: REF TEXT] RETURNS [inTable: BOOL] = {
inTable ¬ globalBitTable.Lookup[w];
};
bitStream.Close[];
DO
wordBuffer ¬ GetToken[listStream, wordBuffer ! IO.EndOfStream => EXIT];
[correctionList, buffer] ¬ SpellingToolServices.BruteForceCorrection [RefText.TrustTextAsRope[wordBuffer], WordChecker, buffer];
FOR l: LIST OF ROPE ¬ correctionList, l.rest UNTIL l = NIL DO
outStream.PutRope[l.first];
outStream.PutChar['\n];
ENDLOOP;
ENDLOOP;
outStream.Close[];
listStream.Close[];
};
GetToken: PROC [s: IO.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.