-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtamguannotator.cxx
1699 lines (1424 loc) · 57.1 KB
/
tamguannotator.cxx
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
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Tamgu (탐구)
*
* Copyright 2019-present NAVER Corp.
* under BSD 3-clause
*/
/* --- CONTENTS ---
Project : Tamgu (탐구)
Version : See tamgu.cxx for the version number
filename : tamguannotator.cxx
Date : 2018/01/25
Purpose :
Programmer : Claude ROUX (claude.roux@naverlabs.com)
Reviewer :
*/
#include "tamgu.h"
#include "predicate.h"
#include "tamguannotator.h"
#include "tamguuvector.h"
#include "tamguvector.h"
#include "tamguivector.h"
#include "tamgutransducer.h"
#include "x_tokenize.h"
#include "tamgusynode.h"
#include "tamgumapuu.h"
//We need to declare once again our local definitions.
Exporting basebin_hash<annotatorMethod> Tamguannotator::methods;
static ThreadLock classlock;
Exporting short Tamguannotator::idtype = 0;
#ifdef UNIX
#define swprintf_s swprintf
#endif
//------------------------------------------------------------------------------------------------------------------
//MethodInitialization will add the right references to "name", which is always a new method associated to the object we are creating
void Tamguannotator::AddMethod(TamguGlobal* global, string name, annotatorMethod func, unsigned long arity, string infos) {
short idname = global->Getid(name);
methods[idname] = func;
if (global->infomethods.find(idtype) != global->infomethods.end() &&
global->infomethods[idtype].find(name) != global->infomethods[idtype].end())
return;
global->infomethods[idtype][name] = infos;
global->RecordArity(idtype, idname, arity);
}
void Tamguannotator::Setidtype(TamguGlobal* global) {
Locking lock(classlock);
if (Tamguannotator::methods.isEmpty())
Tamguannotator::InitialisationModule(global,"");
}
bool Tamguannotator::InitialisationModule(TamguGlobal* global, string version) {
methods.clear();
//Each new object has a specific name, which will help recognize it in the code that will exploit annotator...
Tamguannotator::idtype = global->Getid("annotator");
//You declare your methods here:
// Argument 1 is a call to global, which will record your method into Tamgu (탐구)
// Argument 2 is the name of your command
// Argument 3 links your command with its implementation into the object class
// Argument 4 is a combination of P_... which defines how many parameters this function can accept.
//Argument 5 is a description of this method.
Tamguannotator::AddMethod(global, "_initial", &Tamguannotator::MethodCompile, P_ONE, "_initial(string rules): Compile rules and store them into the annotator.");
Tamguannotator::AddMethod(global, "compile", &Tamguannotator::MethodCompile, P_ONE, "compile(string rules): Compile rules and store them into the annotator.");
Tamguannotator::AddMethod(global, "compilelexicons", &Tamguannotator::MethodCompileLexicons, P_NONE, "compilelexicons(): Compile lexicons.");
Tamguannotator::AddMethod(global, "parse", &Tamguannotator::MethodParse, P_ONE|P_TWO, "parse(ustring tokens,bool keeplex): apply rules to a ustring.");
Tamguannotator::AddMethod(global, "tokenize", &Tamguannotator::MethodTokenize, P_ONE, "tokenize(ustring tokens): apply only lexicons to the string and returns the tokens.");
Tamguannotator::AddMethod(global, "apply", &Tamguannotator::MethodApply, P_ONE|P_TWO, "apply(uvector tokens,bool keeplex): apply rules to a list of tokens.");
Tamguannotator::AddMethod(global, "select", &Tamguannotator::MethodSelect, P_ONE, "select(uvector labels): select the rules, whose label is defined in labels.");
Tamguannotator::AddMethod(global, "clear", &Tamguannotator::MethodClear, P_NONE, "clear(): clear the label selection.");
Tamguannotator::AddMethod(global, "selection", &Tamguannotator::MethodSelection, P_NONE, "selection(): return label selection.");
Tamguannotator::AddMethod(global, "lexicon", &Tamguannotator::MethodLexicon, P_ONE, "lexicon(transducer t): Add a basic dictionary to the process.");
Tamguannotator::AddMethod(global, "checklabel", &Tamguannotator::MethodCheckLabel, P_ONE, "checklabel(ustring label): Check if a label belongs to the annotated text.");
Tamguannotator::AddMethod(global, "checkword", &Tamguannotator::MethodCheckWord, P_ONE, "checkword(ustring wrd): Returns the list of annotations associated to 'wrd' or check if it exists.");
Tamguannotator::AddMethod(global, "labels", &Tamguannotator::MethodLabels, P_NONE, "labels(): Return the list of all labels that were extracted.");
Tamguannotator::AddMethod(global, "words", &Tamguannotator::MethodWords, P_NONE, "words(): Return the list of all words found in the text.");
Tamguannotator::AddMethod(global, "word", &Tamguannotator::MethodWord, P_ONE, "word(ustring w): Check if a word belongs to the text.");
Tamguannotator::AddMethod(global, "token", &Tamguannotator::MethodToken, P_NONE|P_ONE, "token(long idx): Return the token at idx or the current token...");
Tamguannotator::AddMethod(global, "tokens", &Tamguannotator::MethodTokens, P_NONE, "tokens(): Return the list of tokens extracted from the text...");
Tamguannotator::AddMethod(global, "spans", &Tamguannotator::MethodSpans, P_ONE, "spans(bool s): If 's' is true then spans are computed...");
Tamguannotator::AddMethod(global, "dependencies", &Tamguannotator::MethodDependencies, P_NONE | P_ONE, "depedencies(bool clear): Map annotations into dependencies and synodes, when clear is true, clear the knowledge base.");
//We need this code, in order to create new instances of our annotator object... DO NOT ALTER
if (version != "") {
global->newInstance[Tamguannotator::idtype] = new Tamguannotator(global);
global->RecordCompatibilities(Tamguannotator::idtype);
}
return true;
}
Tamgu* Tamguannotator::Put(Tamgu* idx, Tamgu* kval, short idthread) {
//If you want to put some element into your object
if (kval->Type() != Tamguannotator::idtype)
return globalTamgu->Returnerror(e_wrong_assignment_expected);
Tamguannotator* ann = (Tamguannotator*)kval;
if (ownlexicon && lexicon != NULL)
lexicon->Resetreference(reference);
lexicon = ann->lexicon;
if (ann->ownlexicon) {
lexicon->Setreference(reference);
ownlexicon=true;
}
else
ownlexicon=false;
rules = ann->rules;
rules->Setreference(reference);
lexicons = ann->lexicons;
function = ann->function;
return aTRUE;
}
Tamgu* Tamguannotator::Eval(Tamgu* contextualpattern, Tamgu* idx, short idthread) {
//if your object is called from within an expression...
if (contextualpattern == aNULL)
return this;
Tamguuvector* vect= (Tamguuvector*)Selectauvector(contextualpattern);
Locking _lock(this);
if (idthread<=contexts.size())
return this;
vect->values=contexts[idthread].thewords;
return vect;
}
//--------------------------------------------------------------------------------------------------
void An_context::clear(Tamguuvector* v) {
idword=0;
thewords=v->values;
labels.clear();
features.clear();
wordlabels.clear();
classes.clear();
lemmaToword.clear();
wordTolemma.clear();
if (annotator->rules != NULL) {
maxloops=annotator->rules->maxloops;
counters=annotator->rules->maxloops;
}
else {
maxloops=globalTamgu->gTheAnnotationRules->maxloops;
counters=globalTamgu->gTheAnnotationRules->maxloops;
}
for (long i = 0; i< nodes.size(); i++)
nodes[i]->Resetreference();
nodes.clear();
}
void An_context::clean() {
for (long i = 0; i< nodes.size(); i++)
nodes[i]->Resetreference();
}
void An_context::set(An_context& c) {
thewords=c.thewords;
labels = c.labels;
features = c.features;
lemmaToword=c.lemmaToword;
wordlabels.clear();
classes.clear();
wordTolemma = c.wordTolemma;
nodes.clear();
for (long i = 0; i< c.nodes.size(); i++) {
nodes.push_back(c.nodes[i]);
nodes[i]->Setreference();
}
if (annotator->rules != NULL) {
maxloops=annotator->rules->maxloops;
counters=annotator->rules->maxloops;
}
else {
maxloops=globalTamgu->gTheAnnotationRules->maxloops;
counters=globalTamgu->gTheAnnotationRules->maxloops;
}
}
//--------------------------------------------------------------------------------------------------
bool An_rules::storerule(An_rule* r) {
if (r->first->arcs.size()==0)
return false;
An_arc* ar=r->first->arcs[0];
wstring w=ar->atom->value();
if (r->lexicon==true) {
if (r->first->arcs.size()!=1)
return false;
lexiconpresent = true;
//If there are more arcs, then it is a constraint...
if (ar->state!=NULL && ar->state->arcs.size()) {
lexconstraints[r->category][w] = ar->state;
return true;
}
switch(ar->atom->type()) {
case ant_token:
case ant_meta:
lexicons[r->category].parse(w);
return true;
case ant_automaton: {
if (w.find(L" ")!=-1) {
//In this case, we have a multiword expression, to parse it would not make any sense...
//We store it in our multiwords lexicon...
wstring feat=L"\t+";
feat+=r->category;
multiwords.push_back(w);
multiwords.push_back(feat);
}
lexicons[r->category].parse(w);
return true;
}
}
lexarcs[r->category].push_back(ar);
return true;
}
switch(ar->atom->type()) {
case ant_token:
case ant_meta:
case ant_automaton:
break;
default:
starts.push_back(r);
return true;
}
long i;
vector<wstring> wrds;
wrds.push_back(w);
for (i=1; i< r->first->arcs.last;i++) {
w=r->first->arcs[i]->atom->value();
switch(r->first->arcs[i]->atom->type()) {
case ant_token:
case ant_meta:
case ant_automaton:
wrds.push_back(w);
break;
default:
starts.push_back(r);
return true;
}
}
for (i=0;i<wrds.size();i++) {
//this rule starts with a lexicon arc, we store this arc into the lexiconrulecheck automaton
//lexiconrulecheck merges all these specific automata into one...
//We will use it to access lexicon rules through an automaton...
//The rule is still stored in starts, and its index is the actual of starts now...
lexiconrulecheck.compiling(wrds[i],starts.size());
}
r->islexiconrule=true; //this rule will need the entry to have been cleared against lexiconrulecheck to be triggered
starts.push_back(r);
return true;
}
//--------------------------------------------------------------------------------------------------
bool An_arc::scanend() {
if (marque)
return state->isend();
marque=true;
return state->scanend();
}
//we explore the states and find out whether epsilon arcs connect to end state
//in that case, we modify the status of the current node, as it can be an end as well
bool An_state::scanend() {
if (marque)
return isend();
if (!arcs.last) {
status |= an_end;
return true;
}
marque=true;
An_arc* a;
for (long i=0;i<arcs.last;i++) {
a=arcs[i];
if (a->scanend() && a->atom->type() == ant_epsilon)
status|=an_end;
}
return isend();
}
void An_arc::addtotail(An_state* s) {
if (marque)
return;
marque=true;
if (state->arcs.last == 0)
state=s;
else
state->addtotail(s);
}
void An_state::addtotail(An_state* s) {
if (marque)
return;
marque=true;
for (long i=0;i<arcs.last;i++)
arcs[i]->addtotail(s);
if (arcs.last && isfinal())
//in that case, we need to provide an epsilon jump to s...
arcs.push_back(globalTamgu->gTheAnnotationRules->newarc(new An_epsilon,s));
}
bool An_rules::compare(wstring& chr, wstring& action, An_context* context) {
if (lexconstraints.find(action) != lexconstraints.end() && lexconstraints[action].find(chr) != lexconstraints[action].end())
return lexconstraints[action][chr]->apply(chr, context);
if (lexicons.find(action) != lexicons.end()) {
if (lexicons[action].match(chr))
return true;
}
//we will try with the lexarcs...
if (lexarcs.find(action) != lexarcs.end()) {
for (long i=0; i< lexarcs[action].size(); i++) {
if (lexarcs[action][i]->atom->compare(chr,context)==1)
return true;
}
}
return false;
}
//we cehck if "action" is a lemma or a word in the text
char An_token::check(An_context* context) {
//we can also check if the word has a lemma...
if (context->wordTolemma.find(action) == context->wordTolemma.end())
if (context->lemmaToword.find(action) == context->lemmaToword.end())
return false;
return true;
}
char An_token::compare(wstring& chr, An_context* context) {
if (action==chr)
return true;
//we can also check if the word has a lemma...
if (context->wordTolemma.find(chr) != context->wordTolemma.end()) {
for (long i = 0; i< context->wordTolemma[chr].size();i++) {
if (action == context->wordTolemma[chr][i])
return true;
}
}
return false;
}
//We check if our label has been found somewhere in the text
char An_label::check(An_context* context) {
if (context->labels.find(action)!=context->labels.end())
return true;
return false;
}
char An_label::compare(wstring& chr,An_context* context) {
//We need to compare this word against our lexicon...
if (context->annotator->compare(chr,action, context))
return true;
bool act=false;
if (context->labels.find(action)!=context->labels.end()) {
if (context->labels[action].find(chr) != context->labels[action].end())
return true;
act = true;
}
if (context->wordTolemma.find(chr) != context->wordTolemma.end()) {
wstring lem;
for (long i = 0; i< context->wordTolemma[chr].size();i++) {
lem = context->wordTolemma[chr][i];
if (context->annotator->compare(lem,action, context))
return true;
if (act) {
if (context->labels[action].find(lem) != context->labels[action].end())
return true;
}
}
}
return false;
}
//If one is false, it is an error
char An_andlabels::check(An_context* context) {
//We need to compare this word against our lexicon...
for (long i=0;i<actions.size(); i++) {
if (context->labels.find(actions[i])==context->labels.end()) {
if (negations[i]) //if we have a negation then it is still ok for this element
continue;
return false;
}
if (negations[i])
return false;
}
return true;
}
//If one is false, it is an error
char An_andlabels::compare(wstring& chr,An_context* context) {
//We need to compare this word against our lexicon...
for (long i=0;i<actions.size(); i++) {
wstring& action=actions[i];
//Either it is part of the lexicon with this specific label
if (!context->annotator->compare(chr,action, context)) {
//or we try to find it among the other labels...
if (context->labels.find(action)==context->labels.end() || context->labels[action].find(chr) == context->labels[action].end()) {
if (negations[i]) //if we have a negation then it is still ok for this element
continue;
return false;
}
}
if (negations[i])
return false;
}
return true;
}
//if one is true, it is ok
char An_orlabels::check(An_context* context) {
//We need to compare this word against our lexicon...
for (long i=0;i<actions.size(); i++) {
if (context->labels.find(actions[i])!=context->labels.end()) {
if (negations[i]) //this is then false...
continue;
return true;
}
if (negations[i])
return true;
}
return false;
}
//if one is true, it is ok
char An_orlabels::compare(wstring& chr,An_context* context) {
//We need to compare this word against our lexicon...
for (long i=0;i<actions.size(); i++) {
wstring& action=actions[i];
if (!context->annotator->compare(chr,action, context)) {
if (context->labels.find(action)!=context->labels.end() && context->labels[action].find(chr) != context->labels[action].end()) {
if (negations[i]) //this is then false...
continue;
return true;
}
}
if (negations[i])
return true;
}
return false;
}
char An_lemma::check(An_context* context) {
if (context->lemmaToword.find(action)==context->lemmaToword.end())
return false;
return true;
}
char An_lemma::compare(wstring& chr,An_context* context) {
if (context->lemmaToword.find(action)==context->lemmaToword.end())
return false;
if (context->lemmaToword[action].find(chr)==context->lemmaToword[action].end())
return false;
wstring lchr=s_to_lower(chr);
if (context->lemmaToword[action].find(lchr)==context->lemmaToword[action].end())
return false;
return true;
}
An_automaton::An_automaton(string reg) {
action=new Au_automaton;
s_utf8_to_unicode(reference, USTR(reg), reg.size());
if (!action->parse(reference)) {
delete action;
action=NULL;
reference=L"";
}
}
char An_automaton::compare(wstring& chr,An_context* context) {
if (action==NULL)
return false;
return action->match(chr);
}
An_automaton::~An_automaton() {
if (action!=NULL)
delete action;
}
#ifdef Tamgu_REGEX
An_regex::An_regex(wstring& rgx) {
reference=rgx;
action=new wregex(reference);
}
char An_regex::compare(wstring& chr,An_context* context) {
if (action==NULL)
return false;
return regex_match(chr, *action);
}
An_regex::~An_regex() {
if (action!=NULL)
delete action;
}
#endif
//In this case, we can have only one parameter, the context
char An_call::check(An_context* context) {
TamguCallFunction localcall(call);
localcall.arguments.vecteur[0]=context->annotator;
Tamgu* ret=localcall.Eval(aNULL, aNULL, context->idthread);
bool res=ret->Boolean();
ret->Release();
return res;
}
char An_call::compare(wstring& chr,An_context* context) {
TamguCallFunction localcall(call);
Tamguustring* ustr=globalTamgu->Providewithustring(chr);
ustr->Setreference();
localcall.arguments.vecteur[0]=ustr;
localcall.arguments.vecteur[1]=context->annotator;
Tamgu* ret=localcall.Eval(aNULL, aNULL, context->idthread);
bool res=ret->Boolean();
ret->Release();
ustr->Resetreference();
return res;
}
//--------------------------------------------------------------------------
void An_state::setmax(short m) {
idmax=globalTamgu->gTheAnnotationRules->maxloops.size();
globalTamgu->gTheAnnotationRules->maxloops.push_back(m);
}
//applying rules globally...
bool An_arc::apply(An_context* context) {
switch(atom->check(context)) {
case 0:
if ((status&an_negation)==an_negation)
return state->apply(context);
return false;
case 1:
if ((status&an_negation)==an_negation)
return false;
return state->apply(context);
case 2:
if ((status&an_negation)==an_negation)
return false;
return state->apply(context);
}
return false;
}
bool An_state::apply(An_context* context) {
if (idmax!=-1) {
if (context->check(idmax)==false)
return -1;
}
for (long l=0;l<arcs.last;l++) {
if (arcs[l]->apply(context))
return true;
}
if ((status&an_end)==an_end)
return true;
return false;
}
//applying rules to a vector...
long An_arc::apply(vector<wstring>& words, long i,An_context* context) {
if (i==words.size())
return -1;
long ret=-1;
switch(atom->compare(words[i],context)) {
case 0:
if ((status&an_negation)==an_negation) {
context->idword=i+1;
ret=state->apply(words,i+1,context);
context->idword=i;
}
return ret;
case 1:
if ((status&an_negation)==an_negation)
return -1;
context->idword=i+1;
ret=state->apply(words,i+1,context);
context->idword=i;
return ret;
case 2:
if ((status&an_negation)==an_negation)
return -1;
return state->apply(words,i,context);
}
return -1;
}
long An_state::apply(vector<wstring>& words, long i,An_context* context) {
if (idmax!=-1) {
if (context->check(idmax)==false)
return -1;
}
long ret;
for (long l=0;l<arcs.last;l++) {
ret=arcs[l]->apply(words,i,context);
if (ret!=-1)
return ret;
}
if ((status&an_end)==an_end)
return i;
return -1;
}
//applying rules to a vector...
long An_arc::apply(vector<wstring>& words, long i, vector<long>& kept,An_context* context) {
if (i==words.size())
return -1;
long ret=0;
switch(atom->compare(words[i],context)) {
case 0:
if ((status&an_negation)==an_negation) {
context->idword=i+1;
ret=state->apply(words,i+1,kept,context);
context->idword=i;
}
return -1;
case 1:
if ((status&an_negation)==an_negation)
return -1;
context->idword=i+1;
ret=state->apply(words,i+1,kept,context);
context->idword=i;
break;
case 2:
ret=state->apply(words,i,kept,context);
break;
}
if (ret!=-1) {
if ((status&an_remove)==0)
kept.push_back(i);
}
return ret;
}
long An_state::apply(vector<wstring>& words, long i, vector<long>& kept,An_context* context) {
if (idmax!=-1) {
if (context->check(idmax)==false)
return -1;
}
long ret;
for (long l=0;l<arcs.last;l++) {
ret=arcs[l]->apply(words,i, kept,context);
if (ret!=-1)
return ret;
}
if ((status&an_end)==an_end)
return i;
return -1;
}
//applying rules to a vector...
bool An_arc::apply(wstring& word, An_context* context) {
switch(atom->compare(word,context)) {
case 0:
if ((status&an_negation)==an_negation)
return state->apply(word,context);
return false;
case 1:
if ((status&an_negation)==an_negation)
return false;
return state->apply(word,context);
case 2:
return state->apply(word,context);
}
return false;
}
bool An_state::apply(wstring& word, An_context* context) {
if (idmax!=-1) {
if (context->check(idmax)==false)
return false;
}
for (long l=0;l<arcs.last;l++) {
if (arcs[l]->apply(word,context))
return true;
}
if ((status&an_end)==an_end)
return true;
return false;
}
bool An_rule::applyclass(An_context* context) {
return first->apply(context);
}
//---------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------
/*
Different cases:
Tokenization can be handled in different ways:
1) Through a lexicon
2) Through x_wtokenize
Parse with a Lexicon
The lexicon can have been loaded with "lexicon(transducer t)" (loadedlexicon is then true).
However, if none was loaded, then we create one on the fly.
The multiwords expressions are then merged into this lexicon, which will be able to recognize multiword expressions provided by the user
at parse time.
Parse with x_wtokenize:
If not external lexicon was provided (loadedlexicon is then false) and no multiword expressions were provided (lexicon->automaton == NULL)
then we will use x_wtokenize to do the tokenization (non linguistic parsing). In that case, we set compiledlexicon to 3.
*/
Tamgu* Tamguannotator::lexiconcompiling() {
if (compiledlexicon)
return aFALSE;
long i;
//First case, lexicon is NULL but multiwords is not...
if (lexicon == NULL) {
lexicon = new Tamgutransducer(globalTamgu);
lexicon->Setreference(reference);
compiledlexicon = 2;
ownlexicon = true;
}
if (globalTamgu->gTheAnnotationRules != NULL && globalTamgu->gTheAnnotationRules->multiwords.size()) {
if (!lexicon->modified) {
for (i=0;i<globalTamgu->gTheAnnotationRules->multiwords.size();i+=2)
lexicon->compilergx(globalTamgu->gTheAnnotationRules->multiwords[i],globalTamgu->gTheAnnotationRules->multiwords[i+1]);
lexicon->modified = true;
}
compiledlexicon = true;
loadedlexicon = true;
}
if (globalTamgu->gTheAnnotationRules != rules && rules->multiwords.size()) {
//We merge it into our main lexicon
for (i=0;i<rules->multiwords.size();i+=2)
lexicon->compilergx(rules->multiwords[i],rules->multiwords[i+1]);
rules->multiwords.clear();
compiledlexicon = true;
loadedlexicon = true;
}
if (!loadedlexicon || lexicon->automaton == NULL)
compiledlexicon = 3;
if (rules != NULL && rules->lexiconpresent) {
checklexicon = 1;
if (lexicons != rules && lexicons->lexiconpresent)
checklexicon = 3;
}
else {
if (lexicons != rules && lexicons->lexiconpresent)
checklexicon = 2;
}
return aTRUE;
}
Tamgu* Tamguannotator::MethodCompileLexicons(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
Initrules();
return lexiconcompiling();
}
Tamgu* Tamguannotator::MethodCompile(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
Initrules();
string code = callfunc->Evaluate(0,contextualpattern,idthread)->String();
An_rules* old = globalTamgu->gTheAnnotationRules;
if (rules == NULL || rules == globalTamgu->gTheAnnotationRules) {
rules = new An_rules(globalTamgu);
rules->Setreference(reference);
}
globalTamgu->gTheAnnotationRules = rules;
rules = globalTamgu->EvaluateRules(code, idthread);
if (rules == NULL)
return globalTamgu->Errorobject(idthread);
if (old != NULL)
globalTamgu->gTheAnnotationRules = old;
if (rules->lexiconpresent) {
checklexicon = 1;
if (lexicons != rules && lexicons->lexiconpresent)
checklexicon = 3;
}
else {
if (lexicons != NULL && lexicons != rules && lexicons->lexiconpresent)
checklexicon = 2;
}
return aTRUE;
}
Tamgu* Tamguannotator::MethodLexicon(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
Tamgu* tlexicon = callfunc->Evaluate(0,contextualpattern,idthread);
if (tlexicon->Type()== Tamgutransducer::idtype) {
if (ownlexicon && lexicon != NULL)
lexicon->Resetreference(reference);
lexicon= (Tamgutransducer*)tlexicon;
//it can be modified only once
loadedlexicon = true;
return aTRUE;
}
return globalTamgu->Returnerror(e_wrong_argument_expecting);
}
Tamgu* Tamguannotator::MethodCheckLabel(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
Locking _lock(this);
wstring w=callfunc->Evaluate(0,contextualpattern,idthread)->UString();
if (contexts[idthread].labels.find(w)!=contexts[idthread].labels.end())
return aTRUE;
return aFALSE;
}
Tamgu* Tamguannotator::MethodCheckWord(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
Locking _lock(this);
wstring w=callfunc->Evaluate(0,contextualpattern,idthread)->UString();
if (contextualpattern->isContainer()) {
Tamguuvector* uvect=(Tamguuvector*)Selectauvector(contextualpattern);
for (const auto& e: contexts[idthread].labels) {
if (e.second.find(w) == e.second.end())
uvect->storevalue(e.first);
}
return uvect;
}
if (contexts[idthread].wordTolemma.find(w) != contexts[idthread].wordTolemma.end())
return aTRUE;
return aFALSE;
}
Tamgu* Tamguannotator::MethodLabels(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
Locking _lock(this);
Tamgu* vect=Selectauvector(contextualpattern);
for (const auto& a: contexts[idthread].labels)
vect->storevalue(a.first);
return vect;
}
Tamgu* Tamguannotator::MethodWords(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
Locking _lock(this);
Tamgu* vect=Selectauvector(contextualpattern);
for (const auto& l: contexts[idthread].lemmaToword) {
vect->storevalue(l.first);
for (const auto& w: l.second)
vect->storevalue(w.first);
}
return vect;
}
Tamgu* Tamguannotator::MethodWord(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
Locking _lock(this);
wstring w=callfunc->Evaluate(0,contextualpattern,idthread)->UString();
if (contexts[idthread].wordTolemma.find(w) != contexts[idthread].wordTolemma.end())
return aTRUE;
if (contexts[idthread].lemmaToword.find(w) != contexts[idthread].lemmaToword.end())
return aTRUE;
return aFALSE;
}
Tamgu* Tamguannotator::MethodTokens(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
Locking _lock(this);
Tamguuvector* vect= (Tamguuvector*)Selectauvector(contextualpattern);
vect->values=contexts[idthread].thewords;
return vect;
}
Tamgu* Tamguannotator::MethodToken(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
Locking _lock(this);
if (contexts[idthread].idword >= contexts[idthread].thewords.size())
return aNULL;
if (callfunc->Size()==0) {
if (contextualpattern->isNumber())
return globalTamgu->ProvideConstint(contexts[idthread].idword);
return globalTamgu->Provideustring(contexts[idthread].thewords[contexts[idthread].idword]);
}
long idx=callfunc->Evaluate(0,contextualpattern,idthread)->Integer();
if (idx<0 || idx >=contexts[idthread].thewords.size())
return aNULL;
if (contextualpattern->isNumber())
return globalTamgu->ProvideConstint(idx);
return globalTamgu->Provideustring(contexts[idthread].thewords[idx]);
}
Tamgu* Tamguannotator::MethodSelection(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
Tamgu* uvect=Selectauvector(contextualpattern);
for (const auto& s: selections)
uvect->storevalue(s.first);
return uvect;
}
//---------------------------------------------------------------------------------------------------
//We transform each word into a synode, with labels both as dependencies and features...
Tamgu* Tamguannotator::MethodDependencies(Tamgu* contextualpattern, short idthread, TamguCall* callfunc) {
//first for each token, we crate a synode...
Tamgusynode* syn;
Tamgumapuu feats;
An_context& cnt = contexts[idthread];
vector<wstring>& wrds = cnt.thewords;
long sz = wrds.size();
long l;
wchar_t buff[]={'l','e','m','m','a','0',0};
wchar_t idx[] = {0,0,0,0,0};