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
 
Contact
 
 

 
Home | Text version
 

Learn Definite Clause Grammars (DCG) with Toki Pona

Lession 1

In the first script we create DCG rules for the lesson Basic Sentences.

Simple Version

We start with a simple version. Comments start with a "%" or are between "/*" and "*/". As you can see the rules of DCG consist the characters "-->" between the header and the body of a rule. In pure Prolog the rules consist ":-". At the end of every rule is a ".".

At the beginning we have to define the rule for sentences:

sentence               --> noun_phrase, verb_phrase, separator_sentence.

After this we need definitions for "noun_phrase" and "verb_phrase". At the end we have to define facts. Theses are words (nouns, pronouns, separators and transitive verbs).

You have to load the script in your Prolog system.

If you don't like to install swi-prolog:
Use Chromium browser and go to http://swish.swi-prolog.org. Click in the left window on "Program" and copy the cource code in the left window. In the bottom right you can type your query.

If you have installed swi-prolog:
Start swi-prolog in this directory where the file "toki-pona-01-simple.pro" is. Load this file in Prolog with this command:

?- ['toki-pona-01-simple.pro'].

The script consist possible queries as comments at the end. After you load the script in your prolog system you can ask the system with these questions. You will get the answer "true" if the grammar is right.

toki-pona-01-simple.pro
Source code:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Learn Definite Clause Grammars with Toki Pona
% Toki Pona - Lession 01 - Basic Sentences - Simple variant
% by Robert Warnke http://rowa.giso.de
% released under the GNU General Public License 
%
% ['toki-pona-01-simple.pro'].
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Grammar 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

sentence               --> noun_phrase, verb_phrase, separator_sentence. % A sentence consist of a noun phrase, a verb phrase and a ".".

noun_phrase            --> pronoun_1person.                              % A noun phrase can be the pronoun "mi"      or
noun_phrase            --> pronoun_2person.                              % the pronoun "sina"                         or
noun_phrase            --> noun, separator_noun_phrase.                  % a noun plus "li".

verb_phrase            --> verb_transitive.                              % A verb phrase can be a transitive verb     or  
verb_phrase            --> noun.                                         % a noun. In toki pona is no verb "to be".

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

noun                   --> [jan].      % person, people, human, being, somebody, anybody
noun                   --> [moku].     % food, meal
noun                   --> [pona].     % good, simplicity, positivity
noun                   --> [suli].     % size
noun                   --> [suno].     % sun, light
noun                   --> [telo].     % water, liquid, juice, sauce

pronoun_1person        --> [mi].       % I, we
pronoun_2person        --> [sina].     % you

separator_sentence     --> ['.'].      % At the end of a sentence.
separator_noun_phrase  --> [li].       % between any subject except mi and sina and its verb

verb_transitive        --> [jan].      % personify, humanize, personalize
verb_transitive        --> [moku].     % eat, drink, swallow, ingest, consume
verb_transitive        --> [pona].     % improve, fix, repair, make good
verb_transitive        --> [suli].     % enlarge, lengthen
verb_transitive        --> [telo].     % water, wash with water

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/*
Possible queries
The strange syntax is a result of the Difference List of Prolog
----------------

% Show all nouns (Press ";" after each line):
?- noun(X,[]).

% Show all transitive verbs:
?- verb_transitive(X,[]).

% Show all noun phrases:
?- noun_phrase(X,[]).

% For example the sentence "mi moku" has two possibilities (I eat. / I am food.).
?- sentence([mi,moku,'.'],[]).
true ;
true ;

% The sentence "sina pona." has two meanings as well.
?- sentence([sina,pona,'.'],[]).
true ;
true ;
?- sentence([mi,pona,'.'],[]).
true ;
true ;
?- sentence([telo,li,pona,'.'],[]).
true ;
true ;

% All possible sentences
?- sentence(X,[]).

% Sentences with right grammar:
?- sentence([suno,li,suli,'.'],[]).
?- sentence([moku,li,pona,'.'],[]).
?- sentence([suno,li,suli,'.'],[]).
?- sentence([mi,suli,'.'],[]).
?- sentence([jan,li,moku,'.'],[]).
?- sentence([jan,li,pona,'.'],[]).
?- sentence([mi,moku,'.'],[]).
?- sentence([sina,suli,'.'],[]).
?- sentence([telo,li,pona,'.'],[]).
?- sentence([telo,li,suli,'.'],[]).
?- sentence([suno,li,suli,'.'],[]).
?- sentence([mi,suli,'.'],[]).
?- sentence([jan,li,moku,'.'],[]).

% Sentences with wrong grammar:
?- sentence([suno,suli,'.'],[]).
false
?- sentence([mi,li,suli,'.'],[]).
false
?- sentence([li,telo,'.'],[]).
false
?- sentence([moku,jan,'.'],[]).
false

--------------------------------

% List the structure:
?- listing.
?- listing(noun_phrase).
*/

A Better Version

The simple version has some disadvantages. It is not possible to select a member in a list. For select "mi" or "sina" we have to build separate list:

pronoun_1person        --> [mi].    
pronoun_2person        --> [sina]. 

Better is to define a general word list and select the word. For example:

pronoun(mi)

For this we have to use the rule "member" of pure Prolog and give the word via variable to the DCG. In DCG you can write pure Prolog code in "{" and "}". Variables start with capital letters. In this example "Pronoun" is a variable.

pronoun(Pronoun) --> [Pronoun], { member(Pronoun, [
     mi,            
     sina          
 ])
}

Is it not necessary to select a member you have to use "_". This example list all pronouns:

pronoun(_)

As we see later there are different types of sentences: declarative, imperative (commands), interrogative (questions), exclamatory (excitement or emotion) and conditional sentences. We start with the declarative sentences.

sentence                 --> sentence_declarative, separator('.').       % A declarative sentence states a fact or an argument and ends with a full stop.
sentence_declarative     --> noun_phrase, verb_phrase.                   % A declarative sentence consist of a noun phrasea and a verb phrase.  

An other disadvantage is this: For grammar check you have to use this complex syntax (Difference Lists):

?- sentence([mi,moku,'.'],[]).

I'va added the rule "check_grammar". Now you can type Toki Pona sentences direct in.

?- check_grammar.
|: mi moku.
true ;

The rule "check_grammar" is a bit complicated for a beginner. Don't worry. Please look at the rest of the code which is importand for describing Toki Pona.

toki-pona-01.pro
Source code:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Learn Definite Clause Grammars with Toki Pona
% Toki Pona - Lession 01 - Basic Sentences
% by Robert Warnke http://rowa.giso.de
% released under the GNU General Public License 
%
% ['toki-pona-01.pro'].
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* 
Learn Definite Clause Grammars (DCG) of SWI-Prolog (http://www.swi-prolog.org).
with the simple constructed language toki pona (http://tokipona.org).

You can test these examples without installing SWI-Prolog at 
​http://swish.swi-prolog.org (use Chromium browser).
Copy the cource code in the left window and choose "File, Collaborate".
In the bottom right you can type your query. 
For example the query to list all possible sentences with this grammar:
?- sentence(X,[]).
The Button "Next" show the next possibility. 
This is the same as ";" in installed prolog.
To learn Prolog go to http://www.learnprolognow.org
*/
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 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_phrase.                   % A declarative sentence consist of a noun phrasea and a verb phrase.  


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

noun_phrase              --> pronoun(mi).                                % A noun phrase can be the pronoun "mi"      or
noun_phrase              --> pronoun(sina).                              % the pronoun "sina"                         or
noun_phrase              --> noun(_),  separator(li).                    % a noun plus "li".

verb_phrase              --> verb_transitive(_).                         % A verb phrase can be a transitive verb     or  
verb_phrase              --> noun(_).                                    % a noun. In toki pona is no verb "to be". 


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

noun(Noun) --> [Noun], { member(Noun, [   
     jan,            % person, people, human, being, somebody, anybody
     moku,           % food, meal
     pona,           % good, simplicity, positivity
     suli,           % size
     suno,           % sun, light
     telo            % water, liquid, juice, sauce
 ])
}.

pronoun(Pronoun) --> [Pronoun], { member(Pronoun, [
     mi,            % I, we (pronoun 1. person)
     sina           % you (pronoun 2. person)
 ])
}.

separator(Separator) --> [Separator], { member(Separator, [
     li,            % between any subject except mi and sina and its verb
     '.'            % At the end of a sentence.
 ])
}.

verb_transitive(Verb_transitive) --> [Verb_transitive], { member(Verb_transitive, [
     jan,           % personify, humanize, personalize
     moku,          % eat, drink, swallow, ingest, consume
     pona,          % improve, fix, repair, make good
     suli,          % enlarge, lengthen
     suno,          % to light, to illumine
     telo           % water, wash with water
 ])
}.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% For a user friendly input:

% This syntax is pure Prolog:

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.  

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

% Simple 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. 
% If you use http://swish.swi-prolog.org please use "?- sentence([mi,moku,'.'],[]).".


% For example the sentence "mi moku" has two possibilities (I eat. / I am food.).
?- check_grammar.
|: mi moku.
true ;
true ;
false.

% The sentence "sina pona." has two meanings as well.
?- check_grammar.
|: sina pona.
true ;
true ;

% More sentences with right grammar:
?- check_grammar.
|: mi pona.

?- check_grammar.
|: telo li pona.

?- check_grammar.
|: suno li suli.

?- check_grammar.
|: moku li pona.

?- check_grammar.
|: mi suli.

?- check_grammar.
|: jan li moku.

?- check_grammar.
|: jan li pona.

?- check_grammar.
|: mi moku.

?- check_grammar.
|: sina suli.

?- check_grammar.
|: telo li pona.

?- check_grammar.
|: telo li suli.

?- check_grammar.
|: suno li suli.

?- check_grammar.
|: jan li moku.


% Sentences with wrong grammar:
?- check_grammar.
|: suno suli.
false

?- check_grammar.
|: mi li suli.
false

?- check_grammar.
|: li telo.
false

?- check_grammar.
|: moku jan.
false

% --------------------------------
% For other queries you need the syntax of the Difference List:

% All sentences with on word, "li" and one word before and after:
?- sentence([X,li,Y,'.'],[]).

% All possible sentences
?- sentence(X,[]).

% List the structure:
?- listing.
?- listing(noun_phrase).
*/

 


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