with ada.text_io; use ada.text_io;
with ada.integer_text_io; use ada.integer_text_io;
-- Print list of unique words and their frequencies
-- A word is a line of data
procedure wf is
MaxWordLen: Constant Natural := 80;
MaxWordCount: Constant Natural := 1000;
type word is record
letters: String(1..MaxWordLen);
len: Natural := 0;
freq: Natural := 0;
end record;
type WordArray is array(Natural range <>) of Word;
type WordList is record
theWords: WordArray(1..MaxWordCount);
size: Natural := 0;
end record;
-- Define equality for words
function "="(left, right: word) return boolean is
begin
return left.letters(1 .. left.len) = right.letters(1 .. right.len);
end "=";
-- Process a word by counting it or adding it to the array
procedure processWord(w: in word; l: in out wordlist) is
found: boolean := false;
location: Natural;
-- tempword: Word;
begin
for i in 1 .. l.size loop
-- if "="(l.theWords(i), w) then -- prefix
-- if l.theWords(i)(1..l.thewords(i).len) = w(1..w.len) then
if l.theWords(i) = w then -- infix
found := true;
location := i;
end if;
end loop;
if found then
-- tempword := l.thewords(i);
-- tempword.freq := tempword.freq + 1;
-- l.thewords(i) := tempword;
l.thewords(location).freq := l.thewords(location).freq + 1;
else
-- put(l.size);-- We don't need this anymore
l.size := l.size + 1;
l.thewords(l.size) := w;
l.thewords(l.size).freq := 1;
end if;
end processWord;
-- Input and count all of the words
procedure getwords(l: out wordlist) is
w: Word;
begin
-- i := 0; -- We don't need this any more
loop
exit when end_of_file;
get_line(w.letters, w.len);
-- exit when w.len = 0; -- This was a mistake
processWord(w, l);
-- From the earlier version:
-- i := i + 1;
-- l.thewords(i) := w;
end loop;
-- l.size := i; -- This caused the error
end getwords;
procedure putwordsandfreq(l: wordlist) is
w: Word;
begin
-- put(l.size);-- We don't need this any more
for i in 1 .. l.size loop
w := l.thewords(i);
put(w.letters(1 .. w.len));
put(w.freq);
new_line;
end loop;
end putwordsandfreq;
l: wordlist;
begin
getwords(l);
-- put(l.size);-- We don't need this anymore
putwordsandfreq(l);
end wf;