hey all, i've got this task to do approve one of the pascal subjects in my university. honestly my pc broke down so lost like 2 weeks there, then on top of that add the fact that I also work so my time was very limited to make it.
they ask us to make a few procedures and functions, for a simple text prediction program. This program is fed a text and it will go through 2 words at once, it will hash the first word, and take in the second word.
The hash is the index of an array of words (a node list) in which all the words that come after the hashed word will be shown, if there are repeated ones, it will show up as one occurence with quantity of 2, they have a counter variable for this.
like this, lets focus on the world hello, but remember it does for every word pair.
hello whats your name beautiful. hello there. hello whats your name
for the first 2 words, 'hello' and 'whats'
hash hello = 11437
feed 'whats'
if the list at index 11437 is empty, add 'whats' to it, with quantity=1
for the worlds 'hello' and 'there'
hash hello = 11437
feed 'there'
in this case the word 'there' is compared with the words in the list at index 11437, one by one till it reaches the end of the list, if 'there' doesnt exist in the list, it will be added to the end of it, with quantity = 1
...
words 'hello' and 'whats' again
hash hello = 11437
feed 'whats'
now it will compare 'whats' with the words in the list 11437, but now, instead of adding it again, it will add 1 to the counter of the word 'whats'
ignoring all the other words, the program output should be
(11437) -(whats,2),(there,1)
for some reason my program works only if all the words in the text are different. If there is just one repeated word it will not add it to the existing indexed list and it wont show this index in the terminal at all.
We were asked to program the functions that 1) hash the word 2) compare a pair of words, 3) add it to the list, 4) initialize the indexed list (just adding nil to all the pointers) 5) run the program
I have tried to trace the problem and it goes to the function that adds nodes to the list, specially when it has to add one to a non empty list ie when you come across the same words in the text, but im just lost on why this is happening...this is the function if anyone wants to take a look.
the file with all the other functions and procedures is in the attachment, this is the one that first checks if list is empty and if it isnt then it will compare, then depending on this we will either add the word to the list or add 1 to the counter.
procedure agregarOcurrencia(p:palabra; var pals:Ocurrencias);
var
paux,pnueva:Ocurrencias;
comp:Comparacion;
i:integer;
begin
i:=1;
paux:=pals;
if pals=NIL then
begin
new(pnueva);
pnueva^.palc.pal:=p;
pnueva^.palc.cant:=1;
pnueva^.sig:=nil;
pals:=pnueva;
i:=4
end;
while (pals<>NIL) and (i<3) do
begin
comp:=comparaPalabra(pals^.palc.pal,p);
if comp=igual then // if theyre equal we add +1 to the counter
begin
pals^.palc.cant:=pals^.palc.cant+1;
i:=5
end
else if comp<>igual then //if they're different all the way to the end we use i as a flag to modify the list
begin
i:=2
end;
pals:=pals^.sig
end;
if i=2 then // this adds the current word p to the end of the list pals
begin
writeln('añado elemento');
new(pnueva);
pnueva^.palc.pal:=p;
pnueva^.palc.cant:=1;
pnueva^.sig:=nil;
while paux^.sig<>NIL do
paux:=paux^.sig;
paux^.sig:=pnueva;
end;
end;