I have a sentence and its syntax, where can I find the predicates prolog that allow me to write the sentence in a logical language?
Exists or any of you can help me to create a predicate that
takes as input the following string
(S (NP (NNP John))
(VP (VBP see)
(NP (NNP Mary))))
and return this
see(John,Mary)
I think @CapelliC's answer is better (+1), but I wanted to take a crack at it and see what I came up with. You can solve this without LisProlog, but you can be quite sure that LisProlog will do a much better job than my off-the-cuff DCG crap.
First for my convenience I wanted to stick your example sentence into the code so I don't have to keep retyping it:
Now some helper DCG rules:
I need the
memberchk
in there to ensure that I don't put parenthesis into atoms.There's mutual recursion between term and list, but this is what we want, because we want a list to be able to appear nested anywhere. Let's test the parsing:
This looks good so far. The pattern matching looks very similar to @CapelliC's:
Unfortunately, using capitalized atoms is obscuring the intent here, which is actually quite natural. If we could use lowercase letters the pattern would look more like this:
We're using the powerful "univ" operator here (
=..
) to generate a Prolog term from a list. You can think of this operator as the Lisp translator, because that's essentially what it does:So it should be clear how the pattern matching works. Let's see how the whole thing fits together:
For what it's worth, translating natural language sentences into logical propositions is a big question. It won't usually be this easy, but there are books that discuss how to approach the problem. I'd suggest checking out Representation and Inference for Natural Language for more information on this large problem.
So there you have it. The advantage to doing it by hand like this is that you can control what exactly from Lisp's grammar you want to take, and it's easy to extend or modify. The downside is that you will have to debug the grammar—and I'm quite sure there are problems with this one I didn't notice or take time to find (I am far from a DCG expert!). Absent better requirements I would definitely take @CapelliC's answer, but I thought it might be helpful to see how this could be approached from scratch.
If I understand, you want parse LISP structures to Prolog. Then LisProlog, by Markus Triska, could be the appropriate tool. Using that and this snippet
I get
Now you should decide about translating the logical form to Prolog term.
In generality, it's a difficult theme, because you need to do some choice about quantification. For your simple statement, you could begin with a very simple pattern matching, ignoring altogether the problem above.
test: