Rules for building a prologue from atoms

I'm currently trying to interpret user input strings through Prolog. I am using code that I found on the Internet that converts a string to a list of atoms.

"Men are stupid." => [men,are,stupid,'.'] % Example

From this, I would like to create a rule that can then be used on the Prolog command line.

% everyone is a keyword for a rule. If the list doesn't contain 'everyone'
% it a fact.

% [men,are,stupid]
% should become ...
stupid(men).

% [everyone,who,is,stupid,is,tall]
% should become ...
tall(X) :- stupid(X).

% [everyone,who,is,not,tall,is,green]
% should become ...
green(X) :- not(tall(X)).

% Therefore, this query should return true/yes:
?- green(women).
true.

I do not need anything supernatural for this, since my entry will always follow several rules and therefore just need to be analyzed in accordance with these rules.

I probably thought about this for an hour, but did not reach something even significant, so I can not provide you with what I have tried so far. Can someone push me in the right direction?

+4
source share
3

DCG. :

list_clause(List, Clause) :-
    phrase(clause_(Clause), List).

clause_(Fact)         --> [X,are,Y], { Fact =.. [Y,X] }.
clause_(Head :- Body) --> [everyone,who,is,B,is,A],
    { Head =.. [A,X], Body =.. [B,X] }.

:

?-  list_clause([men,are,stupid], Clause).
Clause = stupid(men).

?- list_clause([everyone,who,is,stupid,is,tall], Clause).
Clause = tall(_G2763):-stupid(_G2763).

.

assertz/1 :

?- List = <your list>, list_clause(List, Clause), assertz(Clause).
+4

, . " ".

- :

?- assert_rule_from_sentence("Men are stupid.").

stupid(men).

assert_rule_from_sentence(Sentence) :-
    phrase(sentence_to_database, Sentence).

sentence_to_database -->
    subject(Subject), " ",
    "are", " ",
    object(Object), " ",
    {   Rule =.. [Object, Subject],
        assertz(Rule)
    }.

(, , DCG )

! , sentence_to_database//0 , , , .

@mat, , . :

tokenize_sentence(be(Subject, Object)) -->
    subject(Subject), space,
    be, !,
    object(Object), end.

( , ...)

be -->
    "is".
be -->
    "are".

assert_tokenized(be(Subject, Object)) :-
    Fact =.. [Object, Subject],
    assertz(Fact).

, , : subject - verb - object subject - modifier - object - modifier .., assert_tokenized/1 .

+2

All Articles