Saving prolog query results in Python

Hello,

I am trying to save the prolog.query() results to a file in Python. I tried so many things but I could not find a solution for that. I would be so glad if anyone can help me with this.
prologForumSoru

Well, here i did not save yet but it already gives me something like
[{‘Q’: [[Atom(‘683909’)], [Atom(‘2168325’)], [Atom(‘1420421’)]]}, {‘Q’: [[Atom(‘683909’)], [Atom(‘2168325’)], [Atom(‘1420421’)]]}]

Can you please not use screen shots and instead paste the code so that we can use it as text. :slightly_smiling_face:

Oh,sure. Here is the code;

from pyswip import Prolog

sentences_list = [] # context list
sentence_list = []
word_list = []
results = []
with open('cumleler.txt',encoding='utf8') as sentences, open('word_list.txt', 'a') as words:

    for sentence in sentences.readlines():
        sentence_list.append(sentence)
    #print(sentence_list)
    for sentence in sentence_list:
          word_list.append(sentence.split())
    #print(word_list)

prolog = Prolog()
prolog.consult("sr_parser.pl")
for word in word_list:
    for i in word:

        try:
            answer = i
            query = list(prolog.query(f"sr_parse(['{answer}'],Q)."))
            print(query)
            #with open('answers.txt','w',encoding='utf8') as cvp:
                #cvp.write(query)
        except:
            print('failed')

Gave it to ChatGPT.

Prompt
ChatGPT May 24 Version

Modify code to

  • save the prolog.query() results to a file in Python.
from pyswip import Prolog

sentences_list = [] # context list
sentence_list = []
word_list = []
results = []
with open('cumleler.txt',encoding='utf8') as sentences, open('word_list.txt', 'a') as words:

    for sentence in sentences.readlines():
        sentence_list.append(sentence)
    #print(sentence_list)
    for sentence in sentence_list:
          word_list.append(sentence.split())
    #print(word_list)

prolog = Prolog()
prolog.consult("sr_parser.pl")
for word in word_list:
    for i in word:

        try:
            answer = i
            query = list(prolog.query(f"sr_parse(['{answer}'],Q)."))
            print(query)
            #with open('answers.txt','w',encoding='utf8') as cvp:
                #cvp.write(query)
        except:
            print('failed')

Completion

To save the prolog.query() results to a file, you can modify the code as follows:

from pyswip import Prolog

sentences_list = []  # context list
sentence_list = []
word_list = []
results = []

with open('cumleler.txt', encoding='utf8') as sentences, open('word_list.txt', 'a') as words:
    for sentence in sentences.readlines():
        sentence_list.append(sentence)
    
    for sentence in sentence_list:
        word_list.append(sentence.split())

prolog = Prolog()
prolog.consult("sr_parser.pl")

with open('answers.txt', 'w', encoding='utf8') as cvp:
    for word in word_list:
        for i in word:
            try:
                answer = i
                query = list(prolog.query(f"sr_parse(['{answer}'], Q)."))
                cvp.write(str(query) + '\n')
                results.extend(query)
            except:
                print('failed')

In this modified code, the results of each query are written to the “answers.txt” file using the cvp.write() method. The str(query) converts the query result to a string format, and '\n' is added to move to a new line after each query result. The results list is also updated to store the queries’ results if you need to use them later in your code.

I cleaned answers.txt file and changed the codes according to this answer but answers.txt still gives same result, I dont know why .

Sorry I can’t give you specific help with pyswip as I do not use it and don’t plan to learn it.

Are you aware of MQI?

Oh, there is that too? If it has better functions I should use it instead.

Thanks for your help Eric, I tried using MQI and it is working now.

1 Like

Perhaps you should share what you learned so that others can learn from your experience.

Of course. If anyone wants to work on Python, you should follow the instructions here swiplserver.prologmqi API documentation and to consult your .pl file and query, you should run the code on Python as follows;

from swiplserver import PrologMQI, PrologThread
with PrologMQI() as mqi:
               with mqi.create_thread() as prolog_thread:
                        prolog_thread.query("consult(\"your_prolog_file.pl\").")
                        prolog_thread.query("your_predicate(X,Y).")

Hope that helps!

2 Likes