Skip to main content
Allow basic subordinate clauses
Source Link
chrisd
  • 141
  • 3

This version has a fairly limited vocabulary and sentence structure, but more words can be added (trivial) and more grammar rules can be added as well.

In particular, having a rule for subordinate clauses wouldit should be cool (e.g. 'The food which I ate rest_of_sentence'). This would give much more interesting sentencespretty easy to extend.

% VocabularyDefine the vocabulary verb(V) :- V = 'eats' | V = 'fights' | V = 'finds'. subj_pronoun(P) :- P = 'he' | P = 'she' | P = 'it'. obj_pronoun(P) :- P = 'him' | P = 'her' | P = 'it'. name(N) :- N = 'alice' | N = 'bob'. noun(N) :- N = 'cat' | N = 'door' | N = 'pen'. article(H) :- H = 'the' | H = 'a'. % Grammar obj_nounsubject_phrase_short(NH) :- nounsubj_pronoun(NH)  | obj_pronounname(NH). subject_phrase% Subordinate clause. Don't use verb_phrase here to avoid recursive clauses. sub_clause([H][Which, Verb|T], Rest) :- subj_pronounWhich = 'which', verb(HVerb), | name object_noun_phrase_short(T, Rest). subject_phrase([H|T], Rest) :- subject_phrase_short(H), Rest = T. noun_phraseobject_noun_phrase_short([A, N]N | T], Rest) :- article(A), noun(N), Rest = T | obj_pronoun(A), Rest = [N|T]. object_phrase([H|T]L, Rest) :- noun_phraseobject_noun_phrase_short([H|T]L, Rest) | object_noun_phrase_short(L, Rest1), sub_clause(Rest1, Rest). verb_phrase([H|T], Rest) :- verb(H), object_phrase(T, Rest). sentence([H|T]S) :- subject_phrase([H]S, Rest), verb_phrase(TRest, []). 
L = [he, eats, the, cat] ; L = [she, finds, a, door] ; L = [alice, fights, the, door] ; L = [he, fights, the, cat, which, eats, the, pen] ; L = [alice, eats, him, which, finds, the, cat] ; 

(EDIT: Allow object subordinate clauses).

This version has a fairly limited vocabulary and sentence structure, but more words can be added (trivial) and more grammar rules can be added as well.

In particular, having a rule for subordinate clauses would be cool (e.g. 'The food which I ate rest_of_sentence'). This would give much more interesting sentences.

% Vocabulary verb(V) :- V = 'eats' | V = 'fights' | V = 'finds'. subj_pronoun(P) :- P = 'he' | P = 'she' | P = 'it'. obj_pronoun(P) :- P = 'him' | P = 'her' | P = 'it'. name(N) :- N = 'alice' | N = 'bob'. noun(N) :- N = 'cat' | N = 'door' | N = 'pen'. article(H) :- H = 'the' | H = 'a'. % Grammar obj_noun(N) :- noun(N) | obj_pronoun(N). subject_phrase([H]) :- subj_pronoun(H) | name(H). noun_phrase([A, N]) :- article(A), noun(N). object_phrase([H|T]) :- noun_phrase([H|T]). verb_phrase([H|T]) :- verb(H), object_phrase(T). sentence([H|T]) :- subject_phrase([H]), verb_phrase(T). 
L = [he, eats, the, cat] ; L = [she, finds, a, door] ; L = [alice, fights, the, door] ; 

This version has a fairly limited vocabulary and sentence structure, but it should be pretty easy to extend.

% Define the vocabulary verb(V) :- V = 'eats' | V = 'fights' | V = 'finds'. subj_pronoun(P) :- P = 'he' | P = 'she' | P = 'it'. obj_pronoun(P) :- P = 'him' | P = 'her' | P = 'it'. name(N) :- N = 'alice' | N = 'bob'. noun(N) :- N = 'cat' | N = 'door' | N = 'pen'. article(H) :- H = 'the' | H = 'a'. % Grammar subject_phrase_short(H) :- subj_pronoun(H)  | name(H). % Subordinate clause. Don't use verb_phrase here to avoid recursive clauses. sub_clause([Which, Verb|T], Rest) :- Which = 'which', verb(Verb),  object_noun_phrase_short(T, Rest). subject_phrase([H|T], Rest) :- subject_phrase_short(H), Rest = T. object_noun_phrase_short([A, N | T], Rest) :- article(A), noun(N), Rest = T | obj_pronoun(A), Rest = [N|T]. object_phrase(L, Rest) :- object_noun_phrase_short(L, Rest) | object_noun_phrase_short(L, Rest1), sub_clause(Rest1, Rest). verb_phrase([H|T], Rest) :- verb(H), object_phrase(T, Rest). sentence(S) :- subject_phrase(S, Rest), verb_phrase(Rest, []). 
L = [he, eats, the, cat] ; L = [she, finds, a, door] ; L = [alice, fights, the, door] ; L = [he, fights, the, cat, which, eats, the, pen] ; L = [alice, eats, him, which, finds, the, cat] ; 

(EDIT: Allow object subordinate clauses).

Source Link
chrisd
  • 141
  • 3

Prolog

Use prolog's backtracking and a generative grammar approximating English grammar to generate all possible sentences.

This version has a fairly limited vocabulary and sentence structure, but more words can be added (trivial) and more grammar rules can be added as well.

In particular, having a rule for subordinate clauses would be cool (e.g. 'The food which I ate rest_of_sentence'). This would give much more interesting sentences.

The code:

% Vocabulary verb(V) :- V = 'eats' | V = 'fights' | V = 'finds'. subj_pronoun(P) :- P = 'he' | P = 'she' | P = 'it'. obj_pronoun(P) :- P = 'him' | P = 'her' | P = 'it'. name(N) :- N = 'alice' | N = 'bob'. noun(N) :- N = 'cat' | N = 'door' | N = 'pen'. article(H) :- H = 'the' | H = 'a'. % Grammar obj_noun(N) :- noun(N) | obj_pronoun(N). subject_phrase([H]) :- subj_pronoun(H) | name(H). noun_phrase([A, N]) :- article(A), noun(N). object_phrase([H|T]) :- noun_phrase([H|T]). verb_phrase([H|T]) :- verb(H), object_phrase(T). sentence([H|T]) :- subject_phrase([H]), verb_phrase(T). 

Run this query:

sentence(L). 

to generate all possible sentences in this language.

Some sample outputs:

L = [he, eats, the, cat] ; L = [she, finds, a, door] ; L = [alice, fights, the, door] ;