home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / share / festival / clunits_build.scm < prev    next >
Encoding:
Text File  |  2006-12-20  |  15.4 KB  |  459 lines

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;;                                                                       ;;
  3. ;;;                   Carnegie Mellon University and                      ;;
  4. ;;;                Centre for Speech Technology Research                  ;;
  5. ;;;                     University of Edinburgh, UK                       ;;
  6. ;;;                       Copyright (c) 1998-2001                         ;;
  7. ;;;                        All Rights Reserved.                           ;;
  8. ;;;                                                                       ;;
  9. ;;;  Permission is hereby granted, free of charge, to use and distribute  ;;
  10. ;;;  this software and its documentation without restriction, including   ;;
  11. ;;;  without limitation the rights to use, copy, modify, merge, publish,  ;;
  12. ;;;  distribute, sublicense, and/or sell copies of this work, and to      ;;
  13. ;;;  permit persons to whom this work is furnished to do so, subject to   ;;
  14. ;;;  the following conditions:                                            ;;
  15. ;;;   1. The code must retain the above copyright notice, this list of    ;;
  16. ;;;      conditions and the following disclaimer.                         ;;
  17. ;;;   2. Any modifications must be clearly marked as such.                ;;
  18. ;;;   3. Original authors' names are not deleted.                         ;;
  19. ;;;   4. The authors' names are not used to endorse or promote products   ;;
  20. ;;;      derived from this software without specific prior written        ;;
  21. ;;;      permission.                                                      ;;
  22. ;;;                                                                       ;;
  23. ;;;  THE UNIVERSITY OF EDINBURGH, CARNEGIE MELLON UNIVERSITY AND THE      ;;
  24. ;;;  CONTRIBUTORS TO THIS WORK DISCLAIM ALL WARRANTIES WITH REGARD TO     ;;
  25. ;;;  THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY   ;;
  26. ;;;  AND FITNESS, IN NO EVENT SHALL THE UNIVERSITY OF EDINBURGH, CARNEGIE ;;
  27. ;;;  MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE FOR ANY SPECIAL,    ;;
  28. ;;;  INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER          ;;
  29. ;;;  RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN  AN ACTION   ;;
  30. ;;;  OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF     ;;
  31. ;;;  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.       ;;
  32. ;;;                                                                       ;;
  33. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  34. ;;;
  35. ;;;  Cluster Unit selection support (Black and Taylor Eurospeech '97)
  36. ;;;
  37. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  38. ;;;
  39. ;;;  clunits build support
  40. ;;;
  41. ;;;  There are five stages to this
  42. ;;;     Load in all utterances
  43. ;;;     Load in their coefficients
  44. ;;;     Collect together the units of the same type
  45. ;;;     build distance tables from them
  46. ;;;     dump features for them
  47. ;;;
  48.  
  49. (require_module 'clunits)  ;; C++ modules support
  50. (require 'clunits)         ;; run time scheme support
  51.  
  52. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  53.  
  54. (define (do_all)
  55.   (let ((utterances))
  56.  
  57.     (format t "Loading utterances and sorting types\n")
  58.     (set! utterances (acost:db_utts_load clunits_params))
  59.     (set! unittypes (acost:find_same_types utterances clunits_params))
  60.     (acost:name_units unittypes)
  61.  
  62.     (format t "Dumping features for clustering\n")
  63.     (acost:dump_features unittypes utterances clunits_params)
  64.  
  65.     (format t "Loading coefficients\n")
  66.     (acost:utts_load_coeffs utterances)
  67.     ;; If you are short of diskspace try this
  68.     (acost:disttabs_and_clusters unittypes clunits_params)
  69.  
  70.     ;; or if you have lots of diskspace try
  71. ;    (format t "Building distance tables\n")
  72. ;    (acost:build_disttabs unittypes clunits_params)
  73.  
  74. ;    ;; Build the cluster trees (requires disttabs and features)
  75. ;    (format t "Building cluster trees\n")
  76. ;    (acost:find_clusters (mapcar car unittypes) clunits_params)
  77.  
  78.     ;; Tidy up and put things together
  79.     (acost:collect_trees (mapcar car unittypes) clunits_params)
  80.     
  81.     (format t "Saving unit catalogue\n")
  82.     (acost:save_catalogue utterances clunits_params)
  83.     
  84.   )
  85. )
  86.  
  87. (define (do_init)
  88.     (set! utterances (acost:db_utts_load clunits_params))
  89.     (set! unittypes (acost:find_same_types utterances clunits_params))
  90.     (acost:name_units unittypes)
  91.     t)
  92.  
  93. (define (acost:disttabs_and_clusters unittypes clunits_params)
  94.   "(acost:disttabs_and_custers unittypes)
  95. Cause it uses so much diskspace, build each table individually
  96. and them the cluster, removing the table before moving on to the
  97. next."
  98.   (mapcar
  99.    (lambda (uu)
  100.      (acost:build_disttabs (list uu) clunits_params)
  101.      (acost:find_clusters (list (car uu)) clunits_params)
  102.      (delete-file 
  103.       (format nil "%s/%s/%s%s" 
  104.          (get_param 'db_dir clunits_params "./")
  105.          (get_param 'disttabs_dir clunits_params "disttabs/")
  106.          (car uu)
  107.          (get_param 'disttabs_ext clunits_params ".disttab")))
  108.      )
  109.    unittypes)
  110.   t)
  111.  
  112. (define (acost:db_utts_load params)
  113.   "(acost:db_utts_load params)
  114. Load in all utterances identified in database."
  115.   (let ((files (car (cdr (assoc 'files params)))))
  116.     (set! acost:all_utts
  117.       (mapcar
  118.        (lambda (fname)
  119.          (set! utt_seg (Utterance Text fname))
  120.          (utt.load utt_seg 
  121.                (string-append 
  122.             (get_param 'db_dir params "./")
  123.             (get_param 'utts_dir params "utts/")
  124.             fname
  125.             (get_param 'utts_ext params ".utt")))
  126.          utt_seg)
  127.        files))))
  128.  
  129. (define (acost:utts_load_coeffs utterances)
  130.   "(acost:utts_load_coeffs utterances)
  131. Loading the acoustic coefficients of for each utterance."
  132.   (mapcar 
  133.    (lambda (utt) (acost:utt.load_coeffs utt clunits_params))
  134.    utterances)
  135.   t)
  136.  
  137. (define (acost:find_same_types utterances params)
  138.   "(acost:find_same_types utterances)
  139. Find all the stream items of the same type and collect them into
  140. lists of that type."
  141.   (let ((clunit_name_feat (get_param 'clunit_name_feat params "name")))
  142.     (set! acost:unittypes nil)
  143.     (mapcar 
  144.      (lambda (u)
  145.        (mapcar 
  146.     (lambda (s) 
  147.       (let ((cname (item.feat s clunit_name_feat)))
  148.         (if (not (string-equal "ignore" cname))
  149.         (begin
  150.           (item.set_feat s "clunit_name" (item.feat s clunit_name_feat))
  151.           (let ((p (assoc (item.feat s "clunit_name") acost:unittypes)))
  152.             (if p
  153.             (set-cdr! p (cons s (cdr p)))
  154.             (set! acost:unittypes
  155.                   (cons
  156.                    (list (item.feat s "clunit_name") s) 
  157.                    acost:unittypes))))))))
  158.     (utt.relation.items u 'Segment)))
  159.      utterances)
  160.     (acost:prune_unittypes acost:unittypes params)))
  161.  
  162. (define (acost:prune_unittypes unittypes params)
  163.   "(acost:prune_unittypes unittypes)
  164. If unit types are complex (contain an _) then remove all unittypes sets 
  165. with less than unittype_prune_threshold (typically 3)."
  166.   (if (string-matches (car (car unittypes)) ".*_.*")
  167.       (let ((ut nil) (pt (get_param 'unittype_prune_threshold params 0)))
  168.     (while unittypes
  169.      (if (or (eq? pt 0)
  170.          (> (length (cdr (car unittypes))) pt))
  171.          (set! ut (cons (car unittypes) ut)))
  172.      (set! unittypes (cdr unittypes)))
  173.     (reverse ut))
  174.       unittypes))
  175.  
  176. (define (acost:name_units unittypes)
  177.   "(acost:name_units unittypes)
  178. Names each unit with a unique id and number the occurences of each type."
  179.   (let ((idnum 0) (tynum 0))
  180.     (mapcar
  181.      (lambda (s)
  182.        (set! tynum 0)
  183.        (mapcar
  184.     (lambda (si)
  185.       (item.set_feat si "unitid" idnum)
  186.       (set! idnum (+ 1 idnum))
  187.       (item.set_feat si "occurid" tynum)
  188.       (set! tynum (+ 1 tynum)))
  189.     (cdr s))
  190.        (format t "units \"%s\" %d\n" (car s) tynum))
  191.      unittypes)
  192.     (format t "total units %d\n" idnum)
  193.     idnum))
  194.  
  195. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  196. ;;; Generating feature files
  197.  
  198. (define (acost:dump_features unittypes utterances params)
  199.   "(acost:dump_features unittypes utterances params)
  200. Do multiple passes over the utterances for each unittype and
  201. dump the desired features.  This would be easier if utterances
  202. weren't require for feature functions."
  203.   (mapcar
  204.    (lambda (utype)
  205.      (acost:dump_features_utype 
  206.       (car utype)
  207.       (cdr utype)
  208.       utterances
  209.       params))
  210.    unittypes)
  211.   t)
  212.  
  213. (define (acost:dump_features_utype utype uitems utterances params)
  214.   "(acost:dump_features_utype utype utterances params)
  215. Dump features for all items of type utype."
  216.   (let ((fd (fopen 
  217.          (string-append 
  218.           (get_param 'db_dir params "./")
  219.           (get_param 'feats_dir params "feats/")
  220.           utype
  221.           (get_param 'feats_ext params ".feats"))
  222.          "w"))
  223.     (feats (car (cdr (assoc 'feats params)))))
  224.     (format t "Dumping features for %s\n" utype)
  225.     (mapcar
  226.      (lambda (s)
  227.        (mapcar 
  228.     (lambda (f)
  229.       (format fd "%s " (item.feat s f)))
  230.     feats)
  231.        (format fd "\n"))
  232.      uitems)
  233.     (fclose fd)))
  234.     
  235. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  236. ;;;  Tree building functions
  237.  
  238. (defvar wagon-balance-size 0)
  239.  
  240. (define (acost:find_clusters unittypes clunits_params)
  241. "Use wagon to find the best clusters."
  242.   (mapcar
  243.    (lambda (unittype)
  244.      (build_tree unittype clunits_params))
  245.    unittypes)
  246.   t)
  247.  
  248. (define (build_tree unittype clunits_params)
  249. "Build tree with Wagon for this unittype."
  250.   (let ((command 
  251.      (format nil "%s -desc %s -data %s -balance %s -distmatrix %s -stop %s -output %s %s"
  252.          (get_param 'wagon_progname clunits_params "wagon")
  253.          (if (probe_file
  254.               (string-append
  255.                (get_param 'db_dir clunits_params "./")
  256.                (get_param 'wagon_field_desc clunits_params "wagon")
  257.                "." unittype))
  258.              ;; So there can be unittype specific desc files
  259.              (string-append
  260.                (get_param 'db_dir clunits_params "./")
  261.                (get_param 'wagon_field_desc clunits_params "wagon")
  262.                "." unittype)
  263.              (string-append
  264.                (get_param 'db_dir clunits_params "./")
  265.                (get_param 'wagon_field_desc clunits_params "wagon")))
  266.          (string-append 
  267.           (get_param 'db_dir clunits_params "./")
  268.           (get_param 'feats_dir clunits_params "feats/")
  269.           unittype
  270.           (get_param 'feats_ext clunits_params ".feats"))
  271.          (get_param 'wagon_balance_size clunits_params 0)
  272.          (string-append 
  273.           (get_param 'db_dir clunits_params "./")
  274.           (get_param 'disttabs_dir clunits_params "disttabs/")
  275.           unittype
  276.           (get_param 'disttabs_ext clunits_params ".disttab"))
  277.          (get_param 'wagon_cluster_size clunits_params 10)
  278.          (string-append 
  279.           (get_param 'db_dir clunits_params "./")
  280.           (get_param 'trees_dir clunits_params "trees/")
  281.           unittype
  282.           (get_param 'trees_ext clunits_params ".tree"))
  283.          (get_param 'wagon_other_params clunits_params "")
  284.          )))
  285.     (format t "%s\n" command)
  286.     (system command)))
  287.  
  288. (define (acost:collect_trees unittypes params)
  289. "Collect the trees into one file as an assoc list"
  290.   (let ((fd (fopen 
  291.          (string-append 
  292.           (get_param 'db_dir params "./")
  293.           (get_param 'trees_dir params "trees/")
  294.           (get_param 'index_name params "all.")
  295.           (get_param 'trees_ext params ".tree"))
  296.           "wb"))
  297.     (tree_pref
  298.          (string-append 
  299.           (get_param 'db_dir params "./")
  300.           (get_param 'trees_dir params "trees/")))
  301.     (cluster_prune_limit (get_param 'cluster_prune_limit params 0))
  302.     (cluster_merge (get_param 'cluster_merge params 0)))
  303.     (format fd ";; Autogenerated list of selection trees\n")
  304.     (mapcar
  305.      (lambda (fp)
  306.        (format fd ";; %l %l\n" (car fp) (car (cdr fp))))
  307.      params)
  308.     (format fd "(set! clunits_selection_trees '(\n")
  309.     (mapcar
  310.      (lambda (unit)
  311.        (set! tree (car (load (string-append tree_pref unit ".tree") t)))
  312.        (if (> cluster_prune_limit 0)
  313.        (set! tree (cluster_tree_prune tree cluster_prune_limit)))
  314.        (if (> cluster_merge 0)
  315.        (set! tree (tree_merge_leafs tree cluster_merge)))
  316.        (pprintf (list unit tree) fd))
  317.      unittypes)
  318.     (format fd "))\n")
  319.     (fclose fd)))
  320.  
  321. (define (cluster_tree_prune_in_line prune_limit)
  322. "(cluster_tree_prune_in_line)
  323. Prune number of units in each cluster in each tree *by* prune_limit,
  324. if negative, or *to* prune_limit, if positive."
  325.   (set! sucs_select_trees 
  326.         (mapcar
  327.      (lambda (t)
  328.          (cluster_tree_prune t prune_limit))
  329.      sucs_select_trees)))
  330.  
  331. (define (tree_merge_leafs tree depth)
  332.  "(tree_merge_leafs tree depth)
  333. Merge the leafs of the tree at goven depth.  This allows the trees
  334. to be pruned then the single leafs joined together into larger
  335. clusters (so the viterbi part has something to do)."
  336.  (let ((num_leafs (tree_num_leafs tree)))
  337.    (cond
  338.     ((< num_leafs 2) tree)  ;; already at the foot
  339.     ((< num_leafs depth)
  340.      (tree_collect_leafs tree))
  341.     (t
  342.      (list
  343.       (car tree)
  344.       (tree_merge_leafs (car (cdr tree)) depth)
  345.       (tree_merge_leafs (car (cdr (cdr tree))) depth))))))
  346.  
  347. (define (tree_num_leafs tree)
  348.   "(tree_num_leafs tree)
  349. Number of leafs of given tree."
  350.   (cond
  351.    ((cdr tree)
  352.     (+
  353.      (tree_num_leafs (car (cdr tree)))
  354.      (tree_num_leafs (car (cdr (cdr tree))))))
  355.    (t
  356.     1)))
  357.  
  358. (define (tree_collect_leafs tree)
  359.   "(tree_collect_leafs tree)
  360. Combine all units in the leafs."
  361.   (cond
  362.    ((cdr tree)
  363.     (let ((a (tree_collect_leafs (car (cdr tree))))
  364.       (b (tree_collect_leafs (car (cdr (cdr tree))))))
  365.       (list 
  366.        (list
  367.     (append
  368.      (caar a)
  369.      (caar b))
  370.     10.0))))
  371.    (t
  372.     tree)))
  373.  
  374. (define (cluster_tree_prune tree prune_limit)
  375. "(cluster_tree_prune TREE PRUNE_LIMIT)
  376. Reduce the number of elements in the (CART) tree leaves to PRUNE_LIMIT
  377. removing the ones further from the cluster centre.  Maybe later this should
  378. have guards on minimum number of units that must remain in the tree and
  379. a per unit type limit."
  380.   (cond
  381.    ((cdr tree)  ;; a question
  382.     (list
  383.      (car tree)
  384.      (cluster_tree_prune (car (cdr tree)) prune_limit)
  385.      (cluster_tree_prune (car (cdr (cdr tree))) prune_limit)))
  386.    (t           ;; tree leave
  387.     (list 
  388.      (list
  389.       (remove_n_worst 
  390.        (car (car tree))
  391.        (if (< prune_limit 0)
  392.        (* -1 prune_limit)
  393.        (- (length (car (car tree))) prune_limit)))
  394.       (car (cdr (car tree))))))))
  395.  
  396. (define (remove_n_worst lll togo)
  397. "(remove_n_worst lll togo)
  398. Remove togo worst items from lll."
  399.   (cond
  400.    ((< togo 0)
  401.     lll)
  402.    ((equal? 0 togo)
  403.     lll)
  404.    (t
  405.     (remove_n_worst
  406.      (remove (worst_unit (cdr lll) (car lll)) lll)
  407.      (- togo 1)))))
  408.  
  409. (define (worst_unit lll worst_so_far)
  410. "(worst_unit lll worst_so_far)
  411. Returns unit with worst score in list."
  412.   (cond
  413.    ((null lll)
  414.     worst_so_far)
  415.    ((< (car (cdr worst_so_far)) (car (cdr (car lll))))
  416.     (worst_unit (cdr lll) (car lll)))
  417.    (t
  418.     (worst_unit (cdr lll) worst_so_far))))
  419.  
  420. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  421. ;;; Save the unit catalogue for use in the run-time index
  422.  
  423. (define (acost:save_catalogue utterances clunits_params)
  424.   "(acost:save_catalogue utterances clunits_params)
  425. Save the catalogue with named units with times."
  426.  (let ((fd (fopen 
  427.         (string-append 
  428.          (get_param 'db_dir clunits_params "./")
  429.          (get_param 'catalogue_dir clunits_params "trees/")
  430.          (get_param 'index_name clunits_params "catalogue.")
  431.          ".catalogue")
  432.           "wb"))
  433.        (num_units 0)
  434.        )
  435.    (format fd "EST_File index\n")
  436.    (format fd "DataType ascii\n")
  437.    (format fd "NumEntries %d\n"
  438.        (apply 
  439.         + (mapcar (lambda (u) 
  440.             (length (utt.relation.items u 'Segment))) utterances)))
  441.    (format fd "IndexName %s\n" (get_param 'index_name clunits_params "cluser"))
  442.    (format fd "EST_Header_End\n")
  443.    (mapcar
  444.     (lambda (u)
  445.       (mapcar
  446.        (lambda (s)
  447.      (format fd "%s_%s %s %f %f %f\n"
  448.          (item.feat s "clunit_name")
  449.          (item.feat s 'occurid)
  450.          (utt.feat u 'fileid)
  451.          (item.feat s 'segment_start)
  452.          (item.feat s 'segment_mid)
  453.          (item.feat s 'segment_end)))
  454.        (utt.relation.items u 'Segment)))
  455.     utterances)
  456.    (fclose fd)))
  457.  
  458. (provide 'clunits_build.scm)
  459.