home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: Multimed / Multimed.zip / fest-141.zip / festival / examples / scfg_parse_text.sh < prev    next >
Lisp/Scheme  |  1999-09-09  |  6KB  |  148 lines

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;-*-mode:scheme-*-
  2. ;;                                                                       ;;
  3. ;;                Centre for Speech Technology Research                  ;;
  4. ;;                     University of Edinburgh, UK                       ;;
  5. ;;                       Copyright (c) 1996,1997                         ;;
  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. ;;;           Author:  Alan W Black
  34. ;;;           Date:    October 1997
  35. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  36. ;;;
  37. ;;;  Parse arbitrary text using the given SCFG.  
  38. ;;;
  39. ;;;  Tokenizes given text file and runs the part of speech tagger on it
  40. ;;;  Parses it with respect to a grammar trained from the UPenn WSJ
  41. ;;;  tree bank (you may optionall specify a different grammar).
  42. ;;;
  43. ;;;  This may be slow for long sentences as there is |w|^3 factor
  44. ;;;  involved in parsing algorithm.
  45.  
  46. ;;; Because this is a --script type file I has to explicitly
  47. ;;; load the initfiles: init.scm and user's .festivalrc
  48. (load (path-append libdir "init.scm"))
  49.  
  50. (require 'scfg)
  51.  
  52. ;;; Process command line arguments
  53. (define (scfg_parse_text_help)
  54.   (format t "%s\n"
  55.   "scfg_parse_text [options] textfile
  56.   Parse arbitrary text 
  57.   Options
  58.   -o ofile        File to save parses (default is stdout).
  59.   -grammar ifile  Alternative grammar, default uses standard grammar
  60.                   from Festival distribution.
  61.   -full_parse     Output full parse with probabilities rather than
  62.                   simplified form (which is the default).")
  63.   (quit))
  64.  
  65. ;;; No gc messages
  66. (gc-status nil)
  67.  
  68. ;;; Default argument values
  69. (defvar grammarfile (path-append libdir "scfg_wsj_wp20.gram"))
  70. (defvar outfile "-")
  71. (defvar outfd t)
  72. (defvar parse_type 'brackets_only)
  73. (defvar text_files '("-"))
  74.  
  75. ;;; Get options
  76. (define (get_options)
  77.  
  78.   (let ((files nil)
  79.     (o argv))
  80.     (if (or (member_string "-h" argv)
  81.         (member_string "-help" argv)
  82.         (member_string "--help" argv)
  83.         (member_string "-?" argv))
  84.     (scfg_parse_text_help))
  85.     (while o
  86.       (begin
  87.     (cond
  88.      ((string-equal "-o" (car o))
  89.       (if (not (cdr o))
  90.           (scfg_error "no output file specified"))
  91.       (set! outfile (car (cdr o)))
  92.       (set! outfd (fopen outfile "w"))
  93.       (set! o (cdr o)))
  94.      ((string-equal "-grammar" (car o))
  95.       (if (not (cdr o))
  96.           (scfg_error "no grammar file specified"))
  97.       (set! grammarfile (car (cdr o)))
  98.       (set! o (cdr o)))
  99.      ((string-equal "-full_parse" (car o))
  100.       (set! parse_type 'full_parse))
  101.      (t
  102.       (set! files (cons (car o) files))))
  103.     (set! o (cdr o))))
  104.     (if files
  105.     (set! text_files (reverse files)))))
  106.  
  107. (define (scfg_error message)
  108.   (format stderr "%s: %s\n" "scfg_parse_text" message)
  109.   (scfg_parse_text_help))
  110.  
  111. ;;;  Functions that do the work
  112. (define (find-parse utt)
  113. "Main function for processing TTS utterances.  Tokenizes, predicts POS and
  114. then parses."
  115.   (Token utt)
  116.   (POS utt)
  117.   (Phrasify utt)  ;; cause it maps the POS tags
  118.   (ProbParse utt)
  119. )
  120.  
  121. (define (output-parse utt)
  122. "Output the parse tree for each utt"
  123.  (if (equal? parse_type 'brackets_only)
  124.      (pprintf (scfg_simplify_relation_tree
  125.            (utt.relation_tree utt 'Syntax)) outfd)
  126.      (pprintf (utt.relation_tree utt 'Syntax) outfd))
  127.  (format outfd "\n")
  128.  utt)
  129.  
  130. ;;;
  131. ;;; Redefine what happens to utterances during text to speech 
  132. ;;;
  133. (set! tts_hooks (list find-parse output-parse))
  134.  
  135. (define (main)
  136.   (get_options)
  137.  
  138.   ;; Load the grammar
  139.   (set! scfg_grammar (load grammarfile t))
  140.  
  141.   ;; Parse the files
  142.   (mapcar
  143.    (lambda (f) (tts_file f))
  144.    text_files))
  145.  
  146. ;;;  Do the work
  147. (main)
  148.