PROGRAM read_text;

VAR
   fname : STRING[80];
   check_file : TEXT;
   lines, words, digits,
   vowels, consonants,
   punctuation, others,
   longest, length, letters,
   totchar : LONG_INTEGER;
   
   input : CHAR;
   
   word : BOOLEAN;
   
{ ---------------------------------------------------------------------- }

FUNCTION is_digit( inp : CHAR ):BOOLEAN;

{ This function returns TRUE if the input character is a digit. }

BEGIN
   IF (inp >= '0') AND (inp <= '9') THEN
      is_digit := TRUE
   ELSE
      is_digit := FALSE
END; { FUNCTION is_digit }

{ ---------------------------------------------------------------------- }

FUNCTION is_vowel( inp : CHAR ):BOOLEAN;

{ This function returns TRUE if the input character is a vowel. }

BEGIN
   CASE inp OF
      'A','E','I','O','U',
      'a','e','i','o','u' : is_vowel := TRUE;
   OTHERWISE : is_vowel := FALSE
   END
END; { FUNCTION is_vowel }

{ ---------------------------------------------------------------------- }

FUNCTION is_consonant( inp : CHAR ):BOOLEAN;

{ This function returns TRUE if the input character is a consonant. }

BEGIN
   IF (inp >= 'B') AND (inp <='Z') THEN
      is_consonant := TRUE
   ELSE
      IF (inp >= 'b') AND (inp <= 'z') THEN
         is_consonant := TRUE
      ELSE
         is_consonant := FALSE
END; { FUNCTION is_consonant }

{ ---------------------------------------------------------------------- }

FUNCTION is_punctuation( inp : CHAR ):BOOLEAN;

{ This function returns TRUE if the input character is a punctuation mark. }

BEGIN
   CASE inp OF
      ',' , ';' , '.' , '?' ,
      '!' , '"' , '-' : is_punctuation := TRUE;
      OTHERWISE : is_punctuation := FALSE
   END;
   IF inp = CHR(39) THEN
      is_punctuation := TRUE
END; { FUNCTION is_punctuation }

{ ----------------------------------------------------------------------- }

PROCEDURE check_word( inp : CHAR;
                      VAR words, length, longest : LONG_INTEGER;
                      VAR word : BOOLEAN );

{ This procedure checks to see if the user is currently typing in a word.
  It's length is counted and the longest word length is stored.
  The total number of words are also counted. }
  
BEGIN

   IF (is_vowel(inp) OR is_consonant(inp)) THEN
		length := length + 1
	ELSE
		BEGIN
			IF length >= 1 THEN words := words + 1;
			IF length > longest THEN
				longest := length;
			length := 0
		END

END; { PROCEDURE check_word }

{ ----------------------------------------------------------------------- }

PROCEDURE output_data( lines, words, digits, vowels, consonants,
                      punctuation, others, longest : LONG_INTEGER );

{ This procedure outputs the information on what was typed back to the user. }

VAR
   letters, totchar : LONG_INTEGER;
   
BEGIN
   letters := vowels + consonants;
   totchar := letters + punctuation + digits + others;
   
   { ----------------------------------------------------------- }
   
   writeln('Analysis of the text :');
   writeln;
   write('Number of lines         : ');  writeln(lines: 3);
   write('Number of words         : ');  writeln(words: 3);
   write('Number of digits        : ');  writeln(digits: 3);
   write('Number of vowels        : ');  writeln(vowels: 3);
   write('Number of consonants    : ');  writeln(consonants: 3);
   write('Number of letters       : ');  writeln(letters: 3);
   write('Punctuation marks       : ');  writeln(punctuation: 3);
   write('Other characters        : ');  writeln(others: 3);
   writeln;
   
   { ----------------------------------------------------------- }
   
   write('Average length of words : ');
   IF words <> 0 THEN
      writeln(letters / words : 3: 2)
   ELSE
      writeln('No words entered.');
      
   write('Average vowels per word : ');
   IF words <> 0 THEN
      writeln(vowels / words : 3: 2)
   ELSE
      writeln('No words entered.');
      
   writeln('Length of longest word  : ',longest: 3);

   write('Average words per line  : ');
   IF lines<>0 THEN
      writeln(words / lines: 3: 2)
   ELSE
      writeln('No lines entered.');
   
   write('Average non-blank chars : ');
   IF lines<>0 THEN
      writeln(totchar / lines : 3: 2)
   ELSE
      writeln('No lines entered.');
      
   write('Ratio vowels to letters : ');
   IF consonants<>0 THEN
      writeln(vowels / consonants: 3: 2)
   ELSE
      writeln('No consonants.');
   writeln;
   write('Press RETURN : ');
   readln;

END; { PROCEDURE output_data }

{ ----------------------------------------------------------------------- }

{ Main Routine }

BEGIN
   
   { Initialize variables }
   
   lines := 0;                longest := 0;
   length := 1;               words := 0;
   word := FALSE;             digits :=0 ;
   vowels := 0;               consonants := 0;
   punctuation := 0;          others := 0;
   

   writeln('Readtext - Text file analyzer by Paul R. Lefebvre');
	writeln('Version 1.0      7-20-90     (Written in Personal Pascal)');
	writeln;

   IF cmd_args>0 THEN
      cmd_getarg(1,fname)
   ELSE BEGIN
      write('Enter filename of text file to analyze : ');
      readln(fname);
      writeln
   END;

   reset(check_file,fname);
   
   WHILE NOT eof(check_file) DO BEGIN
      WHILE NOT eoln(check_file) DO BEGIN
         read(check_file, input);
         IF is_digit(input) THEN
            digits := digits + 1
         ELSE
            IF is_vowel(input) THEN
               vowels := vowels + 1
            ELSE
               IF is_consonant(input) THEN
                  consonants := consonants + 1
               ELSE
                  IF is_punctuation(input) THEN
                     punctuation := punctuation + 1
                  ELSE
                     IF input <> ' ' THEN
                        others := others + 1;
         check_word(input, words, length, longest, word)
      END;
	   lines := lines + 1;
      input := '*';
		check_word(input, words, length, longest, word);
	   readln(check_file)
   END;
   lines := lines - 1;   
   writeln('------------------------------------------------------');
   output_data(lines, words, digits, vowels, consonants, punctuation, others, longest);

END.
     
