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];
=
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: 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]<nix>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]<nix>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]<nix>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]<nix>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]<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 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] = {
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;
};