~ubuntu-branches/ubuntu/jaunty/speech-tools/jaunty

« back to all changes in this revision

Viewing changes to ling_class/apml.cc

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Urlichs
  • Date: 2004-07-16 09:25:39 UTC
  • mfrom: (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20040716092539-5p1tzif55b8j924e
Tags: 1:1.2.3-8
Added alaw processing code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 /************************************************************************/
 
2
 /*                                                                      */
 
3
 /*                Centre for Speech Technology Research                 */
 
4
 /*                     University of Edinburgh, UK                      */
 
5
 /*                       Copyright (c) 2002                             */
 
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
 /*                 Author: Rob Clark  (robert@cstr.ed.ac.uk)             */
 
35
 /* --------------------------------------------------------------------  */
 
36
 /* Code to read APML format XML as utterances.                           */
 
37
 /*                                                                       */
 
38
 /*************************************************************************/
 
39
 
 
40
#include <stdlib.h>
 
41
#include <stdio.h>
 
42
#include "EST_THash.h"
 
43
#include "EST_error.h"
 
44
#include "apml.h"
 
45
#include "rxp/XML_Parser.h"
 
46
 
 
47
static EST_Regex simpleIDRegex(".*#id(w\\([0-9]+\\))");
 
48
static EST_Regex rangeIDRegex(".*#id(w\\([0-9]+\\)).*id(w\\([0-9]+\\))");
 
49
static EST_Regex RXpunc("[\\.,\\?\\!]+");
 
50
 
 
51
class Parse_State
 
52
  {
 
53
public:
 
54
    int depth;
 
55
    int maxid;
 
56
    EST_Utterance *utt;
 
57
    EST_Relation *words;
 
58
    EST_Relation *perf;
 
59
    EST_Relation *com;
 
60
    EST_Relation *semstruct;
 
61
    EST_Relation *emphasis;
 
62
    EST_Relation *boundary;
 
63
    EST_Item *parent;
 
64
    EST_Item *pending;
 
65
    EST_Item *last_word;
 
66
  };
 
67
 
 
68
class Apml_Parser_Class : public XML_Parser_Class
 
69
{
 
70
protected:
 
71
  virtual void document_open(XML_Parser_Class &c,
 
72
                        XML_Parser &p,
 
73
                        void *data);
 
74
  virtual void document_close(XML_Parser_Class &c,
 
75
                         XML_Parser &p,
 
76
                         void *data);
 
77
  
 
78
  virtual void element_open(XML_Parser_Class &c,
 
79
                       XML_Parser &p,
 
80
                       void *data,
 
81
                       const char *name,
 
82
                       XML_Attribute_List &attributes);
 
83
  virtual void element(XML_Parser_Class &c,
 
84
                  XML_Parser &p,
 
85
                  void *data,
 
86
                  const char *name,
 
87
                  XML_Attribute_List &attributes);
 
88
  virtual void element_close(XML_Parser_Class &c,
 
89
                        XML_Parser &p,
 
90
                        void *data,
 
91
                        const char *name);
 
92
 
 
93
  virtual void pcdata(XML_Parser_Class &c,
 
94
                 XML_Parser &p,
 
95
                 void *data,
 
96
                 const char *chars);
 
97
  virtual void cdata(XML_Parser_Class &c,
 
98
                XML_Parser &p,
 
99
                void *data,
 
100
                const char *chars);
 
101
 
 
102
  virtual void processing(XML_Parser_Class &c,
 
103
                     XML_Parser &p,
 
104
                     void *data,
 
105
                     const char *instruction);
 
106
  virtual void error(XML_Parser_Class &c,
 
107
                XML_Parser &p,
 
108
                void *data);
 
109
};
 
110
 
 
111
static void print_attributes(XML_Attribute_List &attributes)
 
112
{
 
113
  XML_Attribute_List::Entries them;
 
114
 
 
115
  for(them.begin(attributes); them ; them++)
 
116
    printf(" %s='%s'", 
 
117
           (const char *)them->k, 
 
118
           (const char *)them->v);
 
119
}
 
120
 
 
121
EST_read_status apml_read(FILE *file, 
 
122
                             const EST_String &name,
 
123
                             EST_Utterance &u,
 
124
                             int &max_id)
 
125
{
 
126
  (void)max_id;
 
127
  (void)print_attributes;       // just to shut -Wall up.
 
128
  Apml_Parser_Class pclass;
 
129
  Parse_State state;
 
130
 
 
131
  u.clear();
 
132
 
 
133
  state.utt=&u;
 
134
 
 
135
  XML_Parser *parser = pclass.make_parser(file, name, &state);
 
136
  parser->track_context(TRUE);
 
137
 
 
138
  CATCH_ERRORS()
 
139
    return read_format_error;
 
140
 
 
141
  parser->go();
 
142
 
 
143
  END_CATCH_ERRORS();
 
144
 
 
145
  return read_ok;
 
146
}
 
147
 
 
148
 
 
149
 
 
150
/** Now we define the callbacks.
 
151
  */
 
152
 
 
153
void Apml_Parser_Class::document_open(XML_Parser_Class &c,
 
154
                      XML_Parser &p,
 
155
                      void *data)
 
156
{
 
157
  (void)c; (void)p; 
 
158
  Parse_State *state = (Parse_State *)data;
 
159
 
 
160
  state->maxid=0;
 
161
 
 
162
  state->depth=1;
 
163
  state->parent=NULL;
 
164
  state->pending=NULL;
 
165
  state->last_word=NULL;
 
166
 
 
167
  // create relations:
 
168
  state->perf = state->utt->create_relation("Perfomative");
 
169
  state->com = state->utt->create_relation("Communicative");
 
170
  state->words = state->utt->create_relation("Word");
 
171
  state->semstruct = state->utt->create_relation("SemStructure");
 
172
  state->emphasis = state->utt->create_relation("Emphasis");
 
173
  state->boundary = state->utt->create_relation("Boundary");
 
174
 
 
175
 
 
176
}
 
177
 
 
178
void Apml_Parser_Class::document_close(XML_Parser_Class &c,
 
179
                    XML_Parser &p,
 
180
                    void *data)
 
181
{
 
182
  (void)c; (void)p; (void)data;
 
183
}
 
184
 
 
185
 
 
186
void Apml_Parser_Class::element_open(XML_Parser_Class &c,
 
187
                  XML_Parser &p,
 
188
                  void *data,
 
189
                  const char *name,
 
190
                  XML_Attribute_List &attributes)
 
191
{
 
192
  (void)c; (void)p; (void)attributes;
 
193
  Parse_State *state = (Parse_State *)data;
 
194
 
 
195
  //cout << " In element_open: " << name << "\n";
 
196
 
 
197
  if (strcmp(name, "turnallocation")==0)
 
198
    {
 
199
      // currently ignore
 
200
      return;
 
201
    }
 
202
 
 
203
  if (strcmp(name, "apml")==0) 
 
204
    return;  // ignore
 
205
 
 
206
  state->depth++;
 
207
 
 
208
  if( strcmp(name, "performative")==0
 
209
      || strcmp(name, "rheme")==0
 
210
      || strcmp(name, "theme")==0
 
211
      || strcmp(name, "emphasis")==0
 
212
      || strcmp(name, "boundary")==0)
 
213
    {
 
214
      
 
215
      // create new item content
 
216
      EST_Item_Content *cont = new EST_Item_Content();
 
217
      cont->set_name(name);
 
218
          
 
219
      XML_Attribute_List::Entries them;
 
220
      for(them.begin(attributes); them ; them++)
 
221
        {
 
222
          EST_String k = them->k;
 
223
          EST_String v = them->v;
 
224
          cont->f.set(k,v);
 
225
        }
 
226
 
 
227
      EST_Item *item;
 
228
      
 
229
      if( strcmp(name, "emphasis")==0 )
 
230
        {
 
231
          item = state->emphasis->append();
 
232
          state->pending = item;
 
233
        }
 
234
      else if(strcmp(name, "boundary")==0 )
 
235
        {
 
236
          item = state->boundary->append();
 
237
          if(state->last_word)
 
238
            item->append_daughter(state->last_word);
 
239
        }
 
240
      else
 
241
        {
 
242
          if (state->parent == NULL)
 
243
            item = state->semstruct->append();
 
244
          else
 
245
            item = state->parent->append_daughter();
 
246
          state->parent=item;
 
247
        }
 
248
 
 
249
      item->set_contents(cont);
 
250
      
 
251
            
 
252
    }
 
253
  else
 
254
    EST_warning("SOLE XML Parser: unknown element %s", name);
 
255
}
 
256
 
 
257
 
 
258
void Apml_Parser_Class::element(XML_Parser_Class &c,
 
259
                                XML_Parser &p,
 
260
                                void *data,
 
261
                                const char *name,
 
262
                                XML_Attribute_List &attributes)
 
263
{
 
264
  (void)c; (void)p; (void)attributes;
 
265
 
 
266
  element_open(c, p, data, name, attributes);
 
267
  element_close(c, p, data, name);
 
268
}
 
269
 
 
270
 
 
271
void Apml_Parser_Class::element_close(XML_Parser_Class &c,
 
272
                   XML_Parser &p,
 
273
                   void *data,
 
274
                   const char *name)
 
275
{
 
276
  (void)c; (void)p; (void)name;
 
277
  Parse_State *state = (Parse_State *)data;
 
278
 
 
279
  if ( strcmp(name, "emphasis")==0
 
280
       || strcmp(name, "boundary")==0 )
 
281
    {
 
282
      state->depth--;
 
283
      state->pending=NULL;
 
284
    }
 
285
 
 
286
 
 
287
  if (strcmp(name, "performative")==0 
 
288
      || strcmp(name, "theme")==0
 
289
      || strcmp(name, "rheme")==0)
 
290
    {
 
291
      state->depth--;
 
292
      //      state->pending = state->parent;
 
293
      state->pending = NULL;
 
294
      state->parent=state->parent->up();;
 
295
    }
 
296
}
 
297
 
 
298
 
 
299
void Apml_Parser_Class::pcdata(XML_Parser_Class &c,
 
300
            XML_Parser &p,
 
301
            void *data,
 
302
            const char *chars)
 
303
{
 
304
  (void)c; 
 
305
  
 
306
 Parse_State *state = (Parse_State *)data;
 
307
 EST_String strings[255];
 
308
 
 
309
 split(chars,strings,255,RXwhite);
 
310
 
 
311
 //   for(int cc=0 ; cc < 20 ; ++cc)
 
312
 //  cout << cc << ": \"" << strings[cc] << "\" (" << strings[cc].length() << ")\n";
 
313
 
 
314
 int s=0;
 
315
 
 
316
 while( s < 1 || strings[s].length() > 0 )
 
317
   {
 
318
     if(strings[s].length() > 0 )
 
319
       {
 
320
         if(strings[s].matches(RXpunc))
 
321
           {
 
322
             state->last_word->set("punc",strings[s]);
 
323
           }
 
324
         else      
 
325
           {
 
326
             EST_Item_Content *cont = new EST_Item_Content();
 
327
             EST_Item *item;
 
328
             
 
329
             if (state->parent == NULL)
 
330
               item = state->semstruct->append();
 
331
             else
 
332
               item = state->parent->append_daughter();
 
333
             item->set_contents(cont);
 
334
 
 
335
             // strip punc here.
 
336
             EST_String ps = strings[s].at(RXpunc);
 
337
             if( ps.length() > 0 )
 
338
               {
 
339
                 //cout << "Got punc: " << ps << endl;
 
340
                 cont->set_name(strings[s].before(RXpunc));
 
341
                 item->set("punc",ps);
 
342
               }
 
343
                 else
 
344
                   cont->set_name(strings[s]);
 
345
             
 
346
           state->words->append(item);
 
347
           state->last_word = item;
 
348
           
 
349
           if(state->pending)
 
350
             {
 
351
               state->pending->append_daughter(item);
 
352
             }
 
353
           
 
354
           //  if (state->parent != NULL && p.context(0) == "w")
 
355
           //  state->parent->set(EST_String("word"), chars);
 
356
           
 
357
           //cout << "  got word: " << item->name() << "\n";
 
358
           }
 
359
       }
 
360
     ++s;
 
361
   }
 
362
}
 
363
 
 
364
 
 
365
void Apml_Parser_Class::cdata(XML_Parser_Class &c,
 
366
           XML_Parser &p,
 
367
           void *data,
 
368
           const char *chars)
 
369
{
 
370
  (void)c; (void)p; (void)data; (void)chars;
 
371
  // Parse_State *state = (Parse_State *)data;
 
372
 
 
373
  //   printf("SOLE XML Parser [cdata[%s]] %d\n", chars, state->depth);
 
374
}
 
375
 
 
376
 
 
377
void Apml_Parser_Class::processing(XML_Parser_Class &c,
 
378
                XML_Parser &p,
 
379
                void *data,
 
380
                const char *instruction)
 
381
{
 
382
  (void)c; (void)p; 
 
383
  Parse_State *state = (Parse_State *)data;
 
384
 
 
385
  printf("SOLE XML Parser [proc[%s]] %d\n", instruction, state->depth);
 
386
}
 
387
 
 
388
 
 
389
void Apml_Parser_Class::error(XML_Parser_Class &c,
 
390
           XML_Parser &p,
 
391
           void *data)
 
392
{
 
393
  (void)c; (void)p;  (void)data;
 
394
  // Parse_State *state = (Parse_State *)data;
 
395
 
 
396
  EST_error("SOLE XML Parser %s", get_error(p));
 
397
 
 
398
  est_error_throw();
 
399
}
 
400
 
 
401
 
 
402
 
 
403
 
 
404
 
 
405
 
 
406