-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblackspeech.pl
174 lines (137 loc) · 5.3 KB
/
blackspeech.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
:- module(blackspeech).
%% The One Ring speech:
%%
%% “Ash nazg durbatulûk, ash nazg gimbatul,
%% ash nazg thrakatulûk agh burzum-ishi krimpatul.”
speech([ash, nazg, durbatulûk,
ash, nazg, gimbatul,
ash, nazg, thrakatulûk, agh, burzumishi, krimpatul]).
%% This will be translated into the following English:
%% [one, ring, to, rule, them, all,
%% one, ring, to, find, them,
%% one, ring, to, bring, them, all, and, to, bind, them, in, darkness].
%% Many different parse trees are created. I suspect I have made my grammar
%% rules overly general. However, all of the translations come out
%% identically.
%% lexicon
root(ash, one).
root(lug, tower).
root(nazg, ring).
root(burz, dark).
root(durb, rule).
root(gimb, find).
root(krimp, bind).
root(thrak, bring).
conjunction(agh, and).
preposition(ishi, in).
%% generic grammatical combinators
optional([], []) --> [].
optional([O|Os], [O|Rs]) --> O, optional(Os, Rs).
optional([_|Os], Rs) --> optional(Os, Rs).
%% verb morphology
infinitive --> "at".
person(3) --> "ul".
number(pl) --> "ûk".
%% noun morphology
nominalized(Root, Nominalized) :- root(Root, _), atom_concat(Root, um, Nominalized).
illative(Root, Illative) :- atom_concat(Root, ishi, Illative).
%% top production
utterance([Conj|Rest]) --> conjunction(Conj), utterance(Rest).
utterance([VP|Rest]) --> verb_phrase(VP), utterance(Rest).
utterance([]) --> [].
%% basic grammar
%% conjunctions
conjunction(conj(Conj, Left, Right)) --> verb_phrase(Left), [Conj], { conjunction(Conj, _) }, verb_phrase(Right).
%% noun phrases: 'modifier noun' or 'noun'
noun_phrase(np(modifier(Modifier), Noun)) --> noun(Modifier), noun_phrase(Noun).
noun_phrase(np(Noun)) --> noun(Noun).
%% nouns: root words or nominalized roots ("dark" -> "darkness")
noun(noun(nominalized(root(Root)))) --> [Noun], { nominalized(Root, Noun) }.
noun(noun(root(Noun))) --> root(Noun).
%% prepositional "phrases" (is it still a 'phrase' if it's one word inflected?)
prepositional_phrase(pp(ishi, Noun)) -->
[Word], { illative(Root, Word), phrase(noun(Noun), [Root]) }.
%% verb phrases: 'noun verb' or 'prepositional-noun verb'
verb_phrase(vp(NP, VP)) --> noun_phrase(NP), verb_phrase(VP).
verb_phrase(vp(PP, VP)) --> prepositional_phrase(PP), verb_phrase(VP).
%% the root verb phrase: a root word plus a pile of optional verbal modifiers
verb_phrase(vp(verb(Root, Modifiers))) -->
[Verb],
{
atom_concat(Root, Tail, Verb),
root(Root, _),
atom_codes(Tail, ModifierCodes),
phrase(optional([infinitive, person(_), number(_)], Modifiers), ModifierCodes)
}.
root(Noun) --> [Noun], { root(Noun, _) }.
%% English translation
english([X|Xs], Result) :- english(X, Next), english(Xs, Rest), append(Next, Rest, Result).
english([], []).
%% conj(agh, Left, Right) --> Left 'and' Right
english(conj(agh, Left, Right), English) :-
english(Left, LeftEnglish),
english(Right, RightEnglish),
append(LeftEnglish, [and], HalfEnglish),
append(HalfEnglish, RightEnglish, English).
%% descriptor(Noun, Verb) --> Noun, Verb
english(descriptor(NP, VP), English) :-
english(NP, EnglishNP),
english(VP, EnglishVP),
append(EnglishNP, EnglishVP, English).
%% vp(Verb) --> Verb
english(vp(VP), English) :- english(VP, English).
%% vp(NounPhrase, VerbPhrase) --> NounPhrase, VerbPhrase
english(vp(np(NP), VP), English) :-
english(VP, VerbEnglish),
english(np(NP), NounEnglish),
append(NounEnglish, VerbEnglish, English).
%% vp(PrepositionalPhrase, VerbPhrase) --> VerbPhrase, PrepositionalPhrase
english(vp(pp(Prep, PP), VP), English) :-
english(VP, VerbEnglish),
english(pp(Prep, PP), PrepEnglish),
append(VerbEnglish, PrepEnglish, English).
%% verb(... [infinitive]) --> 'to', verb(...)
english(verb(Root, [infinitive|Mods]), [to|Rest]) :- english(verb(Root, Mods), Rest).
%% verb(... [3rd person plural]) --> verb(...) 'them' 'all'
english(verb(Root, [person(3), number(pl)|Mods]), Result) :-
english(verb(Root, Mods), Rest),
append(Rest, [them, all], Result), !.
%% verb(... [3rd person]) --> verb(...) 'them'
english(verb(Root, [person(3)|Mods]), Result) :-
english(verb(Root, Mods), Rest),
append(Rest, [them], Result).
%% verb(... [plural]) --> verb(...) 'all'
english(verb(Root, [number(pl)|Mods]), Result) :-
english(verb(Root, Mods), Rest),
append(Rest, [all], Result).
%% verb(...) --> root(...)
english(verb(Root, []), [English]) :- root(Root, English).
%% np(Adjective, Noun) --> Adjective, Noun
english(np(modifier(Mod), NP), Result) :-
english(Mod, EnglishMod),
english(NP, EnglishPhrase),
append(EnglishMod, EnglishPhrase, Result).
%% np(Noun) --> Noun
english(np(BS), English) :- english(BS, English).
%% root(X) --> english(X)
english(root(X), [English]) :- root(X, English).
%% noun(X) --> X
english(noun(root(X)), [Y]) :- root(X, Y).
%% pp(ishi, NP) --> 'in', NP
english(pp(ishi, NP), [in|Rest]) :- english(NP, Rest).
%% -- special cases --
%% burzum --> darkness
english(noun(nominalized(root(burz))), [darkness]).
%% helper
display([Word|Rest]) :- write(Word), write(' '), display(Rest).
display([]) :- !.
main :-
speech(RingSpeech),
phrase(utterance(Tree), RingSpeech),
english(Tree, English),
write('The ring speech: '),
display(RingSpeech), nl,
write('Parsed: '), nl,
portray_clause(Tree), nl,
write('Translated: '), nl,
display(English), nl, !.