-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.scm
224 lines (201 loc) · 6.99 KB
/
main.scm
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
;;;;
;;;; main file
;;;;
(require-extension utf8)
(require-extension irregex)
(require-extension srfi-1)
(define s+ string-append)
;;;
;;; assoc lists
;;;
(define sound-classes '())
(define phonological-rules '())
(define prettyprint-rules '())
;;; TODO: clean up the mess
;; Associates a name with a set of sounds
(define (set-class key value)
(let ((x (assoc key sound-classes)))
(if x
(set-cdr! x value)
(set! sound-classes (cons (cons key value) sound-classes)))))
;; Associates a name with a list of rules to get to phonetic representation
(define (set-phono-rules key value)
(let ((x (assoc key phonological-rules)))
(if x
(set-cdr! x value)
(set! phonological-rules (cons (cons key value) phonological-rules)))))
;; Associates a name with a list of rules to pretty-print words
(define (set-pretty-rules key value)
(let ((x (assoc key prettyprint-rules)))
(if x
(set-cdr! x value)
(set! prettyprint-rules (cons (cons key value) prettyprint-rules)))))
(define (lookup key alist)
(let ((x (assoc key alist)))
(if x
(cdr x)
'())))
(define (l key)
(lookup key sound-classes))
;;;
;;; tree and rule functions
;;;
;; makes a list of all languages between start and end language based on a tree
(define (make-path tree start end)
(define (find-start tree)
(cond ((atom? tree)
(if (eq? tree start)
tree
'()))
((eq? (car tree) start)
tree)
(else (let loop ((t (cdr tree)))
(if (null? t)
'()
(let ((s (find-start (car t))))
(if (null? s)
(loop (cdr t))
s)))))))
(define (to-end tree)
(cond ((atom? tree)
(if (eq? tree end)
(cons tree '())
'()))
((eq? (car tree) end)
(cons end '()))
(else (let loop ((t tree))
(if (null? t)
'()
(let ((p (to-end (car t))))
(if (null? p)
(loop (cdr t))
(cons (car tree) p))))))))
(to-end (find-start tree)))
;; make a list of rules that apply to the language development in path
(define (make-rules tree path)
(define start (car path))
(define end (car (reverse path)))
(define (rule-applies? langlist)
(or (null? langlist)
(if (equal? (car langlist) 'not)
(eq? '() (lset-intersection eq? path (cdr langlist)))
(not (eq? '() (lset-intersection eq? path langlist))))))
(define (find-start tree)
(cond ((null? tree)
'())
((eq? (car tree) start)
(cdr tree))
((and (list? (car tree))
(eq? 'br (caar tree)))
(let ((l (find-start (caddar tree))))
(if (null? l)
(find-start (cdr tree))
l)))
(else (find-start (cdr tree)))))
(define (to-end tree)
(cond ((or (null? tree)
(eq? (car tree) end))
'())
((procedure? (car tree))
(cons (car tree) (to-end (cdr tree)))) ; better use tail recursion
((and (list? (car tree))
(rule-applies? (cadar tree)))
(case (caar tree)
((br) (to-end (caddar tree)))
((sub) (append (to-end (caddar tree)) (to-end (cdr tree))))
(else (to-end (cdr tree)))))
(else (to-end (cdr tree)))))
(to-end (find-start tree)))
;;;
;;; helper functions
;;;
;; convert list of strings to single string with one original string per line
(define (make-words lst)
(if (null? lst)
'()
(let ((s (make-words (cdr lst))))
(if (null? s)
(car lst)
(s+ (car lst) "\n" s)))))
;; make regex substitute function
(define (s pattern . subst)
(let ((pat (string->irregex (make-regex pattern) 'utf8 'fast 'm)))
(lambda (word)
(apply irregex-replace/all (cons pat (cons word subst))))))
;; compile regex containing custom character classes
(define (make-regex s)
(let loop ((classes sound-classes)
(s s))
(if (null? classes)
s
(loop (cdr classes)
(irregex-replace/all (s+ "<"
(symbol->string (caar classes))
">")
s
(cdar classes))))))
;; apply a list of rules to a string, return transformed string
(define (apply-rules rules words)
(if (null? rules)
words
(apply-rules (cdr rules)
((car rules) words))))
;; runs 'words' from language 'from' through language 'to'
(define (run-list from to words)
(let ((r1 (make-rules rules (make-path lang-tree from to)))
(r2 (lookup from phonological-rules))
(r3 (lookup to prettyprint-rules)))
(apply-rules r3
(apply-rules r1
(apply-rules r2 (make-words words))))))
;;;
;;; data
;;;
;; tree of languages
;; an atom is a leaf language
;; a list's car is a language, the cdr its children
(define lang-tree
'(uridg (urgr1 (urgr (nwgr (dor (nwdor phok lokr aitol)
(sardor kor meg oarg)
(sdor el lak inseldor warg kret))
(aiol boiot (thess wthess othess) lesb))
(sogr (ach myk ark kypr pamph)
(ion-att (ostion hom) inselion euboi att))))
(urar (urir ap
(urav aav jav))
(uria ved))
urgerm
))
;; sound classes
;; TODO: these should be changeable by the rules
(set-class 'kurz-vok "e|E|o|O|a|A|i|u|ə")
(set-class 'lang-vok "ē|Ē|ō|Ō|ā|Ā|ī|ū")
(set-class 'vok (s+ (l 'kurz-vok) "|" (l 'lang-vok)))
(set-class 'liquid "r|ŕ|l|ĺ")
(set-class 's-liquid "R|L")
(set-class 'nasal "m|n|ṇ|ň|ñ|ń|ṅ")
(set-class 's-nasal "M|N")
(set-class 'halb-vok "y|w")
(set-class 'res (s+ (l 'liquid) "|" (l 'nasal)))
(set-class 'res-yw (s+ (l 'res) "|" (l 'halb-vok)))
(set-class 's-res (s+ (l 's-liquid) "|" (l 's-nasal)))
(set-class 'lary "H1|H2|H3|H")
(set-class 'sibil "s|š|ṣ|ś|z!|z|ž!|ž|ẓ!|ẓ")
(set-class 'labial "b!|p!|b|p")
(set-class 'dental "d!|t!|d|t")
(set-class 'retroflex "ḍ!|ṭ!|ḍ|ṭ")
(set-class 'palatal "ǰ!|č!|ǰ|č")
(set-class 'palatal2 "j!|c!|j|c")
(set-class 'velar "g!|k!|g|k")
(set-class 'labiovelar "G!|K!|G|K")
(set-class 'okklu (s+ (l 'labial) "|" (l 'dental) "|" (l 'retroflex)
"|" (l 'palatal) "|" (l 'palatal2) "|" (l 'velar)
"|" (l 'labiovelar)))
(set-class 'kons (s+ (l 'res) "|" (l 'halb-vok) "|" (l 'sibil) "|"
(l 'okklu) "|" (l 'lary)))
(set-class 'media "b|d|ḍ|ǰ|j|g|G")
(set-class 'tenuis "p|t|ṭ|č|c|k|K")
(set-class 'mediaasp "b!|d!|ḍ!|ǰ!|j!|g!|G!")
(set-class 'tenuisasp "p!|t!|ṭ!|č!|c!|k!|K!")
(load "subfuncs.scm")
(load "rules.scm")