GithubHelp home page GithubHelp logo

Can't query lists about pyswip HOT 4 CLOSED

manraog avatar manraog commented on May 26, 2024
Can't query lists

from pyswip.

Comments (4)

nschllr avatar nschllr commented on May 26, 2024

I have the same problem:

det(the).
adjective(brown).
noun(fox).
generate_output(X) :- maplist(call,[det,adjective,noun],X).

In swiprolog the query works fine. But if I make a query with

result = list(prolog.query('generate_output(X)'))
print(result)

The output is:

[{'X': [Atom('396421'), Atom('397317'), Atom('393093')]}, ....

I work with swipl 7.6.0-rc2, python 3.6.2 and pyswip 0.2.3.

@manraog did you find a workaround?

from pyswip.

Vimos avatar Vimos commented on May 26, 2024

I am facing a similar problem,


In [1]: from pyswip import Prolog, registerForeign

In [2]: prolog = Prolog()

In [3]: prolog.consult("geoquery")
Warning: /home/vimos/git/visualjoyce/zoonlp/data/geoquery/geoquery:57:
	Singleton variables: [R]
Warning: /home/vimos/git/visualjoyce/zoonlp/data/geoquery/geoquery:348:
	Local definition of user:subtract/3 overrides weak import from lists

In [4]: for l in open('geoqueries250'):
   ...:     if l.strip():
   ...:         try:
   ...:             a = list(prolog.query("answer{}".format(l.strip().split(', answer')[1])[:-2])) 
   ...:             print(a)
   ...:         except:
   ...:             pass
   ...:         


Answer = [arkansas,canadian,cimarron,colorado,gila,green,neosho,north platte,pecos,red,republican,rio grande,san juan,smoky hill,south platte,washita]
[{'A': Variable(67), 'B': Variable(70), 'C': Variable(73)}]


Answer = [granite peak]
[{'A': Variable(67), 'B': Variable(70)}]

I used the geoquery dataset, the output for a is
[{'A': Variable(67), 'B': Variable(70)}]

I tried to debug, I found that if we run examples from the repo, the printed mapped terms are

/opt/anaconda/anaconda3/bin/python3.6 /home/vimos/git/pyswip/examples/create_term.py
mappedTerms {}
mappedTerms {}
mappedTerms {}
mappedTerms {70: Atom('361349')}
mappedTerms {70: Atom('361349'), 71: Atom('360709'), 69: Functor(373005,2,X,gnu)}
mappedTerms {70: Atom('361349'), 71: Atom('360709'), 69: Functor(373005,2,X,gnu)}
mappedTerms {70: Atom('361349'), 71: Atom('360709'), 69: Functor(373005,2,X,gnu), 73: Atom('361477')}
[{'X': 'gnu', 'Y': 50}]

But for the geoquery case, it will be

/opt/anaconda/anaconda3/bin/python3.6 /home/vimos/git/pyswip/examples/geoquery.py
Warning: /home/vimos/git/visualjoyce/zoonlp/data/geoquery/geoquery:57:
	Singleton variables: [R]
mappedTerms {}


Answer = [arkansas,canadian,cimarron,colorado,gila,green,neosho,north platte,pecos,red,republican,rio grande,san juan,smoky hill,south platte,washita]
mappedTerms {63: []}
mappedTerms {63: []}
mappedTerms {63: []}
mappedTerms {63: [], 66: Atom('405637')}
mappedTerms {63: [], 66: Atom('405637'), 67: Variable(67)}
mappedTerms {63: [], 66: Atom('405637'), 67: Variable(67), 65: Functor(373005,2,A,_G1073)}
mappedTerms {63: [], 66: Atom('405637'), 67: Variable(67), 65: Functor(373005,2,A,_G1073)}
mappedTerms {63: [], 66: Atom('405637'), 67: Variable(67), 65: Functor(373005,2,A,_G1073), 69: Atom('517893')}
mappedTerms {63: [], 66: Atom('405637'), 67: Variable(67), 65: Functor(373005,2,A,_G1073), 69: Atom('517893'), 70: Variable(70)}
mappedTerms {63: [], 66: Atom('405637'), 67: Variable(67), 65: Functor(373005,2,A,_G1073), 69: Atom('517893'), 70: Variable(70), 68: Functor(373005,2,B,_G1076)}
mappedTerms {63: [], 66: Atom('405637'), 67: Variable(67), 65: Functor(373005,2,A,_G1073), 69: Atom('517893'), 70: Variable(70), 68: Functor(373005,2,B,_G1076)}
mappedTerms {63: [], 66: Atom('405637'), 67: Variable(67), 65: Functor(373005,2,A,_G1073), 69: Atom('517893'), 70: Variable(70), 68: Functor(373005,2,B,_G1076), 72: Atom('505989')}
mappedTerms {63: [], 66: Atom('405637'), 67: Variable(67), 65: Functor(373005,2,A,_G1073), 69: Atom('517893'), 70: Variable(70), 68: Functor(373005,2,B,_G1076), 72: Atom('505989'), 73: Variable(73)}
[{'A': Variable(67), 'B': Variable(70), 'C': Variable(73)}]

After some debuging, the problem of showing Atom may be fixed by changing _unifier function to:

def _unifier(arity, *args):
    assert arity == 2
    #if PL_is_variable(args[0]):
    #    args[0].unify(args[1])

    if hasattr(args[0], 'value'):
        args_0 = args[0].value
    else:
        args_0 = args[0]

    if hasattr(args[1], 'value'):
        args_1 = args[1].value
    else:
        args_1 = args[1]

    if type(args_1) is list:
        for i, item in enumerate(args_1):
            if hasattr(item, 'value'):
                args_1[i] = item.value

    return {args_0: args_1}

But this does not fix the Variable case.
I found a topic on stackoverflow, maybe Variable case is not easy to resolve.

from pyswip.

yuce avatar yuce commented on May 26, 2024

This is fixed in the rewrite/refactoring of PySwip in the refactor branch. E.g.,: https://github.com/yuce/pyswip/blob/refactor/tests/test_prolog.py

from pyswip.

ShubhamSharma1808 avatar ShubhamSharma1808 commented on May 26, 2024

Cant access the above link, can you share the updated link @yuce

from pyswip.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.