2

What I am trying to do is create a Modula-2 program that takes a file and returns a number of words in the read file as well as the frequency of each word that is read. Like if a file said, "The fox fox fox jumped over the lazy dog." The output would be

The 1
fox 1
fox 2
fox 3
jumped 1
over 1
the 2
lazy 1
dog 1

9

(9 here being the total number of words.) So far I've gotten the total word counter to work, and all of the words are displayed in that fashion, but I'm stuck on how I would implement a frequency counter of each word. Here's the code.

MODULE FindWords;
FROM StreamFile IMPORT ChanId, Open, read, Close, OpenResults;
FROM TextIO IMPORT ReadString, WriteLn, WriteString, WriteChar, SkipLine;
FROM IOResult IMPORT ReadResult, ReadResults;
FROM StdChans IMPORT StdInChan, StdOutChan;
FROM WholeIO  IMPORT WriteInt, WriteCard;

TYPE line = ARRAY[0..120] OF CHAR;
 AllLines = ARRAY [1..1000] OF line;

 word = ARRAY[0..20] OF CHAR;
 WordFreq = RECORD
            freq   : INTEGER;
            aword   : word;
        END;
 AllWords = ARRAY [1..1000] OF WordFreq;

VAR
  infile, stdOut, stdIn : ChanId;
  res : OpenResults;
  inputline: line;
  i, j, k : INTEGER;
  x, y, z : INTEGER;
  charval : INTEGER;
  numlines: INTEGER;
  wordindex: INTEGER;
  numwords: INTEGER;
  document: AllLines;
  wordsindoc: AllWords;
  freqindoc: AllWords;

BEGIN
  y:=0;
  stdOut := StdOutChan();
  stdIn := StdInChan(); 

  Open (infile, "input.txt", read, res);
  IF res = opened
    THEN
      i := 1;
      REPEAT  
        ReadString (infile, inputline);
        WriteString (stdOut, inputline);
        IF ReadResult (infile) = allRight
          THEN
            SkipLine (infile);       
            document[i]:=inputline;
            WriteLn (stdOut);
            i:=i+1;
          END;  (* if *)
      UNTIL ReadResult (infile) # allRight;
     Close (infile);
     WriteLn (stdOut);
    ELSE
     WriteString (stdOut,"Sorry, couldn't open the file");
     WriteLn (stdOut);
    END; (* if *)
    numlines :=i-1; 


    (* in order to identify words in each line*)
    numwords:=0;
    wordindex := 1;
    FOR i:=1 TO numlines DO 
      j:=0; k:=0; 
      WHILE ORD(document[i][j]) <> 0
      DO
       charval := ORD(document[i][j]);
       IF ((charval >= 48) AND (charval < 58)) OR
          ((charval >= 65) AND (charval < 91)) OR
          ((charval >= 97) AND (charval < 123)) OR
          ((charval = 45)) OR ((charval=39))
         THEN 
           wordsindoc[wordindex].aword[k] := document[i][j];
           WriteChar(stdOut, wordsindoc[wordindex].aword[k]);
           k:=k+1;
         ELSE 
           WriteLn (stdOut);
           wordindex:=wordindex+1;
           numwords:=numwords+1;
           k:=0;
         END;
       j:=j+1;
      END;
    END;
    numwords:=numwords-1;
    WriteInt(stdOut, numwords, 3); 
    WriteLn(stdOut);
END FindWords.
Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
Jason
  • 23
  • 2

0 Answers0