Search code examples
pythoninteropipczeromqswi-prolog

2-way interop/IPC between SWI Prolog & Python


There is module to call from Python (pyswip) =to=> Prolog, but is there a way to call the other way around from Prolog =to=> Python ?

I need it so I can call Spacy-NLP module from SWI-Prolog


couldn't find if SWI supports ZeroMQ.


Solution

  • This is the sample python code in a file "c:\hello.py" that extracts the argumentq on the command line (optional). It's results are echoed to the standard output stream.

    import sys
    def hello():
        return 'hello world: ' + ','.join(sys.argv[1:])
    if __name__ == '__main__':
        print(hello())
    

    Here is the prolog program in file "c:\hello.pl" invoking the above python code as a process:

    do :-
        process_create(path('python3.7'),
                       ['c:/hello.py', foo, bar],
                       [stdout(pipe(In))]), %output stream named In
        read_string(In, Len, X), %In=input stream to read_string/3
        write(X), nl, halt.
    

    To activate this prolog/python combo and writting the results to the output stream

    $ swipl -g do c:\hello.pl
    hello world: foo,bar
    

    Does this do what you wanted?