// Abstract ------------------------------------------------------------------------------------------------------------------------------

This sample tests or makes simple English sentences. It is based on an example from the AI book by Luger & Stubblefield ("AI Structures and Strategies for Complex Problem Solving", 3rd edition, page 419).

// Examples ------------------------------------------------------------------------------------------------------------------------------

?- #utterance.test([the,man,bites,the,dog])
-> ( ) := 1.00 (0.002) 1
?- #utterance.test([the,man,bites,:x,:y])
-> ( a , man ) := 1.00 (0.003) 1
-> ( a , dog ) := 1.00 (0.003) 2
-> ( the , man ) := 1.00 (0.004) 3
-> ( the , dog ) := 1.00 (0.004) 4
?- #utterance.make(:x)
-> ( [man, likes] ) := 1.00 (0.005) 1
-> ( [man, bites] ) := 1.00 (0.005) 2
-> ( [dog, likes] ) := 1.00 (0.009) 3
-> ( [dog, bites] ) := 1.00 (0.010) 4
-> ( [a, man, likes] ) := 1.00 (0.011) 5
-> ( [a, man, bites] ) := 1.00 (0.011) 6
-> ( [a, dog, likes] ) := 1.00 (0.011) 7
-> ( [a, dog, bites] ) := 1.00 (0.011) 8
-> ( [man, likes, man] ) := 1.00 (0.012) 9
-> ( [man, likes, dog] ) := 1.00 (0.012) 10
-> ...
-> ( [the, dog, bites, the, man] ) := 1.00 (0.025) 83
-> ( [the, dog, bites, the, dog] ) := 1.00 (0.026) 84
 
// Code ----------------------------------------------------------------------------------------------------------------------------------

article { // defines all the supported 'articles'

    (a);
    (the);

}

noun { // defines all the supported 'nouns'

    (man);
    (dog);

}

verb { // defines all the supported 'verbs'

    (likes);
    (bites);

}

utterance.test { // test a given 'utterance'

    (:x) :- #sentence(:x,[]);

}

utterance.make { // make any possible 'utterance'

    (:x) :- #sentence(:x);

}

sentence { // make or test a 'sentence'

    (:s,:e) :- #n.phrase(:s,:r), #v.phrase(:r,:e);
    (:s)    :- #n.phrase(:n,_), #v.phrase(:v,_), lst.cat(:n,:v,:s);

}

n.phrase { // rules for the 'noun' part of a sentence

    ([:n],[])       :- #noun(:n);
    ([:a,:n],[])^   :- #article(:a), #noun(:n);
    ([:a,:n|:e],:e) :- #article(:a), #noun(:n);

}

v.phrase { // rules for the 'verb' part of a sentence

    ([:v],[])       :- #verb(:v);
    ([:v|:r],[])    :- #verb(:v), #n.phrase(:r,[]);

}

// ---------------------------------------------------------------------------------------------------------------------------------------

[Home] [Email] [Twitter] [LinkedIn]