home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Multimed / Multimed.zip / fest-141.zip / festival / lib / lts.scm < prev    next >
Lisp/Scheme  |  1999-05-30  |  7KB  |  195 lines

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;;                                                                       ;;
  3. ;;;                Centre for Speech Technology Research                  ;;
  4. ;;;                     University of Edinburgh, UK                       ;;
  5. ;;;                         Copyright (c) 1998                            ;;
  6. ;;;                        All Rights Reserved.                           ;;
  7. ;;;                                                                       ;;
  8. ;;;  Permission is hereby granted, free of charge, to use and distribute  ;;
  9. ;;;  this software and its documentation without restriction, including   ;;
  10. ;;;  without limitation the rights to use, copy, modify, merge, publish,  ;;
  11. ;;;  distribute, sublicense, and/or sell copies of this work, and to      ;;
  12. ;;;  permit persons to whom this work is furnished to do so, subject to   ;;
  13. ;;;  the following conditions:                                            ;;
  14. ;;;   1. The code must retain the above copyright notice, this list of    ;;
  15. ;;;      conditions and the following disclaimer.                         ;;
  16. ;;;   2. Any modifications must be clearly marked as such.                ;;
  17. ;;;   3. Original authors' names are not deleted.                         ;;
  18. ;;;   4. The authors' names are not used to endorse or promote products   ;;
  19. ;;;      derived from this software without specific prior written        ;;
  20. ;;;      permission.                                                      ;;
  21. ;;;                                                                       ;;
  22. ;;;  THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK        ;;
  23. ;;;  DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING      ;;
  24. ;;;  ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT   ;;
  25. ;;;  SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE     ;;
  26. ;;;  FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES    ;;
  27. ;;;  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN   ;;
  28. ;;;  AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,          ;;
  29. ;;;  ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF       ;;
  30. ;;;  THIS SOFTWARE.                                                       ;;
  31. ;;;                                                                       ;;
  32. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  33. ;;;
  34. ;;;  Functions specific to supporting a trained LTS rules
  35. ;;;
  36.  
  37. (define (lts_rules_predict word feats)
  38.   (let ((dcword (downcase word))
  39.     (syls) (phones))
  40.     (if (string-matches dcword "[a-z]*")
  41.     (begin
  42.       (set! phones 
  43.         (cdr (reverse (cdr (reverse (lts_predict dcword))))))
  44.       (set! phones (add_lex_stress word feats phones))
  45.       (set! syls (lex.syllabify.phstress phones))
  46. ;;      (set! syls (add_lex_stress word syls))
  47.       )
  48.     (set! syls nil))
  49.     (format t "word %l phones %l\n" word syls)
  50.     (list word nil syls)))
  51.  
  52. ;(define (add_lex_stress word syls)
  53. ;  (cond
  54. ;   ((> (length syls) 1)
  55. ;      (set-car! (cdr (nth (- (length syls) 2) syls)) 1))
  56. ;   ((word-is-content word english_guess_pos)
  57. ;      (set-car! (cdr (car syls)) 1)))
  58. ;  syls)
  59.  
  60. (define (word-is-content word guess_pos)
  61.   (cond
  62.    ((null guess_pos)
  63.     t)
  64.    ((member_string word (cdr (car guess_pos)))
  65.     nil)
  66.    (t
  67.     (word-is-content word (cdr guess_pos)))))
  68.  
  69. (defvar lts_pos nil)
  70.  
  71. (define (lts_predict word rules)
  72.   "(lts_predict word rules)
  73. Return list of phones related to word using CART trees."
  74.   (let ((utt (make_let_utt (enworden (symbolexplode word)))))
  75.     (predict_phones utt rules)
  76.     (cdr (reverse (cdr (reverse ;; remove #'s
  77.       (mapcar item.name (utt.relation.items utt 'PHONE))))))
  78.     )
  79. )
  80.  
  81. (define (make_let_utt letters)
  82. "(make_let_utt letters)
  83. Build an utterances from th4ese letters."
  84.   (let ((utt (Utterance Text "")))
  85.     (utt.relation.create utt 'LTS)
  86.     (utt.relation.create utt 'LETTER)
  87.     (utt.relation.create utt 'PHONE)
  88.     ;; Create letter stream
  89.     (mapcar
  90.      (lambda (l)
  91.        (let ((lsi (utt.relation.append utt 'LETTER)))
  92.      (item.set_feat lsi "pos" lts_pos)
  93.      (item.set_name lsi l)))
  94.      letters)
  95.     utt))
  96.  
  97. (define (predict_phones utt rules)
  98.   "(predict_phones utt)
  99. Predict phones using CART."
  100.   (add_new_phone utt (utt.relation.first utt 'LETTER) '#)
  101.   (mapcar
  102.    (lambda (lsi)
  103.      (let ((tree (car (cdr (assoc (intern (item.name lsi)) rules)))))
  104.        (if (not tree)
  105.        (format t "failed to find tree for %s\n" (item.name lsi))
  106.        (let ((p (wagon_predict lsi tree)))
  107.          (if (string-matches p ".*-.*");; a double one
  108.          (begin
  109.            (add_new_phone utt lsi (string-before p "-"))
  110.            (add_new_phone utt lsi (string-after p "-")))
  111.          (add_new_phone utt lsi p))))))
  112.    (reverse (cdr (reverse (cdr (utt.relation.items utt 'LETTER))))))
  113.   (add_new_phone utt (utt.relation.last utt 'LETTER) '#)
  114.   utt)
  115.  
  116. (define (add_new_phone utt lsi p)
  117.   "(add_new_phone utt lsi p)
  118. Add new phone linking to letter, ignoreing it if its _epsilon_."
  119.   (if (not (equal? p '_epsilon_))
  120.       (let ((psi (utt.relation.append utt 'PHONE)))
  121.     (item.set_name psi p)
  122.     (item.relation.append_daughter
  123.      (utt.relation.append utt 'LTS lsi)
  124.      'LTS psi)
  125.     )))
  126.  
  127. (define (enworden lets)
  128.   (cons '# (reverse (cons '# (reverse lets)))))
  129.  
  130. ;;; Lexical stress assignment
  131. ;;;
  132.  
  133. (define (add_lex_stress word pos phones tree)
  134.   "(add_lex_stress word syls)
  135. Predict lexical stress by decision tree."
  136.   (let ((utt (Utterance Text ""))
  137.     (si)
  138.     (nphones))
  139.     (utt.relation.create utt 'Letter)
  140.     (set! si (utt.relation.append utt 'Letter))
  141.     (item.set_feat si 'pos pos)
  142.     (item.set_feat si 'numsyls (count_syls phones))
  143.     (item.set_feat si 'sylpos 1)
  144.     (set! nphones (add_lex_stress_syl phones si tree))
  145. ;    (format t "%l\n" phones)
  146. ;    (format t "%l\n" nphones)
  147.     nphones))
  148.  
  149. (define (count_syls phones)
  150.   (cond
  151.    ((null phones) 0)
  152.    ((string-matches (car phones) "[aeiou@].*")
  153.     (+ 1 (count_syls (cdr phones))))
  154.    (t (count_syls (cdr phones)))))
  155.  
  156. (define (add_lex_stress_syl phones si tree)
  157.   "(add_lex_stress_syl phones si tree)
  158. Add lexical stressing."
  159.   (cond
  160.    ((null phones) nil)
  161.    ((string-matches (car phones) "[aeiou@].*")
  162.     (item.set_feat si 'phone (car phones))
  163.     (item.set_feat si 'name (car phones))
  164.     (item.set_feat si 'num2end 
  165.              (- (+ 1 (item.feat si 'numsyls))
  166.                 (item.feat si 'sylpos)))
  167.     (set! stress (wagon_predict si tree))
  168.     (item.set_feat si 'sylpos
  169.              (+ 1 (item.feat si 'sylpos)))
  170.     (cons
  171.      (if (not (string-equal stress "0"))
  172.      (string-append (car phones) stress)
  173.      (car phones))
  174.      (add_lex_stress_syl (cdr phones) si tree)))
  175.    (t 
  176.     (cons
  177.      (car phones)
  178.      (add_lex_stress_syl (cdr phones) si tree)))))
  179.  
  180. ;;; Morphological analysis
  181.  
  182.  
  183. ;(define (wfst_stemmer)
  184. ;  (wfst.load 'stemmer "/home/awb/projects/morpho/engstemmer.wfst")
  185. ;  (wfst.load 'stemmerL "/home/awb/projects/morpho/engstemmerL.wfst")
  186. ;  t)
  187.  
  188. ;(define (stem word)
  189. ;  (wfst.transduce 'stemmer (enworden (symbolexplode word))))
  190.  
  191. ;(define (stemL word)
  192. ;  (wfst.transduce 'stemmerL (enworden (symbolexplode word))))
  193.  
  194. (provide 'lts)
  195.