-
Notifications
You must be signed in to change notification settings - Fork 11
/
mbma_mod.cxx
1185 lines (1122 loc) · 32.5 KB
/
mbma_mod.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
/* ex: set tabstop=8 expandtab: */
/*
Copyright (c) 2006 - 2024
CLST - Radboud University
ILK - Tilburg University
This file is part of frog:
A Tagger-Lemmatizer-Morphological-Analyzer-Dependency-Parser for
several languages
frog is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
frog is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http:https://www.gnu.org/licenses/>.
For questions and suggestions, see:
https://github.com/LanguageMachines/frog/issues
or send mail to:
lamasoftware (at ) science.ru.nl
*/
#include "frog/mbma_mod.h"
#include <cstdlib>
#include <string>
#include <set>
#include <iostream>
#include <algorithm>
#include <fstream>
#include "timbl/TimblAPI.h"
#include "ticcutils/Configuration.h"
#include "ticcutils/PrettyPrint.h"
#include "ticcutils/StringOps.h"
#include "ticcutils/Unicode.h"
#include "ticcutils/SocketBasics.h"
#include "ticcutils/json.hpp"
#include "frog/Frog-util.h"
#include "frog/FrogData.h"
using namespace std;
using namespace icu;
using namespace nlohmann;
using TiCC::operator<<;
const long int LEFT = 6; // left context
const long int RIGHT = 6; // right context
#define LOG *TiCC::Log(errLog)
#define DBG *TiCC::Log(dbgLog)
Mbma::Mbma( TiCC::LogStream *errlog, TiCC::LogStream *dbglog ):
MTree(0),
filter(0),
debugFlag(0),
filter_diac(false),
doDeepMorph(false)
{
/// create an Mbma classifier object
/*!
\param errlog the LogStream to use for Error messages
\param dbglog the LogStream to use for Debug messages
*/
errLog = new TiCC::LogStream( errlog, "mbma-" );
if ( dbglog ){
dbgLog = new TiCC::LogStream( dbglog, "mbma-" );
}
else {
dbgLog = errLog;
}
}
// define the statics
map<UnicodeString,UnicodeString> Mbma::TAGconv;
string Mbma::mbma_tagset = "http:https://ilk.uvt.nl/folia/sets/frog-mbma-nl";
string Mbma::pos_tagset = "http:https://ilk.uvt.nl/folia/sets/frog-mbpos-cgn";
string Mbma::clex_tagset = "http:https://ilk.uvt.nl/folia/sets/frog-mbpos-clex";
Mbma::~Mbma() {
/// the mbma destructor
delete MTree;
clearAnalysis();
delete filter;
if ( errLog != dbgLog ){
delete dbgLog;
}
delete errLog;
}
void Mbma::init_cgn( const string& main, const string& sub ) {
/// initalize the CGN-CLEX translation tables
/*!
\param main the name of the main-rules file
\param sub the name of the sub-rules file
the main file gives translations from CGN tags to CELEX tags, like:
\verbatim
N N
ADJ A
TW Q
WW V
\endverbatim
the sub file gives translations from CGN subtags to CELEX attributes, like:
\verbatim
comp C
conj
dat D
deeleigen
det
dial
dim d
\endverbatim
a single valued entry means that NO translation is available
*/
ifstream tc( main );
if ( tc ){
UnicodeString line;
while ( TiCC::getline( tc, line) ) {
vector<UnicodeString> tmp = TiCC::split_at( line, " " );
if ( tmp.size() < 2 ){
LOG << "splitting '" << line << "' failed" << endl;
throw ( runtime_error("panic") );
}
TAGconv.insert( make_pair( tmp[0], tmp[1] ) );
}
}
else {
throw ( runtime_error( "unable to open: " + main ) );
}
ifstream tc1( sub );
if ( tc1 ){
UnicodeString line;
while( TiCC::getline( tc1, line ) ) {
vector<UnicodeString> tmp = TiCC::split_at( line, " " );
if ( tmp.size() == 2 ){
TAGconv.insert( make_pair( tmp[0], tmp[1] ) );
}
}
}
else {
throw ( runtime_error( "unable to open:" + sub ) );
}
}
bool Mbma::init( const TiCC::Configuration& config ) {
/// initialize the Mbma analyzer using the config
/*!
\param config the Configuration to use
\return true when no problems are detected
*/
LOG << "Initiating morphological analyzer..." << endl;
debugFlag = 0;
string val = config.lookUp( "host", "mbma" );
if ( !val.empty() ){
// assume we must use a Timbl Server for the MBMA
_host = val;
val = config.lookUp( "port", "mbma" );
if ( val.empty() ){
LOG << "missing 'port' settings for host= " << _host << endl;
return false;
}
_port = val;
val = config.lookUp( "base", "mbma" );
if ( val.empty() ){
LOG << "missing 'base' settings for host= " << _host << endl;
return false;
}
_base = val;
}
else {
val = config.lookUp( "base", "mbma" ) + ":"
+ config.lookUp( "port", "mbma" );
if ( val.size() > 1 ){
LOG << "missing 'host' settings for base:port " << val << endl;
return false;
}
}
val = config.lookUp( "debug", "mbma" );
if ( val.empty() ){
val = config.lookUp( "debug" );
}
if ( !val.empty() ){
debugFlag = TiCC::stringTo<int>( val );
}
val = config.lookUp( "deep-morph", "mbma" );
if ( !val.empty() ){
doDeepMorph = TiCC::stringTo<bool>( val );
}
val = config.lookUp( "version", "mbma" );
if ( val.empty() ){
_version = "1.0";
}
else {
_version = val;
}
val = config.lookUp( "set", "mbma" );
if ( !val.empty() ){
mbma_tagset = val;
}
val = config.lookUp( "set", "tagger" );
if ( !val.empty() ){
pos_tagset = val;
}
val = config.lookUp( "clex_set", "mbma" );
if ( !val.empty() ){
clex_tagset = val;
}
string cgn_clex_main;
val = config.lookUp( "cgn_clex_main", "mbma" );
if ( val.empty() ){
cgn_clex_main = "cgntags.main";
}
else {
cgn_clex_main = val;
}
cgn_clex_main = prefix( config.configDir(), cgn_clex_main );
string cgn_clex_sub;
val = config.lookUp( "cgn_clex_sub", "mbma" );
if ( val.empty() ){
cgn_clex_sub = "cgntags.sub";
}
else {
cgn_clex_sub = val;
}
cgn_clex_sub = prefix( config.configDir(), cgn_clex_sub );
init_cgn( cgn_clex_main, cgn_clex_sub );
string charFile = config.lookUp( "char_filter_file", "mbma" );
if ( charFile.empty() ){
charFile = config.lookUp( "char_filter_file" );
}
if ( !charFile.empty() ){
charFile = prefix( config.configDir(), charFile );
filter = new TiCC::UniFilter();
filter->fill( charFile );
}
string dof = config.lookUp( "filter_diacritics", "mbma" );
if ( !dof.empty() ){
filter_diac = TiCC::stringTo<bool>( dof );
}
string cls = config.lookUp( "outputclass" );
if ( !cls.empty() ){
textclass = cls;
}
else {
textclass = "current";
}
if ( _host.empty() ){
// so classic monolytic run
string tfName = config.lookUp( "treeFile", "mbma" );
if ( tfName.empty() ){
tfName = "mbma.igtree"; // the default
}
MTreeFilename = prefix( config.configDir(), tfName );
//Read in (igtree) data
string opts = config.lookUp( "timblOpts", "mbma" );
if ( opts.empty() ){
opts = "-a1";
}
opts += " +vs -vf"; // make Timbl run quietly
MTree = new Timbl::TimblAPI(opts);
return MTree->GetInstanceBase(MTreeFilename);
}
else {
string mess = check_server( _host, _port, "MBMA" );
if ( !mess.empty() ){
LOG << "FAILED to find an MBMA server:" << endl;
LOG << mess << endl;
LOG << "timblserver not running??" << endl;
return false;
}
else {
LOG << "using MBMA Timbl on " << _host << ":" << _port << endl;
return true;
}
}
}
vector<UnicodeString> Mbma::make_instances( const UnicodeString& word ){
/// convert a Unicode string into a range of UTF8 instances for Timbl
/*!
\param word the UnicodeString representing 1 word to analyze
\return a vector of UTF8 stringa with instances for Timbl
*/
vector<UnicodeString> insts;
insts.reserve( word.length() );
for ( long i=0; i < word.length(); ++i ) {
if (debugFlag > 10){
DBG << "itt #:" << i << endl;
}
UnicodeString inst;
for ( long j=i ; j <= i + RIGHT + LEFT; ++j ) {
if (debugFlag > 10){
DBG << " " << j-LEFT << ": ";
}
if ( j < LEFT || j >= word.length()+LEFT ){
inst += '_';
}
else {
if (word[j-LEFT] == ',' ){
inst += 'C';
}
else {
inst += word[j-LEFT];
}
}
inst += ",";
if (debugFlag > 10){
DBG << " : " << inst << endl;
}
}
inst += "?";
insts.push_back( inst );
}
return insts;
}
UnicodeString find_class( unsigned int step,
const vector<UnicodeString>& classes,
unsigned int nranal ){
UnicodeString result = classes[0];
if ( nranal > 1 ){
if ( classes.size() > 1 ){
if ( classes.size() > step ){
result = classes[step];
}
else {
result = classes.back();
}
}
}
return result;
}
vector<vector<UnicodeString> > generate_all_perms( const vector<UnicodeString>& classes ){
/// determine all alternative analyses, remember the largest
/// and store every part in a vector of string vectors
/*!
\param classes A vector of possibilities generated by Timbl
example: Timbl has given 3 results for the word 'gek':
classes={"A|N|V","0","0/P|0/e|0/te1|0/te2I"} (from the word 'gek')
This is converted to
{ {"A","N","V","V"}, {"0","0","0","0" }, {"0/P","0/e","0/te1","0/te2I"} }
As we can see, the "0" from classes is expanded by duplication 4 times.
The "A|N|V" is expanded by duplicating the last "V".
an then for index 1 to 4 the results are collected:
{ {"A","0","0/P"}, {N","0", "0/e"}, {"V","0", "0/te1"}, {"V", "0","0/te2I"} }
*/
size_t largest_anal=1;
vector<vector<UnicodeString> > classParts;
classParts.reserve( classes.size() );
for ( const auto& uclass : classes ){
vector<UnicodeString> parts = TiCC::split_at( uclass, "|" );
size_t num = parts.size();
if ( num > 1 ){
classParts.push_back( parts );
if ( num > largest_anal ){
largest_anal = num;
}
}
else {
// only one, create a dummy
vector<UnicodeString> dummy;
dummy.push_back( uclass );
classParts.push_back( dummy );
}
}
//
// now expand the result
vector<vector<UnicodeString> > result;
result.reserve( largest_anal );
for ( size_t step=0; step < largest_anal; ++step ){
vector<UnicodeString> item;
for ( const auto& cp : classParts ){
item.push_back( find_class( step, cp, largest_anal ) );
}
result.push_back( item );
}
return result;
}
void Mbma::clearAnalysis(){
for ( const auto& a: analysis ){
delete a;
}
analysis.clear();
}
Rule* Mbma::matchRule( const std::vector<icu::UnicodeString>& ana,
const icu::UnicodeString& word,
bool keep_V2I ){
/// attempt to match an Analysis on a word
/*!
\param ana one analysis result, expanded from the Timbl classifier
\param word a Unicode Word to check
\param keep_V2I a boolean that determines if we want to keep Inversed
second person variants.
\return a matched Rule or 0
*/
Rule *rule = new Rule( ana, word, *errLog, *dbgLog, debugFlag );
if ( rule->performEdits() ){
rule->reduceZeroNodes();
if ( debugFlag > 1 ){
DBG << "after reduction: " << rule << endl;
}
rule->resolve_inflections();
if ( debugFlag > 1 ){
DBG << "after resolving: " << rule << endl;
}
rule->getCleanInflect( keep_V2I );
if ( debugFlag > 1 ){
DBG << "1 added Inflection: " << rule << endl;
}
rule->resolveBrackets();
return rule;
}
else {
if ( debugFlag > 1 ){
DBG << "rejected rule: " << rule << endl;
}
delete rule;
return 0;
}
}
bool check_next( const UnicodeString& tag ){
vector<UnicodeString> v = TiCC::split_at_first_of( tag, "()" );
if ( v.size() != 2
|| v[0] != "VNW" ){
return false;
}
else {
bool result = v[1].indexOf( ",2," ) == -1;
return result;
}
}
vector<Rule*> Mbma::execute( const icu::UnicodeString& word,
const icu::UnicodeString& next_tag,
const vector<icu::UnicodeString>& classes ){
/// attempt to find matching Rules
/*!
\param word a word to check
\param next_tag The tag of the word following this word (when available)
\param classes the Timbl classifications
\return 0 or more matching Rules
*/
vector<vector<UnicodeString> > allParts = generate_all_perms( classes );
if ( debugFlag > 1 ){
UnicodeString out = "alternatives: word="
+ word + ", classes=<" + TiCC::join( classes, "," ) + ">";
DBG << out << endl;
DBG << "allParts : " << allParts << endl;
}
bool both_V2_and_V2I = false;
for ( const auto& cls : classes ){
if ( cls.indexOf( "te2|" ) != -1
&& cls.indexOf( "te2I" ) != -1 ){
both_V2_and_V2I = true;
break;
}
}
if ( debugFlag > 1 ){
if ( both_V2_and_V2I ){
DBG << "found a special V2VI one: " << word << " in : "
<< TiCC::join( classes, "," ) << endl;
}
}
bool keep_V2I = false;
if ( !both_V2_and_V2I ){
keep_V2I = check_next( next_tag );
}
vector<Rule*> accepted;
size_t id = 0;
// now loop over all the analysis
for ( auto const& ana : allParts ){
Rule *rule = matchRule( ana, word, keep_V2I );
if ( rule ){
rule->ID = id++;
accepted.push_back( rule );
}
}
return accepted;
}
void Mbma::addBracketMorph( folia::Word *word,
const UnicodeString& orig_word,
const BaseBracket *brackets ) const {
/// add a (deep) Morpheme layer to the FoLiA Word
/*!
\param word the FoLiA Word
\param orig_word, a Unicode string with the oriiginal word
\param brackets the nested structure describing the morphemes
*/
if (debugFlag > 1){
DBG << "addBracketMorph(" << word << "," << orig_word << ","
<< brackets << ")" << endl;
}
folia::KWargs args;
args["set"] = mbma_tagset;
folia::MorphologyLayer *ml;
#pragma omp critical (foliaupdate)
{
try {
ml = word->addMorphologyLayer( args );
}
catch( const exception& e ){
LOG << e.what() << " addBracketMorph failed." << endl;
throw;
}
}
folia::Morpheme *m = 0;
try {
m = brackets->createMorpheme( word->doc(), textclass );
}
catch( const exception& e ){
LOG << "createMorpheme failed: " << e.what() << endl;
throw;
}
#pragma omp critical (foliaupdate)
{
m->setutext( orig_word, textclass );
ml->append( m );
}
}
bool mbmacmp( const Rule *m1, const Rule *m2 ){
/// sorting function for Rule's
return m1->getKey().length() > m2->getKey().length();
}
struct id_cmp {
bool operator()( const Rule *m1, const Rule *m2 ) const {
/// sorting function for Rule's on ID
return m1->ID < m2->ID;
}
};
void Mbma::filterHeadTag( const icu::UnicodeString& head ){
/// reduce the Mbms analysis by removing all solutions where the head is not
/// matched
/*!
\param head the head-tag that is required
matching does not mean equality. We are a forgivingful in the sense that
\verbatim
N matches PN
A matches B and vv
A matches V
\endverbatim
*/
// first we select only the matching heads
if (debugFlag > 1){
DBG << "filter with head: " << head << endl;
DBG << "filter: analysis is:" << endl;
int i=0;
for ( const auto *it : analysis ){
DBG << ++i << " - " << it << endl;
}
}
auto const tagIt = TAGconv.find( head );
if ( tagIt == TAGconv.end() ) {
// this should never happen
throw folia::ValueError( "1 unknown head feature '"
+ TiCC::UnicodeToUTF8(head) + "'" );
}
UnicodeString celex_tag = tagIt->second;
if (debugFlag > 1){
DBG << "#matches: CGN:" << head << " CELEX " << celex_tag << endl;
}
auto ait = analysis.begin();
while ( ait != analysis.end() ){
UnicodeString mbma_tag = CLEX::toUnicodeString((*ait)->tag);
if ( celex_tag == mbma_tag ){
if (debugFlag > 1){
DBG << "comparing " << celex_tag << " with "
<< mbma_tag << " (OK)" << endl;
}
(*ait)->confidence = 1.0;
++ait;
}
else if ( celex_tag == "N" && mbma_tag == "PN" ){
if (debugFlag > 1){
DBG << "comparing " << celex_tag << " with "
<< mbma_tag << " (OK)" << endl;
}
(*ait)->confidence = 1.0;
++ait;
}
else if ( ( celex_tag == "B" && mbma_tag == "A" )
|| ( celex_tag == "A" && mbma_tag == "B" ) ){
if (debugFlag > 1){
DBG << "comparing " << celex_tag << " with "
<< mbma_tag << " (OK)" << endl;
}
(*ait)->confidence = 0.8;
++ait;
}
else if ( celex_tag == "A" && mbma_tag == "V" ){
if (debugFlag > 1){
DBG << "comparing " << celex_tag << " with "
<< mbma_tag << " (OK)" << endl;
}
(*ait)->confidence = 0.5;
++ait;
}
else {
if (debugFlag > 1){
DBG << "comparing " << celex_tag << " with "
<< mbma_tag << " (rejected)" << endl;
}
delete *ait;
ait = analysis.erase( ait );
}
}
if (debugFlag > 1){
DBG << "filter: analysis after head filter:" << endl;
int i=0;
for ( const auto *it : analysis ){
DBG << ++i << " - " << it << endl;
}
}
}
void Mbma::filterSubTags( const vector<icu::UnicodeString>& feats ){
/// reduce the analyses set based on sub-features
/*!
\param feats a list of subfeatures
when a candidate Rule has inflexion it should match a feature.
Other criteria: only take the highest confidence, and remove Rules that
yield the same analysis.
Example:
\verbatim
The word 'appel' is according to Mbma an N with inflection e ('enkelvoud')
The tagger will assign an N too, with features [soort,ev,basis,zijd,stan]
one of the features, 'ev' will match the 'e' inflection after translation
so this is a good reading
\endverbatim
*/
if ( analysis.size() < 1 ){
if (debugFlag > 1){
DBG << "analysis is empty so skip next filter" << endl;
}
return;
}
// ok there are several analysis left.
// try to select on the features:
//
// find best match
// loop through all subfeatures of the tag
// and match with inflections from each m
set<Rule *, id_cmp> bestMatches; // store rules on ID, maybe overkill?
int max_count = 0;
for ( const auto& q : analysis ){
int match_count = 0;
UnicodeString inflection = q->inflection;
if ( inflection.isEmpty() ){
bestMatches.insert(q);
continue;
}
if (debugFlag>1){
DBG << "matching " << inflection << " with " << feats << endl;
}
for ( const auto& feat : feats ){
auto const conv_tag_p = TAGconv.find( feat );
if (conv_tag_p != TAGconv.end()) {
UnicodeString c = conv_tag_p->second;
if (debugFlag > 1){
DBG << "found " << feat << " ==> " << c << endl;
}
if ( inflection.indexOf( c ) != -1 ){
if (debugFlag >1){
DBG << "it is in the inflection " << endl;
}
match_count++;
}
}
}
if (debugFlag > 1 ){
DBG << "score: " << match_count << " max was " << max_count << endl;
}
if (match_count >= max_count) {
if (match_count > max_count) {
max_count = match_count;
bestMatches.clear();
}
bestMatches.insert(q);
}
}
//
// now filter on confidence:
//
if ( debugFlag > 1){
DBG << "filter: best matches before sort on confidence:" << endl;
int i=0;
for ( const auto *it : bestMatches ){
DBG << ++i << " - " << it << endl;
}
DBG << "" << endl;
}
double best_conf = -0.1;
set<const Rule*, id_cmp> highConf; // store Rule's on ID to be reproducable
for ( const auto *it : bestMatches ){
if ( it->confidence >= best_conf ){
if ( it->confidence > best_conf ){
best_conf = it->confidence;
highConf.clear();
}
highConf.insert( it );
}
}
// now we can remove all analysis that aren't in the set.
auto ana = analysis.begin();
while ( ana != analysis.end() ){
if ( highConf.find( *ana ) == highConf.end() ){
delete *ana;
ana = analysis.erase( ana );
}
else {
++ana;
}
}
//
// we still might have doubles. (different Rule's yielding the same result)
// reduce these
//
if ( debugFlag > 1){
DBG << "filter: analysis before sort key:" << endl;
int i=0;
for ( const auto *a_it : analysis ){
DBG << ++i << " - " << a_it << " " << a_it->getKey()
<< " (" << a_it->getKey().length() << ")" << endl;
}
DBG << endl;
}
map<icu::UnicodeString, const Rule*> unique;
// create a map of "key's" to Rule. The key is the string representation
// of the Rule's flattened result, like [ver][zeker][ing][s][ageer][ent]
// (with inflection info for Deep Morph analysis)
for ( const auto& ait : highConf ){
icu::UnicodeString tmp = ait->getKey( doDeepMorph );
unique.emplace(tmp,ait);
}
// so now we have map of 'equal' analysis.
// create a set for reverse lookup, using the Rule.ID to distinguish Rule's
set<const Rule*, id_cmp> uniqueAna;
for ( auto const& uit : unique ){
uniqueAna.insert( uit.second );
}
// now we can remove all analysis that aren't in that set.
ana = analysis.begin();
while ( ana != analysis.end() ){
if ( uniqueAna.find( *ana ) == uniqueAna.end() ){
delete *ana;
ana = analysis.erase( ana );
}
else {
++ana;
}
}
if ( debugFlag > 1){
DBG << "filter: analysis before sort on length:" << endl;
int i=0;
for ( const auto *a_it : analysis ){
DBG << ++i << " - " << a_it << " " << a_it->getKey()
<< " (" << a_it->getKey().length() << ")" << endl;
}
DBG << "" << endl;
}
// Now we have a small list of unique and different analysis.
// We assume the 'longest' analysis to be the best.
// So we prefer '[ge][maak][t]' over '[gemaak][t]'
// Therefor we sort on (unicode) string length
sort( analysis.begin(), analysis.end(), mbmacmp );
if ( debugFlag > 1){
DBG << "filter: definitive analysis:" << endl;
int i=0;
for ( auto const *a_it : analysis ){
DBG << ++i << " - " << a_it << endl;
}
DBG << "done filtering" << endl;
}
return;
}
void Mbma::assign_compounds(){
/// add compound information to the result
for ( auto const& sit : analysis ){
sit->compound = sit->brackets->speculateCompoundType();
}
}
void Mbma::add_provenance( folia::Document& doc,
folia::processor *main ) const {
/// add provenance information to the FoLiA document
/*!
\param doc the foLiA document we are working on
\param main the main processor (presumably Frog) we want to add a new one to
*/
string _label = "mbma";
if ( !main ){
throw logic_error( "mbma::add_provenance() without arguments." );
}
folia::KWargs args;
args["name"] = _label;
args["generate_id"] = "auto()";
args["version"] = _version;
args["begindatetime"] = "now()";
folia::processor *proc = doc.add_processor( args, main );
args.clear();
args["processor"] = proc->id();
doc.declare( folia::AnnotationType::MORPHOLOGICAL, mbma_tagset, args );
doc.declare( folia::AnnotationType::POS, clex_tagset, args );
}
void Mbma::store_morphemes( frog_record& fd,
const vector<UnicodeString>& morphemes ) const {
/// store the calculated morphemes in the FrogData
if ( fd.morph_string.isEmpty() ){
// not yet filled
UnicodeString out;
for ( const auto& m : morphemes ){
out += "[" + m + "]";
}
#pragma omp critical (dataupdate)
{
fd.morph_string = out;
}
}
}
void Mbma::store_brackets( frog_record& fd,
const UnicodeString& wrd,
const UnicodeString& head,
bool unanalysed ) const {
if (debugFlag > 1){
DBG << "store_brackets(" << wrd << "," << head << ")" << endl;
}
if ( unanalysed ) {
// unanalysed, so trust the TAGGER
if (debugFlag > 1){
DBG << "head was X, tagger gives :" << head << endl;
}
const auto tagIt = TAGconv.find( head );
if ( tagIt == TAGconv.end() ) {
// this should never happen
throw logic_error( "2 unknown head feature '"
+ TiCC::UnicodeToUTF8( head ) + "'" );
}
CLEX::Type clex_tag = CLEX::toCLEX( tagIt->second );
if (debugFlag > 1){
DBG << "replaced X by: " << head << endl;
}
BaseBracket *leaf = new BracketLeaf( clex_tag,
wrd,
debugFlag,
*dbgLog );
if ( fd.morph_string.isEmpty() ){
fd.morph_string = "[" + wrd + "]";
if ( doDeepMorph ){
UnicodeString head_desc = CLEX::get_tag_descr(clex_tag);
fd.morph_string += head_desc;
}
}
fd.morph_structure.push_back( leaf );
}
else if ( head == "LET" || head == "SPEC" ){
BaseBracket *leaf = new BracketLeaf( CLEX::toCLEX(head),
wrd,
debugFlag,
*dbgLog );
leaf->set_status( STEM );
if ( fd.morph_string.isEmpty() ){
fd.morph_string = "[" + wrd + "]";
}
fd.morph_structure.push_back( leaf );
}
else {
DBG << "case 3 " << endl;
BaseBracket *leaf = new BracketLeaf( CLEX::toCLEX(head),
wrd,
debugFlag,
*dbgLog );
leaf->set_status( STEM );
if ( fd.morph_string.isEmpty() ){
fd.morph_string = "[" + wrd + "]";
}
if ( doDeepMorph ){
fd.morph_string += head;
}
fd.morph_structure.push_back( leaf );
}
return;
}
void Mbma::store_brackets( frog_record& fd,
const UnicodeString& orig_word,
const BracketNest *brackets ) const {
if (debugFlag > 1){
DBG << "store_brackets(" << fd.word << "," << orig_word
<< "," << brackets << ")" << endl;
}
#pragma omp critical (dataupdate)
{
fd.morph_structure.push_back( brackets );
}
return;
}
UnicodeString flatten( const UnicodeString& in ){
/// helper function to 'flatten out' bracketed morpheme strings
/*!
\param in a bracketed string of morphemes
\return a string with multiple '[' and ']' reduced to single occurrences
*/
string s = TiCC::UnicodeToUTF8( in );
string::size_type bpos = s.find_first_not_of( " [" );
// deb << " FLATTEN: '" << s << "'" << endl;
string result;
if ( bpos != string::npos ){
string::size_type epos = s.find_first_of( "]", bpos );
result += "[" + s.substr( bpos, epos-bpos ) + "]";
// deb << "substring: '" << s.substr( bpos, epos-bpos ) << "'" << endl;
bpos = s.find_first_of( "[", epos+1 );
bpos = s.find_first_not_of( " [", bpos );
while ( bpos != string::npos ){
epos = s.find_first_of( "]", bpos );
if ( epos == string::npos ){
break;
}
result += "[" + s.substr( bpos, epos-bpos ) + "]";
// deb << "substring: '" << s.substr( bpos, epos-bpos ) << "'" << endl;
bpos = s.find_first_of( "[", epos+1 );
bpos = s.find_first_not_of( " [", bpos );
}
}
else {
result = s;
}
// deb << "FLATTENED: '" << result << "'" << endl;
return TiCC::UnicodeFromUTF8(result);
}
void Mbma::storeResult( frog_record& fd,
const UnicodeString& uword,
const UnicodeString& uhead ) const {
if ( analysis.size() == 0 ){
// fallback option: use the word and pretend it's a morpheme ;-)
if ( debugFlag > 1){
DBG << "no matches found, use the word instead: "
<< uword << endl;
}
store_brackets( fd, uword, uhead, true );
vector<UnicodeString> tmp;
tmp.push_back( uword );
store_morphemes( fd, tmp );
}
else {
vector<pair<UnicodeString,string>> pv = getResults();
if ( doDeepMorph ){
fd.morph_string = pv[0].first;
}
else {
fd.morph_string = flatten( pv[0].first );
}
if ( pv[0].second == "none" ){
fd.compound_string = "0";
}
else {
fd.compound_string = pv[0].second;
}
for ( auto& sit : analysis ){
store_brackets( fd, uword, sit->brackets );