File: lts_build.scm

package info (click to toggle)
festival 1%3A2.4~release-3
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 7,432 kB
  • ctags: 6,364
  • sloc: cpp: 27,729; lisp: 15,695; ansic: 6,022; sh: 5,660; java: 1,536; makefile: 769; xml: 291; perl: 87
file content (723 lines) | stat: -rw-r--r-- 23,476 bytes parent folder | download | duplicates (4)
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;                                                                       ;;
;;;                Centre for Speech Technology Research                  ;;
;;;                     University of Edinburgh, UK                       ;;
;;;                         Copyright (c) 1998                            ;;
;;;                        All Rights Reserved.                           ;;
;;;                                                                       ;;
;;;  Permission is hereby granted, free of charge, to use and distribute  ;;
;;;  this software and its documentation without restriction, including   ;;
;;;  without limitation the rights to use, copy, modify, merge, publish,  ;;
;;;  distribute, sublicense, and/or sell copies of this work, and to      ;;
;;;  permit persons to whom this work is furnished to do so, subject to   ;;
;;;  the following conditions:                                            ;;
;;;   1. The code must retain the above copyright notice, this list of    ;;
;;;      conditions and the following disclaimer.                         ;;
;;;   2. Any modifications must be clearly marked as such.                ;;
;;;   3. Original authors' names are not deleted.                         ;;
;;;   4. The authors' names are not used to endorse or promote products   ;;
;;;      derived from this software without specific prior written        ;;
;;;      permission.                                                      ;;
;;;                                                                       ;;
;;;  THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK        ;;
;;;  DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING      ;;
;;;  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT   ;;
;;;  SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE     ;;
;;;  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES    ;;
;;;  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN   ;;
;;;  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,          ;;
;;;  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF       ;;
;;;  THIS SOFTWARE.                                                       ;;
;;;                                                                       ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;
;;;  Functions for building LTS rules sets from lexicons
;;;
;;;

(defvar pl-table nil)

(define (allaligns phones letters)
  "(cummulate phones lets)
Aligns all possible ways for these strings."
  (cond
   ((null letters)
    ;; (wrongly) assume there are never less letters than phones
    (if phones
	(format t "wrong end: %s\n" word))
    nil)
   ((null phones)
    nil)
   (t
    (if (< (length phones) (length letters))
	(begin
	  (cummulate '_epsilon_ (car letters))
	  (allaligns phones (cdr letters))))
    (cummulate (car phones) (car letters))
    (allaligns (cdr phones) (cdr letters)))))

(define (valid-pair phone letter)
  "(valid-pair phone letter)
If predefined to be valid."
  (let ((entry1 (assoc_string letter pl-table)))
    (if entry1
	(assoc_string phone (cdr entry1))
	nil)))

(define (valid-pair-e phone nphone letter)
  "(valid-pair-e phone letter)
Special cases for when epsilon may be inserted before letter."
  (let ((ll (assoc_string letter pl-table))
	(pp (intern (string-append phone "-" nphone))))
    (assoc_string pp (cdr ll))))

(define (find-aligns phones letters)
  "(find-aligns phones letters)
Find all feasible alignments."
  (let ((r nil))
    (cond
     ((and (null (cdr phones)) (null (cdr letters))
	   (equal? (car phones) (car letters))
	   (equal? '# (car phones)))
      (list (list (cons '# '#)))) ;; valid end match
     (t
      (if (valid-pair '_epsilon_ (car letters))
	  (set! r (mapcar
		   (lambda (p)
		     (cons (cons '_epsilon_ (car letters)) p))
		   (find-aligns phones (cdr letters)))))
      (if (valid-pair (car phones) (car letters))
	  (set! r 
		(append r
			(mapcar
			 (lambda (p)
			   (cons (cons (car phones) (car letters)) p))
			 (find-aligns (cdr phones) (cdr letters))))))
      ;; Hmm, change this to always check doubles
      (if (valid-pair-e (car phones) (car (cdr phones)) (car letters))
	  (set! r
		(append r
			(mapcar
			 (lambda (p)
			   (cons (cons (intern (format nil "%s-%s"
						       (car phones)
						       (car (cdr phones))))
				       (car letters)) p))
			 (find-aligns (cdr (cdr phones)) 
				      (cdr letters))))))
      r))))

(define (findallaligns phones letters)
  (let ((a (find-aligns phones letters)))
    (if (null a)
	(begin
	  (set! failedaligns (+ 1 failedaligns))
	  (format t "failed: %l %l\n" letters phones)))
    a))

(define (cummulate phone letter)
  "(cummulate phone letter)
record the alignment of this phone and letter."
  (if (or (equal? phone letter)
	  (and (not (equal? phone '#))
	       (not (equal? letter '#))))
  (let ((entry1 (assoc_string letter pl-table))
	score)
    (if (equal? phone '_epsilon_)
	(set! score 0.1)
	(set! score 1))
    (if entry1
	(let ((entry2 (assoc_string phone (cdr entry1))))
	  (if entry2
	      (set-cdr! entry2 (+ score (cdr entry2)))
	      (set-cdr! entry1 (cons (cons phone 1) (cdr entry1)))))
	(set! pl-table
	      (cons 
	       (cons letter
		     (list (cons phone score)))
	       pl-table)))
    t)))

(define (score-pair phone letter)
"(score-pair phone letter)
Give score for this particular phone letter pair."
  (let ((entry1 (assoc_string letter pl-table)))
    (if entry1
	(let ((entry2 (assoc_string phone (cdr entry1))))
	  (if entry2
	      (cdr entry2)
	      0))
	0)))

(define (cummulate-aligns aligns)
  (mapcar
   (lambda (a)
     (mapcar 
      (lambda (p)
	(cummulate (car p) (cdr p)))
      a))
   aligns)
  t)

(define (cummulate-pairs trainfile)
  "(cummulate-pairs trainfile)
Build cummulatation table from allowable alignments in trainfile."
  (set! failedaligns 0)
  (set! allaligns 0)
  (if (not pl-table)
      (set! pl-table
	    (mapcar
	     (lambda (l)
	       (cons (car l) (mapcar (lambda (c) (cons c 0)) (cdr l))))
	     allowables)))
  (let ((fd (fopen trainfile "r"))
	(c 0) (d 0)
	(entry))
    (while (not (equal? (set! entry (readfp fd)) (eof-val)))
	   (if (equal? c 1000)
	       (begin
		 (format t "ENTRY: %d %l\n" (set! d (+ 1000 d)) entry)
		 (set! c 0)))
	   (set! word (car entry))
	   (cummulate-aligns
	    (findallaligns 
	     (enworden (car (cdr (cdr entry))))
	     (enworden (wordexplode (car entry)))))
	   (set! allaligns (+ 1 allaligns))
           (format t "aligned %d\n" allaligns)
	   (set! c (+ 1 c)))
    (fclose fd)
    (format t "failedaligns %d/%d\n" failedaligns allaligns)
    ))

(define (find_best_alignment phones letters)
  "(find_best_alignment phones letters)
Find the alignement containg the most frequent alignment pairs."
  ;; hackily do this as a global
  (set! fba_best_score 0)
  (set! fba_best nil)
  (find-best-align phones letters nil 0)
  fba_best
)


(define (find-best-align phones letters path score)
  "(find-best-align phones letters)
Find all feasible alignments."
  (cond
   ((null letters)
    (if (> score fba_best_score)
	(begin
	  (set! fba_best_score score)
	  (set! fba_best (reverse path))))
    nil)
   (t
    (if (valid-pair '_epsilon_ (car letters))
	(find-best-align phones (cdr letters)
			 (cons (cons '_epsilon_ (car letters)) path)
			 (+ score (score-pair '_epsilon_ (car letters)))))
    (if (valid-pair (car phones) (car letters))
	(find-best-align (cdr phones) (cdr letters)
			 (cons (cons (car phones) (car letters))path)
			 (+ score (score-pair (car phones) (car letters)))))
    (if (valid-pair-e (car phones) (car (cdr phones)) (car letters))
	(find-best-align (cdr (cdr phones)) (cdr letters)
			 (cons (cons (intern (format nil "%s-%s"
						     (car phones)
						     (car (cdr phones))))
				     (car letters))
			       path)
			 (+ score (score-pair 
				   (intern (format nil "%s-%s"
						   (car phones)
						   (car (cdr phones))))
				   (car letters))))))))

(define (align_and_score phones letters path score)
  "(align_and_score phones lets)
Aligns all possible ways for these strings."
  (cond
   ((null letters)
    (if (> score fba_best_score)
	(begin
	  (set! fba_best_score score)
	  (set! fba_best (reverse path))))
    nil)
   (t
    (if (< (length phones) (length letters))
	(align_and_score
	 phones
	 (cdr letters)
	 (cons '_epsilon_ path)
	 (+ score
	    (score-pair '_epsilon_ (car letters)))))
    (align_and_score
     (cdr phones)
     (cdr letters)
     (cons (car phones) path)
     (+ score
	(score-pair (car phones) (car letters)))))))

(define (aligndata file ofile)
  (let ((fd (fopen file "r"))
	(ofd (fopen ofile "w"))
	(c 1)
	(entry))
    (while (not (equal? (set! entry (readfp fd)) (eof-val)))
	   (set! lets (enworden (wordexplode (car entry))))
	   (set! bp (find_best_alignment
		     (enworden (car (cdr (cdr entry))))
		     lets))
	   (if (not bp)
	       (format t "align failed: %l\n" entry)
	       (save_info (car (cdr entry)) bp ofd))
	   (set! c (+ 1 c)))
    (fclose fd)
    (fclose ofd)))

(define (enworden lets)
  (cons '# (reverse (cons '# (reverse lets)))))

(define (wordexplode lets)
  (if (consp lets)
      lets
      (symbolexplode lets)))

(define (save_info pos bp ofd)
  "(save_info pos bp ofd)
Cut out one expensive step and 50M of diskspace and just save it
in a simpler format."
  (format ofd "( ( ")
  (mapcar
   (lambda (l) 
     (if (not (string-equal "#" (cdr l)))
	 (format ofd "%l " (cdr l))))
   bp)
  (format ofd ") %l" pos)
  (mapcar
   (lambda (l)
     (if (not (string-equal "#" (car l)))
	 (format ofd " %s" (car l))))
   bp)
  (format ofd " )\n"))

(define (normalise-table pl-table)
  "(normalise-table pl-table)
Change scores into probabilities."
  (mapcar
   (lambda (s)
     (let ((sum (apply + (mapcar cdr (cdr s)))))
       (mapcar
	(lambda (p)
          (if (equal? sum 0)
              (set-cdr! p 0)
              (set-cdr! p (/ (cdr p) sum))))
	(cdr s))))
   pl-table)
  t)

(define (save-table pre)
  (normalise-table pl-table)
  (set! fd (fopen (string-append pre "pl-tablesp.scm") "w"))
  (format fd "(set! pl-table '\n")
  (pprintf pl-table fd)
  (format fd ")\n")
  (fclose fd)
  t)

(define (build-feat-file alignfile featfile)
"(build-feat-file alignfile featfile)
Build a feature file from the given align file.  The feature
file contain predicted phone, and letter with 3 preceding and
3 succeeding letters."
  (let ((fd (fopen alignfile "r"))
	(ofd (fopen featfile "w"))
	(entry)
	(pn)
	(sylpos 1))
    (while (not (equal? (set! entry (readfp fd)) (eof-val)))
;;           (format t "read: %l\n" entry)
	   (set! lets (append '(0 0 0 0 #) (wordexplode (car entry))
			      '(# 0 0 0 0)))
	   (set! phones (cdr (cdr entry)))
	   (set! pn 5)
	   (mapcar
	    (lambda (p)
	      (format ofd
		      "%s  %s %s %s %s  %s  %s %s %s %s  %s\n"
		      p
		      (nth (- pn 4) lets)
		      (nth (- pn 3) lets)
		      (nth (- pn 2) lets)
		      (nth (- pn 1) lets)
		      (nth pn lets)
		      (nth (+ pn 1) lets)
		      (nth (+ pn 2) lets)
		      (nth (+ pn 3) lets)
		      (nth (+ pn 4) lets)
                      (cond
                       ((not (consp (car (cdr entry))))
                        (car (cdr entry)))
                       ((not (consp (caar (cdr entry))))
                        (caar (cdr entry)))
                       (t nil))
		      ;; sylpos
		      ;; numsyls
		      ;; num2end
		      )
	      (set! pn (+ 1 pn)))
	    phones))
    (fclose fd)
    (fclose ofd))
)

(define (merge_models name filename allowables)
"(merge_models name filename)
Merge the models into a single list of cart trees as a variable
named by name, in filename."
  (require 'cart_aux)
  (let (trees fd)
    (set! trees nil)
    (set! lets (mapcar car allowables))
    (while lets
      (if (probe_file (format nil "lts.%s.tree" (car lets)))
          (begin
            (format t "%s\n" (car lets))
            (set! tree (car (load (format nil "lts.%s.tree" (car lets)) t)))
            (set! tree (cart_simplify_tree2 tree nil))
            (set! trees
                  (cons (list (car lets) tree) trees))))
      (set! lets (cdr lets)))
    (set! trees (reverse trees))
    (set! fd (fopen filename "w"))
    (format fd ";; LTS rules \n")
    (format fd "(set! %s '(\n" name)
    (mapcar
     (lambda (tree) (pprintf tree fd))
     trees)
    (format fd "))\n")
    (fclose fd))
)

(define (lts_testset file cartmodels)
  "(lts_testset file cartmodels)
Test an aligned lexicon file against a set of cart trees.  Prints out
The number of letters correct (for each letter), total number of 
letters correct and the total number of words correct.  cartmodels is
the structure as saved by merge_models."
  (let ((fd (fopen file "r"))
	(entry)
	(wordcount 0)
	(correctwords 0)
	(phonecount 0)
	(correctphones 0))
    (while (not (equal? (set! entry (readfp fd)) (eof-val)))
	   (let ((letters (enworden (wordexplode (car entry))))
		 (phones (enworden (cdr (cdr entry))))
		 (pphones))
	     (set! wordcount (+ 1 wordcount))
	     (set! pphones (gen_cartlts letters (car (cdr entry)) cartmodels))
;	     (set! pphones 
;                   (or ; unwind-protect
;                    (gen_vilts letters (car (cdr entry))
;                               cartmodels wfstname)
;                    nil))
	     (if (equal? (ph-normalize pphones) (ph-normalize phones))
		 (set! correctwords (+ 1 correctwords))
		 (or nil
		     (format t "failed %l %l %l %l\n" (car entry) (car (cdr entry)) phones pphones)))
	     (count_correct_letters   ;; exclude #, cause they're always right
	      (cdr letters)
	      (cdr phones)
	      (cdr pphones))
	     (set! phonecount (+ (length (cdr (cdr letters))) phonecount))
	     ))
    (fclose fd)
    (mapcar
     (lambda (linfo)
       (format t "%s %d correct %d (%2.2f)\n"
	       (car linfo) (car (cdr linfo))
	       (car (cdr (cdr linfo)))
	       (/ (* (car (cdr (cdr linfo))) 100) (car (cdr linfo))))
       (set! correctphones (+ correctphones (car (cdr (cdr linfo))))))
     correct_letter_table)
    (format t "phones %d correct %d (%2.2f)\n"
	    phonecount correctphones (/ (* correctphones 100) phonecount))
    (format t "words %d correct %d (%2.2f)\n"
	    wordcount correctwords (/ (* correctwords 100) wordcount))
    (format t "tree model has %d nodes\n"
	    (apply + (mapcar (lambda (a) (cart_tree_node_count (car (cdr a))))
			     cartmodels)))
    ))

(define (cart_tree_node_count tree)
  "(tree_node_count tree)
Count the number nodes (questions and leafs) in the given CART tree."
  (cond
   ((cdr tree)
    (+ 1
       (cart_tree_node_count (car (cdr tree)))
       (cart_tree_node_count (car (cdr (cdr tree))))))
   (t
    1)))

(defvar correct_letter_table
  (mapcar
   (lambda (l) (list l 0 0))
   '(a b c d e f g h i j k l m n o p q r s t u v w x y z))
  "correct_letter_table
List used to cummulate the number of correct (and incorrect) letter to
phone predictions.  This list will be extended if there are more letters
in your alphabet, though it doesn't take a fairly western european
view of the alphabet,  but you can change this yourself is necessary.")

(define (count_correct_letters lets phs pphs)
 "(count_correct_letters lets phs pphs)
Count which letters have the correct phone prediction.  Cummulate this
is a per letter table."
 (cond
  ((or (null phs) (null pphs) (null lets))
   (format t "misaligned entry\n") 
   nil)
  ((and (null (cdr lets)) (null (cdr phs)) (null (cdr pphs)))
   nil)  ;; omit final #
  (t
   (let ((letinfo (assoc_string (car lets) correct_letter_table)))
     (if (not letinfo)
	 (set! correct_letter_table
	       (append correct_letter_table
		       (list (set! letinfo (list (car lets) 0 0))))))
     (set-car! (cdr letinfo) (+ 1 (car (cdr letinfo)))) ;; total
     (if (equal? (car phs) (car pphs))                  ;; correct 
	 (set-car! (cdr (cdr letinfo)) (+ 1 (car (cdr (cdr letinfo))))))
     (count_correct_letters (cdr lets) (cdr phs) (cdr pphs))))))

(define (ph-normalize ph)
  (cond
   ((null ph) nil)
   ((string-equal "_epsilon_" (car ph))
    (ph-normalize (cdr ph)))
   ((string-matches (car ph) ".*-.*")
    (cons
     (string-before (car ph) "-")
     (cons
      (string-after (car ph) "-")
      (ph-normalize (cdr ph)))))
   (t
    (cons (car ph) (ph-normalize (cdr ph))))))

(define (make_let_utt_p letters pos)
"(make_let_utt letters)
Build an utterances from th4ese letters."
  (let ((utt (Utterance Text "")))
    (utt.relation.create utt 'LTS)
    (utt.relation.create utt 'LETTER)
    (utt.relation.create utt 'PHONE)
    ;; Create letter stream
    (mapcar
     (lambda (l)
       (let ((lsi (utt.relation.append utt 'LETTER)))
	 (item.set_name lsi l)
	 (item.set_feat lsi "pos" pos)))
     letters)
    utt))

(define (gen_vilts letters pos cartmodels ngram)
  "(get_vilts letters pos cartmodels ngram)
Use cart plus ngrams in viterbi search."
  (require 'lts)
  (let ((utt (make_let_utt_p letters pos)))
    (set! gen_vit_params
	  (list
	   (list 'Relation "LETTER")
	   (list 'return_feat "phone")
	   (list 'p_word "#")
	   (list 'pp_word "0")
           (list 'ngramname ngram)
;	   (list 'wfstname ngram)
	   (list 'cand_function 'lts_cand_function)))
    (Gen_Viterbi utt)
    (mapcar 
      (lambda (lsi)
	(intern (item.feat lsi "phone")))
      (utt.relation.items utt 'LETTER))))

(define (gen_cartlts letters pos cartmodels)
  "(get_cartlts letters cartmodels)
Generate the full list of predicted phones, including
epsilon and unexpanded multi-phones."
  (require 'lts)
  (let ((utt (make_let_utt_p letters pos)))
    (enworden
     (mapcar
      (lambda (lsi)
	(let ((tree (car (cdr (assoc_string (item.name lsi) cartmodels))))
	      (p))
	  (if (not tree)
	      (begin
		(format t "failed to find tree for %s\n" (item.name lsi))
		nil)
	      (begin
		(set! p (wagon_predict lsi tree))
		(item.set_feat lsi "val" p)
		p))))
      (reverse (cdr (reverse (cdr (utt.relation.items utt 'LETTER)))))))))

(define (reduce_lexicon entryfile exceptionfile lts_function)
  "(reduce_lexicon entryfile exceptionfile lts_function)
Look up each word in entryfile using the current lexicon, if the entry
doesn't match save it in the exception file.  This is a way of reducing
the lexicon based on a letter to sound model (and lexical stress 
model, if appropriate)."
  (let ((fd (fopen entryfile "r"))
	(ofd (fopen exceptionfile "w"))
	(entry)
	(wordcount 0)
	(correctwords 0))
    (while (not (equal? (set! entry (readfp fd)) (eof-val)))
	   (if (and (consp entry) 
		    (> (length entry) 1))
	       (let ((lts (lts_function (car entry) (car (cdr entry))))
		     (encount (lex.entrycount (car entry))))
		 (set! wordcount (+ 1 wordcount))
		 (if (and (equal? (nth 2 entry) (nth 2 lts))
			  (< encount 2))
		     (set! correctwords (+ 1 correctwords))
		     (format ofd "%l\n" entry))
		 )))
    (fclose fd)
    (fclose ofd)
    (format t "words %d correct %d (%2.2f)\n"
	    wordcount correctwords (/ (* correctwords 100) wordcount))
    ))

(define (dump-flat-entries infile outfile ltype)
  (let ((ifd (fopen infile "r"))
        (ofd (fopen outfile "w"))
        clength
        entry)
;    (set! entry (readfp ifd))
;    (if (or (consp entry) (not (string-equal entry "MNCL")))
;        (begin 
;          (format t "Expected MNCL at start of file: not a compiled lexicon\n")
;          (exit)))
    (while (not (equal? (set! entry (readfp ifd)) (eof-val)))
       (cond
        ((not (consp entry))
         t) ;; not an entry
        ((string-equal ltype "utf8")
         (set! clength (length (utf8explode (car entry)))))
        (t
         (set! clength (length (car entry)))))
       (cond
        ((not (consp entry))
         t) ;; not an entry
        ((and ;(string-matches (car entry) "...*")
              ;(< clength 14)
              (not (string-matches (car entry) ".*'.*")) ;; no quotes
              (car (cddr entry))) ;; non-nil pronounciation
         (begin
           (cond
            ((string-equal ltype "utf8")
             (format ofd
                       "( %l %l ("
                       (utf8explode (car entry))
                       (cadr entry)))
            ((string-equal ltype "asis")
             (format ofd
                     "( \"%s\" %l ("
                     (car entry)
                     (cadr entry)))
            (t
             (format ofd
                     "( \"%s\" %l ("
                     (downcase (car entry))
                     (cadr entry))))
           (if (consp (car (car (cddr entry))))
               (begin ;; it is syllabified)
                 (mapcar
                  (lambda (syl)
                    (mapcar
                     (lambda (seg)
                       (cond
                        ((string-matches seg "[aeiouAEIOU@].*")
                         (format ofd "%s " (string-append seg (cadr syl))))
                        (t
                         (format ofd "%s " seg))))
                     (car syl)))
                  (car (cddr entry))))
               (begin ;; it is already flat
                 (mapcar
                  (lambda (p)
                    (format ofd "%s " p))
                  (car (cddr entry)))
                 ))
           (format ofd "))\n")))
        (t nil)))
    (fclose ifd)
    (fclose ofd)))

(define (dump-lets-phones infile)
  "(dump-lets-phones infile)
Dump all the letters to alllets.out and phones to allphones.out for processing.
This expects an external script to sort and uniquify them.  This is done
in scheme so we can get utf8/non-utf8 to be easy."
  (let ((ifd (fopen infile "r"))
        (lfd (fopen "alllets.out" "w"))
        (apfd (fopen "allphones.out" "w"))
        (pfd (fopen "let2phones.out" "w"))
        entry)
    (while (not (equal? (set! entry (readfp ifd)) (eof-val)))
       (mapcar
        (lambda (l) 
          (format lfd "%s\n" l)
          (format pfd "%s " l)
          (mapcar 
           (lambda (p) (format pfd "%s " p))
           (car (cddr entry)))
          (format pfd "\n"))
        (wordexplode (car entry)))
       (mapcar
        (lambda (p) (format apfd "%s " p))
        (car (cddr entry)))
       (format apfd "\n")
       )
    (fclose ifd)
    (fclose lfd)
    (fclose pfd)
    (fclose apfd)
    t))

(define (dump-flat-entries-all infile outfile)
  "(dump-flat-entries-all infile outfile)
Do this for *all* entries not just ones with more than three chars."
  (let ((ifd (fopen infile "r"))
        (ofd (fopen outfile "w"))
        entry)
    (readfp ifd) ;; skip "MNCL"
    (while (not (equal? (set! entry (readfp ifd)) (eof-val)))
     (if (consp entry)
         (begin
           (format ofd
                   "( \"%s\" %s ("
                   (downcase (car entry))
                   (cadr entry))
           (mapcar
            (lambda (syl)
              (mapcar
               (lambda (seg)
                 (cond
;                 ((string-equal seg "ax")
;                    (format ofd "%s " seg))
                  ((string-matches seg "[aeiouAEIOU@].*")
                     (format ofd "%s " (string-append seg (cadr syl))))
                  (t
                   (format ofd "%s " seg))))
               (car syl)))
            (car (cddr entry)))
           (format ofd "))\n"))))
    (fclose ifd)
    (fclose ofd)))

(provide 'lts_build)