Initial import to Gerrit.
[profile/ivi/festival.git] / lib / pos.scm
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
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 ;;;
34 ;;;   A part of speech tagger
35 ;;;
36
37 (set! english_guess_pos
38       '((in of for in on that with by at from as if that against about 
39             before because if under after over into while without
40             through new between among until per up down)
41         (to to)
42         (det the a an no some this that each another those every all any 
43              these both neither no many)
44         (md will may would can could should must ought might)
45         (cc and but or plus yet nor)
46         (wp who what where how when)
47         (pps her his their its our their its mine)
48         (aux is am are was were has have had be)
49         (punc "." "," ":" ";" "\"" "'" "(" "?" ")" "!")
50         ))
51
52 (defvar guess_pos english_guess_pos
53   "guess_pos
54   An assoc-list of simple part of speech tag to list of words in that
55   class.  This basically only contains closed class words all other 
56   words may be assumed to be content words.  This was built from information
57   in the f2b database and is used by the ffeature gpos.")
58
59 ;;;  A more elaborate part of speech tagger using ngrams works but
60 ;;;  at present requires a large list of a priori probabilities
61 ;;;  to work.  If that file exists on your system we'll use it otherwise
62 ;;;  POS is guessed by the lexicon
63
64 ;;;  These models were build from the Penn TreeBank, WSJ corpus
65
66 (defvar pos_model_dir lexdir
67   "pos_model_dir
68   The directory contains the various models for the POS module.  By
69   default this is the same directory as lexdir.  The directory should
70   contain two models: a part of speech lexicon with reverse log probabilities
71   and an ngram model for the same part of speech tag set.")
72
73 (defvar pos_p_start_tag "punc"
74   "pos_p_start_tag
75   This variable's value is the tag most likely to appear before
76   the start of a sentence.  It is used when looking for pos context
77   before an utterance.  Typically it should be some type of punctuation
78   tag.")
79
80 (defvar pos_pp_start_tag "n"
81   "pos_pp_start_tag
82   This variable's value is the tag most likely to appear before
83   pos_p_start_tag and any position preceding that.  It is typically
84   some type of noun tag.  This is used to provide pos context for
85   early words in an utterance.")
86
87 (defvar pos_supported nil
88   "pos_supported
89   If set to non-nil use part of speech prediction, if nil just get
90   pos information from the lexicon.")
91
92 (defvar pos_ngram_name nil
93   "pos_ngram_name
94   The name of a loaded ngram containing the a posteriori ngram model for 
95   predicting part of speech.  The a priori model is held as a 
96   lexicon call poslex.")
97
98 (defvar pos_map nil
99   "pos_map
100   If set this should be a reverse assoc-list mapping on part of speech
101   tag set to another.  It is used after using the defined POS models to
102   map the pos feature on each word to a new tagset.")
103
104 ;;;
105 ;;;  All the names here don't really allow multiple versions
106 ;;;  they should be prefixed with english_
107 ;;;
108
109 (if (probe_file (path-append pos_model_dir "wsj.wp39.poslexR"))
110     (begin
111       (lex.create "english_poslex")
112       (lex.set.compile.file 
113        (path-append pos_model_dir "wsj.wp39.poslexR"))
114       (lex.set.phoneset "mrpa")
115       (lex.set.lts.method nil)
116       (set! pos_lex_name "english_poslex")
117       (set! pos_p_start_tag "punc")
118       (set! pos_pp_start_tag "nn")
119       ;; wp39
120       (lex.add.entry '("_OOV_" ((nnp -2.9144) (jj -2.7357) (nn -3.5787)
121                                 (nns -3.4933) (vbn -3.2486) (vbg -2.9419)
122                                 (vb  -3.5471) (vbd -3.7896) (vbz -3.7820)
123                                 (rb  -4.1940) (vbp -3.2755) (nnps -2.1605))
124                                ()))
125       (lex.add.entry '("_number_" 
126                        ((cd -0.35202) (jj -4.1083) (nns -6.4488) (nnp -7.3595))
127                        () ))
128       (lex.add.entry '("," ((punc -0.88488)) () ))
129       (lex.add.entry '("." ((punc -1.1104)) () ))
130       (lex.add.entry '(":" ((punc -4.4236)) () ))
131       (lex.add.entry '("``" ((punc -2.7867)) () ))
132       (lex.add.entry '("`" ((punc -2.7867)) () ))
133       (lex.add.entry '("'" ((punc -2.7867)) () ))
134       (lex.add.entry '("\"" ((punc -2.7867)) () ))
135       ;; wp17
136 ;;      (lex.add.entry '("_OOV_" ((n -3.4109) (j -2.7892) (v -3.7426)) ()))
137 ;      (lex.add.entry '("_OOV_" ((n -1.968) (j -2.351) (v -2.287)) ()))
138 ;      (lex.add.entry '("_number_" ((j -0.35202)) ()))
139 ;      (lex.add.entry '("," ((punc -0.88359)) () ))
140 ;      (lex.add.entry '("." ((punc -1.1101)) () ))
141 ;      (lex.add.entry '(":" ((punc -4.4236)) () ))
142 ;      (lex.add.entry '("``" ((punc -2.7867)) () ))
143 ;      (lex.add.entry '("`" ((punc -2.7867)) () ))
144 ;      (lex.add.entry '("'" ((punc -2.7867)) () ))
145 ;      (lex.add.entry '("\"" ((punc -2.7867)) () ))
146       ;; wp22
147 ;      (lex.add.entry '("_OOV_" ((n -3.4109) (j -2.7892) (v -3.7426)) ()))
148 ;      (lex.add.entry '("_number_" ((cd -0.35202) (j -4.1908) (n -7.3890)) ()))
149 ;      (lex.add.entry '("," ((punc -0.88359)) () ))
150 ;      (lex.add.entry '("." ((punc -1.1101)) () ))
151 ;      (lex.add.entry '(":" ((punc -4.4236)) () ))
152 ;      (lex.add.entry '("``" ((punc -2.7867)) () ))
153       ;; wp18 
154 ;      (lex.add.entry '("_OOV_" ((n -3.4109) (j -2.7892) (v -3.7426)) ()))
155 ;      (lex.add.entry '("_number_" ((j -0.35202)) ()))
156 ;      (lex.add.entry '("`" ((punc -6.539) ) () ))
157 ;      (lex.add.entry '("``" ((punc -2.399) ) () ))
158 ;      (lex.add.entry '("," ((punc -0.480) ) () ))
159 ;      (lex.add.entry '("." ((fpunc -0.012) ) () ))
160 ;      (lex.add.entry '(":" ((punc -4.100) ) () ))
161
162      (ngram.load 'english_pos_ngram
163             (path-append pos_model_dir  "wsj.wp39.tri.ngrambin"))
164 ;      (ngram.load 'english_pos_ngram
165 ;           (path-append pos_model_dir  "wsj.wp45.tri.ngram"))
166       (set! pos_supported t)
167       )
168     (set! pos_supported nil))
169
170 (setq english_pos_map_wp39_to_wp20
171       '(
172         (( vbd vb vbn vbz vbp vbg ) v)
173         (( nn nnp nns nnps fw sym ls ) n)
174         (( dt ) dt)
175         (( punc fpunc ) punc)
176         (( in ) in)
177         (( jj jjr jjs 1 2 ) j)
178         (( prp ) prp)
179         (( rb rp rbr rbs ) r)
180         (( cc ) cc)
181         (( of ) of)
182         (( to ) to)
183         (( cd ) cd)
184         (( md ) md)
185         (( pos ) pos)
186         (( wdt ) wdt)
187         (( wp ) wp)
188         (( wrb ) wrb)
189         (( ex ) ex)
190         (( uh ) uh)
191         (( pdt ) pdt)
192         ))
193
194 (defvar pos_map nil
195   "pos_map
196 A reverse assoc list of predicted pos tags to some other tag set.  Note
197 using this changes the pos tag loosing the actual predicted value.  Rather
198 than map here you may find it more appropriate to map tags sets locally
199 in the modules that use them (e.g. phrasing and lexicons).")
200
201 ;;(setq pos_map_remap
202 ;;      '(
203 ;;      (( fpunc ) punc)
204 ;;      (( of ) in)))
205
206 (def_feature_docstring 'Word.pos
207   "Word.pos
208   Part of speech tag value returned by the POS tagger module.")
209
210 (def_feature_docstring 'Word.pos_score
211   "Word.pos_score
212   Part of speech tag log likelihood from Viterbi search.")
213
214 (define (POS utt)
215 "(POS utt)                                
216 Apply part of speech tagging (and possible parsing too) to Word
217 relation."
218   (let ((rval (apply_method 'POS_Method utt)))
219     (cond
220      (rval rval) ;; new style
221      (t
222       (Classic_POS utt)))))
223
224
225 (provide 'pos)