Intro
 
Lession 1
 
Lession 2
 
Lession 3
 
Lession 4
 
Lession 5
 
Lession 6
 
Lession 7
 
Lession 8
 
Lession 9
 
Lession 10
 
Lession 11
 
Lession 12
 
Lession 13
 
Lession 14
 
Lession 15
 
Lession 16
 
Lession 17
 
Lession 18
 
Lession 19
 
Contact
 
 

 
Home | Text version
 
Download swi-prolog | Download these scripts

Learn Definite Clause Grammars (DCG) with Toki Pona

Lession 2

Now we add code for the lesson Direct Objects, Compound Sentences.

It is better to put the confusing code of "check_grammar" in a separate file "check_grammar.pro" (see at the end of this page) and include this file:

:- ['check_grammar.pro']. 

We complete the word list with the vocabulary of this lesson. The most words in Toki Pona can be different word types. For example "moku" can be a noun or an transitive verb. So we have to put it in both lists.

New are the rules for "direct_objects" and "direct_object". It is possible to have no direct objects:

direct_objects          --> [].

Or it is possible to have at least on direct object ("direct_object" without "s" at the end) or more than one ("direct_objects"). We define recursivly "direct_objects". Recursiv programming is very importand in Prolog.

direct_objects          --> direct_object,        direct_objects.  

Now we have to define "direct_object". It can be consist an "e" and a noun or an "e" and a pronoun.

direct_object           --> separator(e),         noun(_).
direct_object           --> separator(e),         pronoun(_).

Here is the whole code snippet:

direct_objects          --> [].                                                                                  % no direct object
direct_objects          --> direct_object,        direct_objects.   
direct_object           --> separator(e),         noun(_).
direct_object           --> separator(e),         pronoun(_).

Nearly the same are the definitions of the rules for "verb_phrases", "verb_phrase" and "verb_phrase_add". Try to find out why they are a bit more complicated. In Toki Pona additional verb phrases need everytime the separator "li".

Some verbs can be used before an other verb, for example "wile".

verb_phrase              --> verb_pre(Verb_pre),  verb_transitive(Verb), direct_objects.

Here is the source code of toki-pona-02.pro:


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Learn Definite Clause Grammars with Toki Pona
% Toki Pona - Lession 02 - Direct Objects, Compound Sentences
% by Robert Warnke http://rowa.giso.de
% released under the GNU General Public License 
%
% ['toki-pona-02.pro'].
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Grammar 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% sentence types:

sentence                 --> sentence_declarative, separator('.').       % A declarative sentence states a fact or an argument and ends with a full stop.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% definitions of the sentence types:

sentence_declarative     --> noun_phrase, verb_phrases.                  % A declarative sentence consist of a noun phrase and verb phrases (plural!). 


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%  phrases:

noun_phrase              --> pronoun(mi).
noun_phrase              --> pronoun(sina).
noun_phrase              --> pronoun(ona),        separator(li).
noun_phrase              --> pronoun(ni),         separator(li).
noun_phrase              --> noun(_),             separator(li).

verb_phrases             --> verb_phrase,         verb_phrase_add.
verb_phrase              --> noun(_).                                                                            % In Toki Pona is no verb "to be". 
verb_phrase              -->                      verb_transitive(_),                            direct_objects.
verb_phrase              --> verb_pre(Verb_pre),  verb_transitive(Verb),                         direct_objects.
verb_phrase_add          --> [].
verb_phrase_add          --> separator(li),       verb_phrase,          verb_phrase_add.


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% objects:

direct_objects          --> [].                                                                                  % no direct object
direct_objects          --> direct_object,        direct_objects.   
direct_object           --> separator(e),         noun(_).
direct_object           --> separator(e),         pronoun(_).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Words 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

noun(Noun) --> [Noun], { member(Noun, [   
     ijo,          % thing, something, stuff, anything, object
     ilo,          % tool, device, machine, thing used for a specific purpose
     jan,          % person, people, human, being, somebody, anybody
     jo,           % having
     kili,         % fruit, pulpy vegetable, mushroom
     lukin,        % view, look, glance, sight, gaze, glimpse, seeing, vision, sight 
     ma,           % land, earth, country, (outdoor) area
     moku,         % food, meal
     pakala,       % blunder, accident, mistake, destruction, damage, breaking
     pipi,         % bug, insect, spider
     pona,         % good, simplicity, positivity
     suli,         % size
     suno,         % sun, light
     telo,         % water, liquid, juice, sauce
     unpa          % sex, sexuality
     % wile          % desire, need, will
 ])
}.
pronoun(Pronoun) --> [Pronoun], { member(Pronoun, [
     mi,          % I, we (pronoun 1. person)
     sina,        % you (pronoun 2. person)
     ona,         % she, he, it, they (pronoun 3. person)
     ni           % this, that (pronoun determiner)
 ])
}.
separator(Separator) --> [Separator], { member(Separator, [
     e,             % introduces a direct object
     li,            % between any subject except mi and sina and its verb
     '.'            % At the end of a sentence.
 ])
}.
verb_intransitive(Verb_intransitive) --> [Verb_intransitive], { member(Verb_intransitive, [
     lukin,         % look, watch out, pay attention
     pakala,        % screw up, fall apart, break
     unpa           % have sex
 ])
}.
verb_transitive(Verb_transitive) --> [Verb_transitive], { member(Verb_transitive, [
     ijo,           % objectify
     jan,           % personify, humanize, personalize
     jo,            % have, contain
     lukin,         % see, look at, watch, read
     moku,          % eat, drink, swallow, ingest, consume
     pakala,        % screw up, fuck up, botch, ruin, break, hurt, injure, damage, spoil, ruin
     pona,          % improve, fix, repair, make good
     suli,          % enlarge, lengthen
     suno,          % to light, to illumine
     telo,          % water, wash with water
     unpa           % have sex with, sleep with, fuck
 ])
}.
verb_pre(Verb_pre) --> [Verb_pre], { member(Verb_pre, [
     wile           % to want, need, wish, have to, must, will, should
 ])
}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% For a user friendly input:
:- ['check_grammar.pro']. 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

/*
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Possible queries
% 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Sentences with right grammar:
% Instead of "?- sentence([mi,moku,'.'],[])." you can use "check_grammar." 
% But "check_grammar." does not work in ​http://swish.swi-prolog.org. 

?- check_grammar.

mi moku e kili.
ona li lukin e pipi.
ona li pona e ilo.
mi pona e ijo.
mi wile lukin e ma.
mi wile pakala e sina.
ona li wile jo e ilo.
pipi li lukin li unpa.
mi moku li pakala.                 % many variants!
mi moku e kili e telo.
mi wile lukin e ma e suno.
mi wile lukin e ma e suno e ni e sina e ona.
mi wile lukin e ma e suno e ni e sina e ona li pona e ijo.
mi lukin e ni.
mi wile unpa e ona.
jan li wile jo e ma.
mi jan li suli.                    % many variants!
mi jo e ilo.
ona li moku e kili.
ijo li lukin e mi.
ona li wile pakala e pipi.
kili li moku li pona.              % many variants!
pipi li wile moku e telo.
mi lukin e ni.
jan li wile jo e ma.
mi jan li suli.                    % many variants!
mi moku li pakala li pakala.
mi moku li ma li pona li pipi li pakala li kili li lukin.


% Sentences with wrong grammar:

?- check_grammar.
|: mi li moku e wile.
false

?- check_grammar.
|: mi moku li pakala li.
false

?- check_grammar.
|: ona e pona li moku.
false.


%%% Possible queries with the syntax of the Difference Lists:

?- sentence([mi,wile,X,'.'],[]).   
?- direct_object(X,[]).
?- direct_objects(X,[]).
?- verb_phrase(X,[]).
?- verb_phrase_add(X,[]).
?- verb_phrases(X,[]).
?- sentence([mi,moku,e,X,'.'],[]).

?- sentence([mi,moku,e,kili,'.'],[]).
?- sentence([ona,li,lukin,e,pipi,'.'],[]).
?- sentence([ona,li,pona,e,ilo,'.'],[]).
?- sentence([mi,pona,e,ijo,'.'],[]).
?- sentence([mi,wile,lukin,e,ma,'.'],[]).
?- sentence([mi,wile,pakala,e,sina,'.'],[]).
?- sentence([ona,li,wile,jo,e,ilo,'.'],[]).
?- sentence([mi,moku,li,pakala,li,pakala,'.'],[]).
?- sentence([pipi,li,lukin,li,unpa,'.'],[]).
?- sentence([mi,moku,li,pakala,'.'],[]).                 % many variants!
?- sentence([mi,moku,e,kili,e,telo,'.'],[]).
?- sentence([mi,wile,lukin,e,ma,e,suno,'.'],[]).
?- sentence([mi,wile,lukin,e,ma,e,suno,e,ni,e,sina,e,ona,'.'],[]).
?- sentence([mi,wile,lukin,e,ma,e,suno,e,ni,e,sina,e,ona,li,pona,e,ijo,'.'],[]).
?- sentence([mi,lukin,e,ni,'.'],[]).
?- sentence([mi,wile,unpa,e,ona,'.'],[]).
?- sentence([jan,li,wile,jo,e,ma,'.'],[]).
?- sentence([mi,jan,li,suli,'.'],[]).                    % many variants!
?- sentence([mi,jo,e,ilo,'.'],[]).
?- sentence([ona,li,moku,e,kili,'.'],[]).
?- sentence([ijo,li,lukin,e,mi,'.'],[]).
?- sentence([ona,li,wile,pakala,e,pipi,'.'],[]).
?- sentence([kili,li,moku,li,pona,'.'],[]).              % many variants!
?- sentence([pipi,li,wile,moku,e,telo,'.'],[]).
?- sentence([mi,lukin,e,ni,'.'],[]).
?- sentence([mi,wile,unpa,e,ona,'.'],[]).
?- sentence([jan,li,wile,jo,e,ma,'.'],[]).
?- sentence([mi,jan,li,suli,'.'],[]).                    % many variants!

% Sentences with wrong grammar:
?- sentence([mi,Y,wile,'.'],[]).
?- sentence([mi,e,X,'.'],[]).
?- sentence([mi,li,moku,e,wile,'.'],[]).
*/

Here is the code of the include file check_grammar.pro.
Note: Include files don't work in http://swish.swi-prolog.org.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Learn Definite Clause Grammars
% Read a line of words from the user and check the grammar.
% Every word and !,.:;? are elements of a list.
% by Robert Warnke http://rowa.giso.de
% released under the GNU General Public License
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% For a user friendly input:

check_grammar             :- read_line(CL), wordlist(WL,CL,[]), !, sentence(WL,[]).   % Read a line from the user, put it in a list of words and check it with "sentence".

read_line(WL)             :- get0(C), codelist(C,WL).                           % Read a character code and put it in a list of codes.

codelist(10,[])           :- !.                                                 % Stop to build the list after line feed (10).
codelist(13,[])           :- !.                                                 % Stop to build the list after carriage return (13).
codelist(33,[32,33,32|X]) :- get0(C2), codelist(C2,X).                          % Put spaces (32) arround the "!" (33) and continue to build the list.
codelist(44,[32,44,32|X]) :- get0(C2), codelist(C2,X).                          % Put spaces (32) arround the "," (44) and continue to build the list.
codelist(46,[32,46,32|X]) :- get0(C2), codelist(C2,X).                          % Put spaces (32) arround the "." (46) and continue to build the list.
codelist(58,[32,58,32|X]) :- get0(C2), codelist(C2,X).                          % Put spaces (32) arround the ":" (58) and continue to build the list.
codelist(59,[32,59,32|X]) :- get0(C2), codelist(C2,X).                          % Put spaces (32) arround the ";" (59) and continue to build the list.
codelist(63,[32,63,32|X]) :- get0(C2), codelist(C2,X).                          % Put spaces (32) arround the "?" (63) and continue to build the list.
codelist(C,[C|X])         :- get0(C2), codelist(C2,X).                          % Continue to build the list of codes with the next character code.

% The rest is in dcg syntax:

wordlist([X|Y])          --> word(X), whitespaces, wordlist(Y).                 % A wordlist could be bild of one word, whitespaces and an other wordlist.
wordlist([X])            --> whitespaces, wordlist(X).                          % A wordlist could be bild of whitespaces and the same wordlist.
wordlist([X])            --> word(X).                                           % A wordlist could be bild of one word.
wordlist([X])            --> word(X), whitespaces.                              % A wordlist could be bild of one word and whitespaces character.

word(W)                  --> charlist(X), {name(W,X)}.                          % Build a word from a list of character codes.
                                                                                %  The prolog built-in predicate "name" represent a list of character codes as Atomic.
charlist([X|Y])          --> chr(X), charlist(Y).                               % A list of characters could be bild of one character and and an other list of characters.
charlist([X])            --> chr(X).                                            % A list of characters could be bild of one character.

chr(X)                   --> [X],{X>=33}.                                       % A useful character code for words is greater than or equal 33.

whitespaces              --> whitespace, whitespaces.                           % A whitspace could be one and omore whitspaces.
whitespaces              --> whitespace.                                        % A whitspace could be one whitspace

whitespace               --> [X], {X<33}.                                       % A useful character code for whitespaces is less than 33.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% eof

Download swi-prolog | Download these scripts | My langugage Projects on Github

 


rowa.giso.de | contact | Berlin (Good Old Europe)
This unofficial fan site by Robert Warnke is based on the official Toki Pona book and website: tokipona.org