alphabet.C
23.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
/*******************************************************************/
/* */
/* FILE alphabet.C */
/* MODULE alphabet */
/* PROGRAM SFST */
/* AUTHOR Helmut Schmid, IMS, University of Stuttgart */
/* */
/* PURPOSE basic FST functions */
/* */
/*******************************************************************/
#include <climits>
#include <cstring>
#include "utf8.h"
#include "alphabet.h"
using std::vector;
using std::ostream;
const int BUFFER_SIZE=100000;
char EpsilonString[]="<>";
/*******************************************************************/
/* */
/* Alphabet::add */
/* */
/*******************************************************************/
void Alphabet::add( const char *symbol, Character c )
{
char *s = fst_strdup(symbol);
cm[c] = s;
sm[s] = c;
}
/*******************************************************************/
/* */
/* Alphabet::Alphabet */
/* */
/*******************************************************************/
Alphabet::Alphabet()
{
utf8 = false;
add(EpsilonString, Label::epsilon);
}
/*******************************************************************/
/* */
/* Alphabet::clear */
/* */
/*******************************************************************/
void Alphabet::clear()
{
char **s=new char*[cm.size()];
ls.clear();
sm.clear();
size_t i, n=0;
for( CharMap::iterator it=cm.begin(); it!=cm.end(); it++ )
s[n++] = it->second;
cm.clear();
for( i=0; i<n; i++ )
free(s[i]);
delete[] s;
}
/*******************************************************************/
/* */
/* Alphabet::new_marker */
/* */
/*******************************************************************/
Character Alphabet::new_marker()
{
// find some unused character code
for(Character i=1; i!=0; i++)
if (cm.find(i) == cm.end()) {
// create a unique identifier string
char symbol[100];
sprintf(symbol,">%ld<",(long)i);
add(symbol, i);
return i;
}
throw "Error: too many symbols in transducer definition";
}
/*******************************************************************/
/* */
/* is_marker_symbol */
/* */
/*******************************************************************/
static bool is_marker_symbol( const char *s )
{
// recogize strings matching the expression ">[0-9]+<"
if (s != NULL && *s == '>') {
do { s++; } while (*s >= '0' && *s <= '9');
if (*s=='<' && *(s+1) == 0 && *(s-1) != '>')
return true;
}
return false;
}
/*******************************************************************/
/* */
/* Alphabet::delete_markers */
/* */
/*******************************************************************/
void Alphabet::delete_markers()
{
vector<char*> sym;
vector<Character> code;
vector<Label> label;
for( CharMap::const_iterator it=cm.begin(); it!=cm.end(); it++ ) {
Character c=it->first;
char *s=it->second;
if (!is_marker_symbol(s)) {
sym.push_back(fst_strdup(s));
code.push_back(c);
}
}
for( LabelSet::const_iterator it=begin(); it!=end(); it++ ) {
Label l=*it;
if (!is_marker_symbol(code2symbol(l.upper_char())) &&
!is_marker_symbol(code2symbol(l.lower_char())))
label.push_back(l);
}
clear();
for( size_t i=0; i<sym.size(); i++ ) {
add_symbol(sym[i], code[i]);
free(sym[i]);
}
for( size_t i=0; i<label.size(); i++ )
insert( label[i] );
}
/*******************************************************************/
/* */
/* Alphabet::add_symbol */
/* */
/*******************************************************************/
Character Alphabet::add_symbol(const char *symbol)
{
if (sm.find(symbol) != sm.end())
return sm[symbol];
// assign the symbol to some unused character
for(Character i=1; i!=0; i++)
if (cm.find(i) == cm.end()) {
add(symbol, i);
return i;
}
throw "Error: too many symbols in transducer definition";
}
/*******************************************************************/
/* */
/* Alphabet::add_symbol */
/* */
/*******************************************************************/
void Alphabet::add_symbol( const char *symbol, Character c )
{
// check whether the symbol was previously defined
int sc=symbol2code(symbol);
if (sc != EOF) {
if ((Character)sc == c)
return;
if (strlen(symbol) < 60) {
static char message[100];
sprintf(message, "Error: reinserting symbol '%s' in alphabet with incompatible character value %u %u", symbol, (unsigned)sc, (unsigned)c);
throw message;
}
else
throw "reinserting symbol in alphabet with incompatible character value";
}
// check whether the character is already in use
const char *s=code2symbol(c);
if (s == NULL)
add(symbol, c);
else {
if (strcmp(s, symbol) != 0) {
static char message[100];
if (strlen(symbol) < 70)
sprintf(message,"Error: defining symbol %s as character %d (previously defined as %s)", symbol, (unsigned)c, s);
else
sprintf(message,"Error: defining a (very long) symbol with previously used character");
throw message;
}
}
}
/*******************************************************************/
/* */
/* Alphabet::write_char */
/* */
/*******************************************************************/
void Alphabet::write_char( Character c, char *buffer, int *pos,
bool with_brackets) const
{
const char *s = code2symbol(c);
if (s) {
int i = 0;
int l=(int)strlen(s)-1;
if (!with_brackets && s[i] == '<' && s[l] == '>') { i++; l--; }
while (i <= l)
buffer[(*pos)++] = s[i++];
}
else {
unsigned int uc = c;
if (uc>=32 && uc<256)
buffer[(*pos)++] = (char)c;
else {
sprintf(buffer+(*pos),"\\%u", uc);
*pos += (int)strlen(buffer+(*pos));
}
}
buffer[*pos] = '\0';
}
/*******************************************************************/
/* */
/* Alphabet::write_char */
/* */
/*******************************************************************/
const char *Alphabet::write_char( Character c, bool with_brackets ) const
{
static char buffer[1000];
int n=0;
write_char( c, buffer, &n, with_brackets );
return buffer;
}
/*******************************************************************/
/* */
/* Alphabet::write_label */
/* */
/*******************************************************************/
void Alphabet::write_label( Label l, char *buffer, int *pos,
bool with_brackets ) const
{
Character lc=l.lower_char();
Character uc=l.upper_char();
write_char( lc, buffer, pos, with_brackets );
if (lc != uc) {
buffer[(*pos)++] = ':';
write_char( uc, buffer, pos, with_brackets );
}
}
/*******************************************************************/
/* */
/* Alphabet::write_label */
/* */
/*******************************************************************/
const char *Alphabet::write_label( Label l, bool with_brackets ) const
{
static char buffer[1000];
int n=0;
write_label( l, buffer, &n, with_brackets );
return buffer;
}
/*******************************************************************/
/* */
/* Alphabet::insert_symbols */
/* */
/*******************************************************************/
void Alphabet::insert_symbols( const Alphabet &a )
{
for( CharMap::const_iterator it=a.cm.begin(); it!=a.cm.end(); it++ )
add_symbol(it->second, it->first);
}
/*******************************************************************/
/* */
/* Alphabet::complement */
/* */
/*******************************************************************/
void Alphabet::complement( vector<Character> &sym )
{
vector<Character> result;
for( CharMap::const_iterator it=cm.begin(); it!=cm.end(); it++ ) {
Character c = it->first;
if (c != Label::epsilon) {
size_t i;
for( i=0; i<sym.size(); i++ )
if (sym[i] == c)
break;
if (i == sym.size())
result.push_back(c);
}
}
sym.swap(result);
}
/*******************************************************************/
/* */
/* Alphabet::copy */
/* */
/*******************************************************************/
void Alphabet::copy( const Alphabet &a )
{
insert_symbols( a );
utf8 = a.utf8;
for( LabelSet::const_iterator it=a.begin(); it!=a.end(); it++ )
ls.insert( *it );
}
/*******************************************************************/
/* */
/* Alphabet::compose */
/* */
/*******************************************************************/
void Alphabet::compose( const Alphabet &la, const Alphabet &ua )
{
// insert the symbols
insert_symbols(la);
insert_symbols(ua);
utf8 = la.utf8;
hash_map<Character, hash_set<Character> > cs;
// create a hash table for a quick lookup of the target characters
for( iterator it=ua.begin(); it!=ua.end(); it++ ) {
Character lc=it->lower_char();
if (lc == Label::epsilon)
insert(*it);
else
cs[lc].insert(it->upper_char());
}
for( iterator it=la.begin(); it!=la.end(); it++ ) {
Character uc=it->upper_char();
if (uc == Label::epsilon)
insert(*it);
else {
if (cs.find(uc) != cs.end()) {
hash_set<Character> s=cs[uc];
Character lc=it->lower_char();
for( hash_set<Character>::iterator it=s.begin(); it!=s.end(); it++)
insert(Label(lc, *it));
}
}
}
}
/*******************************************************************/
/* */
/* operator<<(Alphabet) */
/* */
/*******************************************************************/
ostream &operator<<( ostream &s, const Alphabet &a )
{
for( Alphabet::CharMap::const_iterator it=a.cm.begin(); it!=a.cm.end(); it++ )
s << it->first << " -> " << it->second << "\n";
for( Alphabet::iterator it=a.begin(); it!=a.end(); it++ )
s << a.write_label(*it) << " ";
s << "\n";
return s;
}
/*******************************************************************/
/* */
/* Alphabet::next_mcsym */
/* */
/* recognizes multi-character symbols which are enclosed with */
/* angle brackets <...>. If the argument flag insert is true, */
/* the multi-character symbol must be already in the lexicon in */
/* order to be recognized. */
/* */
/*******************************************************************/
int Alphabet::next_mcsym( char* &string, bool insert )
{
char *start=string;
if (*start == '<')
// symbol might start here
for( char *end=start+1; *end; end++ )
if (*end == '>') {
// matching pair of angle brackets found
// mark the end of the substring with \0
char lastc = *(++end);
*end = 0;
int c;
if (insert)
c = add_symbol( start );
else
c = symbol2code(start);
// restore the original string
*end = lastc;
if (c != EOF) {
// symbol found
// return its code
string = end;
return (Character)c;
}
else
// not a complex character
break;
}
return EOF;
}
/*******************************************************************/
/* */
/* Alphabet::next_code */
/* */
/*******************************************************************/
int Alphabet::next_code( char* &string, bool extended, bool insert )
{
if (*string == 0)
return EOF; // finished
int c = next_mcsym(string, insert);
if (c != EOF)
return c;
if (extended && *string == '\\')
string++; // remove quotation
if (utf8) {
unsigned int c = utf8toint( &string );
return (int)add_symbol(int2utf8(c));
}
else {
char buffer[2];
buffer[0] = *string;
buffer[1] = 0;
string++;
return (int)add_symbol(buffer);
}
}
/*******************************************************************/
/* */
/* Alphabet::next_label */
/* */
/*******************************************************************/
Label Alphabet::next_label( char* &string, bool extended )
{
// read first character
int c = next_code( string, extended );
if (c == EOF)
return Label(); // end of string reached
Character lc=(Character)c;
if (!extended || *string != ':') { // single character?
if (lc == Label::epsilon)
return next_label(string, extended); // ignore epsilon
return Label(lc);
}
// read second character
string++; // jump over ':'
c = next_code( string );
if (c == EOF) {
static char buffer[1000];
sprintf(buffer,"Error: incomplete symbol in input file: %s", string);
throw buffer;
}
Label l(lc, (Character)c);
if (l.is_epsilon())
return next_label(string, extended); // ignore epsilon transitions
return l;
}
/*******************************************************************/
/* */
/* Alphabet::string2symseq */
/* */
/*******************************************************************/
void Alphabet::string2symseq( char *s, vector<Character> &ch )
{
int c;
while ((c = next_code(s, false)) != EOF)
ch.push_back((Character)c);
}
/*******************************************************************/
/* */
/* Alphabet::string2labelseq */
/* */
/*******************************************************************/
void Alphabet::string2labelseq( char *s, vector<Label> &labels )
{
Label l;
while ((l = next_label(s)) != Label::epsilon)
labels.push_back(l);
}
/*******************************************************************/
/* */
/* Alphabet::store */
/* */
/*******************************************************************/
void Alphabet::store( FILE *file ) const
{
char c=(utf8)? (char)1: (char)0;
fputc(c, file);
// write the symbol mapping
Character n=(Character)cm.size();
fwrite(&n, sizeof(n), 1, file);
for( CharMap::const_iterator it=cm.begin(); it!=cm.end(); it++ ) {
Character c=it->first;
char *s=it->second;
fwrite(&c, sizeof(c), 1, file);
fwrite(s, sizeof(char), strlen(s)+1, file);
}
// write the character pairs
n = (Character)size();
fwrite(&n, sizeof(n), 1, file);
for( LabelSet::const_iterator p=ls.begin(); p!=ls.end(); p++ ) {
Character c=p->lower_char();
fwrite(&c, sizeof(c), 1, file);
c = p->upper_char();
fwrite(&c, sizeof(c), 1, file);
}
if (ferror(file))
throw "Error encountered while writing alphabet to file\n";
}
/*******************************************************************/
/* */
/* Alphabet::read */
/* */
/*******************************************************************/
void Alphabet::read( FILE *file )
{
utf8 = (fgetc(file) != 0);
// read the symbol mapping
Character n=0;
read_num(&n, sizeof(n), file);
for( unsigned i=0; i<n; i++) {
char buffer[BUFFER_SIZE];
Character c;
read_num(&c, sizeof(c), file);
if (!read_string(buffer, BUFFER_SIZE, file) ||
feof(file) || ferror(file))
throw "Error1 occurred while reading alphabet!\n";
add_symbol(buffer, c);
}
// read the character pairs
read_num(&n, sizeof(n), file);
if (ferror(file))
throw "Error2 occurred while reading alphabet!\n";
for( unsigned i=0; i<n; i++) {
Character lc, uc;
read_num(&lc, sizeof(lc), file);
read_num(&uc, sizeof(uc), file);
insert(Label(lc, uc));
}
if (ferror(file))
throw "Error3 occurred while reading alphabet!\n";
}
/*******************************************************************/
/* */
/* Alphabet::compute_score */
/* */
/*******************************************************************/
int Alphabet::compute_score( Analysis &ana )
{
// check whether the morpheme boundaries are explicitly marked
// with <X> tags
int score=0;
for( size_t i=0; i<ana.size(); i++ ) {
// get next symbol
const char *sym=write_char(ana[i].lower_char());
if (strcmp(sym,"<X>") == 0)
score--;
}
if (score < 0)
return score;
// No explicit morpheme boundary markers have been found.
// Count the number of part-of-speech and PREF tags.
for( size_t i=0; i<ana.size(); i++ ) {
// get next symbol
const char *sym=write_char(ana[i].lower_char());
// Is it not a multi-character symbol
if (sym[0] != '<' || sym[1] == 0)
continue;
// Is it a POS tag starting with "+" like <+NN>?
if (sym[1] == '+') {
const char *t=sym+2;
for( ; *t >= 'A' && *t <= 'Z'; t++) ;
if (t > sym+2 && *t == '>')
return score;
}
// Is it a potential POS tag (i.e. all uppercase)?
const char *t = sym+1;
for( ; *t >= 'A' && *t <= 'Z'; t++) ;
if (t == sym+1 || *t != '>')
continue;
// uppercase symbol found
if (strcmp(sym,"<SUFF>") == 0 ||
strcmp(sym,"<OLDORTH>") == 0 ||
strcmp(sym,"<NEWORTH>") == 0)
continue; // not what we are looking for
// disprefer nouns with prefixes
if (strcmp(sym,"<PREF>") == 0)
score-=2;
if (strcmp(sym,"<V>") == 0 || strcmp(sym,"<ADJ>") == 0) {
bool is_verb=(strcmp(sym,"<V>")==0);
// get the next non-empty symbol
Character c=Label::epsilon;
size_t k;
for( k=i+1; k<ana.size(); k++ )
if ((c = ana[k].lower_char()) != Label::epsilon)
break;
// Is it a participle
if (c != Label::epsilon) {
sym = write_char(c);
if (strcmp(sym,"<OLDORTH>") == 0 || strcmp(sym,"<NEWORTH>") == 0) {
for( k++; k<ana.size(); k++ )
if ((c = ana[k].lower_char()) != Label::epsilon)
break;
if (c != Label::epsilon)
sym = write_char(c);
}
if (is_verb &&
(strcmp(sym,"<PPres>") == 0 || strcmp(sym,"<PPast>") == 0))
continue; // don't consider participles as complex
if (!is_verb &&
(strcmp(sym,"<Sup>") == 0 || strcmp(sym,"<Comp>") == 0))
continue; // don't consider participles as complex
}
}
score--;
}
return score;
}
/*******************************************************************/
/* */
/* Alphabet::disambiguate */
/* */
/*******************************************************************/
void Alphabet::disambiguate( vector<Analysis> &analyses )
{
// compute the scores
int bestscore=INT_MIN;
vector<int> score;
for( size_t i=0; i<analyses.size(); i++ ) {
score.push_back(compute_score(analyses[i]));
if (bestscore < score[i])
bestscore = score[i];
}
// delete suboptimal analyses
size_t k=0;
for( size_t i=0; i<analyses.size(); i++ )
if (score[i] == bestscore)
analyses[k++] = analyses[i];
analyses.resize(k);
}
/*******************************************************************/
/* */
/* Alphabet::print_analysis */
/* */
/*******************************************************************/
char *Alphabet::print_analysis( Analysis &ana, bool both_layers )
{
vector<char> ch;
// for each transition
for( size_t i=0; i<ana.size(); i++ ) {
// get the transition label
Label l=ana[i];
const char *s;
// either print the analysis symbol or the whole label
if (both_layers) {
s = write_label(l);
// quote colons
if (strcmp(s,":") == 0)
ch.push_back('\\');
}
else if (l.lower_char() != Label::epsilon)
s = write_char(l.lower_char());
else
continue;
// copy the characters to the character array
while (*s)
ch.push_back(*(s++));
}
ch.push_back(0); // terminate the string
static char *result=NULL;
if (result != NULL)
delete[] result;
result = new char[ch.size()];
for( size_t i=0; i<ch.size(); i++ )
result[i] = ch[i];
return result;
}