I'm new in Prolog. I have a dictionary of 660000 lines (so a text file), where every word is in a different line, like this:
hello
everyone
thanks
for
help
I have to put these words into a list because I have to verify if user's input word is in this text file. So I should have something like this:
[hello, everyone, thanks, for, help]
I tried many solutions, but due to the huge number of words (I think), the program returns Fatal Error: global stack overflow. If the following can be a solution, is there a way to read a block of words from the file and if the user's word is not in this block, clear the stack and read another block of words till the end of file to avoid stack overflow error? I'm using GNU-Prolog, not SWI-Prolog, so I can't use predicates like read_line_to_string/2. Thank you for your help!
Once you load the list of words from the file, how are you going to use it? Could it be easier to add the words as a table of facts? So, if your file is:
then you have a table of facts and you can query:
This might resolve your memory problem, too. Who knows....
But you still need to figure out how to read a line from input. On SWI-Prolog's website you see an implementation of "read line to codes"; why won't you use it? I copied it, compiled it, and it just works:
Here is how I read the first line to an atom:
Now, if you take the example code under the docs for read_line_to_codes/2, where it says "Backtrack over lines in a file", and just use that, replacing
read_line_to_codes/2
with your definition ofpl_read_line_to_codes/2
:it already works:
Does it work for 660000 words? You should try it out. When I tried with ~700K words, GNU-Prolog complained that:
I fixed this by starting GNU-Prolog like this:
Reading all ~700K words takes about 2.5 minutes on my computer, so pretty slow.