-
Notifications
You must be signed in to change notification settings - Fork 11
/
FrogAPI.cxx
3252 lines (3141 loc) · 92.3 KB
/
FrogAPI.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 <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/FrogAPI.h"
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
#include <pwd.h>
#include <signal.h>
#include <algorithm>
#include <string>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <vector>
#include "unicode/schriter.h"
#include "config.h"
#ifdef HAVE_OPENMP
#include <omp.h>
#endif
#ifdef HAVE_LIBREADLINE
# if defined(HAVE_READLINE_READLINE_H)
# include <readline/readline.h>
# elif defined(HAVE_READLINE_H)
# include <readline.h>
# else
# define NO_READLINE
# endif /* !defined(HAVE_READLINE_H) */
#else
# define NO_READLINE
#endif /* HAVE_LIBREADLINE */
#ifdef HAVE_READLINE_HISTORY
# if defined(HAVE_READLINE_HISTORY_H)
# include <readline/history.h>
# elif defined(HAVE_HISTORY_H)
# include <history.h>
# endif /* defined(HAVE_READLINE_HISTORY_H) */
#endif /* HAVE_READLINE_HISTORY */
#include "ticcutils/PrettyPrint.h"
#include "ticcutils/FileUtils.h"
// individual module headers
#include "frog/Frog-util.h"
#include "frog/ucto_tokenizer_mod.h"
#include "frog/mblem_mod.h"
#include "frog/mbma_mod.h"
#include "frog/mwu_chunker_mod.h"
#include "frog/cgn_tagger_mod.h"
#include "frog/iob_tagger_mod.h"
#include "frog/ner_tagger_mod.h"
#include "frog/Parser.h"
#include "frog/AlpinoParser.h"
#include "ticcutils/json.hpp"
using namespace std;
using namespace icu;
using namespace nlohmann;
using namespace Tagger;
using TiCC::operator<<;
#define LOG *TiCC::Log(theErrLog)
#define DBG *TiCC::Log(theDbgLog)
/// the FoLiA setname for languages
const string ISO_SET = "https://raw.github.com/proycon/folia/master/setdefinitions/iso639_3.foliaset";
const char *homedir = getenv("HOME") ? getenv("HOME") : getpwuid(getuid())->pw_dir; //never NULL
const char *xdgconfighome = getenv("XDG_CONFIG_HOME"); //may be NULL
string localConfigDir = ((xdgconfighome != NULL) ? string(xdgconfighome) : string(homedir) + "/.config") + "/" + PACKAGE + "/";
string configDir = string(SYSCONF_PATH) + "/" + PACKAGE + "/";
string FrogAPI::defaultConfigDir( const string& language ){
/// return the default location of the configuration files
/*!
\param language use this to find the configuration for this language
\return a string representing the (language specific) path
*/
//! The global location of the configuration files is determined at installation
//! of the package. Users can place configurations in $XDG_CONFIG_HOME/frog (i.e. ~/.config/frog)
//! which will take precendence over the global configuration.
//! All languages are supposed to be in sub-directories
if ( language.empty() ){
if (TiCC::isDir(localConfigDir)) {
return localConfigDir;
}
else {
return configDir;
}
}
else {
if (TiCC::isDir(localConfigDir + language)) {
return localConfigDir + language +"/";
}
else {
return configDir+language+"/";
}
}
}
string FrogAPI::defaultConfigFile( const string& language ){
/// return the filename for the default configuration
/*!
\param language use this to find the configuration for this language
\return a string representing (language specific) default filename
*/
//! The location of the configuration files is determined at installation
//! of the package. All languages are supposed to be in sub-directories
return defaultConfigDir( language ) + "frog.cfg";
}
FrogOptions::FrogOptions():
doTok(true),
doLemma(true),
doMbma(true),
doDeepMorph(false),
doCompounds(false),
doMwu(true),
doIOB(true),
doNER(true),
doParse(true),
doTagger(true),
doSentencePerLine(false),
doQuoteDetection(false),
doRetry(false),
noStdOut(false),
doXMLin(false),
doXMLout(false),
wantOUT(false),
doJSONin(false),
doJSONout(false),
doServer(false),
doKanon(false),
test_API(false),
hide_timers(false),
interactive(false),
doAlpinoServer(false),
doAlpino(false),
do_und_language(false),
do_language_detection(false),
numThreads(1),
debugFlag(0),
JSON_pp(0),
uttmark("<utt>"),
listenport("void"),
docid("untitled"),
inputclass("current"),
outputclass("current"),
textredundancy("minimal"),
correct_words(false),
maxParserTokens(500) // 500 words in a sentence is already insane
// needs about 16 Gb memory to parse!
// set tot 0 for unlimited
{
#ifdef HAVE_OPENMP
numThreads = min<int>( 8, omp_get_max_threads() ); // ok, don't overdo
#endif
}
void FrogAPI::test_version( const TiCC::Configuration& configuration,
const string& module,
double minimum ){
/// check if a module in the Frog Configuration is at least at the
/// requested version level
/*!
\param module whick module to check
\param minumum the minimum level
This function will throw on a mismatch
*/
string version = configuration.lookUp( "version", module );
double v = 0.0;
if ( !version.empty() ){
if ( !TiCC::stringTo( version, v ) ){
v = 0.5;
}
}
if ( module == "IOB" ){
if ( v < minimum ){
LOG << "[[" << module << "]] Wrong FrogData!. "
<< "Expected version " << minimum << " or higher for module: "
<< module << endl;
if ( version.empty() ) {
LOG << "but no version info was found!." << endl;
}
else {
LOG << "but found version " << v << endl;
}
throw runtime_error( "Frog initialization failed" );
}
}
else if ( module == "NER" ){
if ( v < minimum ){
LOG << "[[" << module << "]] Wrong FrogData!. "
<< "Expected version " << minimum << " or higher for module: "
<< module << endl;
if ( version.empty() ) {
LOG << "but no version info was found!." << endl;
}
else {
LOG << "but found version " << v << endl;
}
throw runtime_error( "Frog initialization failed" );
}
}
else {
throw logic_error( "unknown module:" + module );
}
}
bool FrogAPI::parse_language_option( const string& languages ){
if ( languages.empty() ){
return true;
}
LOG << "check: " << languages << endl;
vector<string> lang_v = TiCC::split_at( languages, "," );
if ( lang_v.empty() ){
LOG << "invalid value in --language=" << languages
<< " option. " << endl;
return false;
}
set<string> ucto_languages = Tokenizer::Setting::installed_languages();
set<string> rejected;
auto l = lang_v.begin();
while ( l != lang_v.end() ){
if ( *l == "und" ){
options.do_und_language = true;
l = lang_v.erase(l);
}
else if ( ucto_languages.find( *l ) == ucto_languages.end() ){
rejected.insert( *l );
++l;
}
else {
++l;
}
}
if ( !rejected.empty() ){
LOG << "unsupported languages: " << rejected << endl;
LOG << "available languages: " << ucto_languages << endl;
return false;
}
options.languages = lang_v;
options.default_language = options.languages[0]; // the first mentioned is the default.
return true;
}
bool FrogAPI::collect_options( TiCC::CL_Options& Opts,
TiCC::Configuration& configuration,
TiCC::LogStream* theErrLog ){
/// process the command line and fill FrogOptions to initialize the API
/*!
\param Opts The command line options we have received
\param options The FrogOptions structure we will fill
\param theErrLog the stream to send error messages to
return true on succes
*/
options.command = Opts.toString();
// get the (optional) configuration file
string configFileName;
if ( !Opts.extract( 'c', configFileName ) ){
Opts.extract( "config", configFileName );
}
// is a language-list specified? Default is dutch
string languages;
Opts.extract ("language", languages );
if ( configFileName.empty() ){
if ( languages.empty() ){
// ok no config or languages parameter.
// use a (default) configfile. Dutch
configFileName = FrogAPI::defaultConfigFile("nld");
options.default_language = "nld";
options.languages.push_back( "nld" );
}
else {
// no config, but languages
if ( !parse_language_option( languages ) ){
return false;
}
if ( options.languages.size() > 1 ){
cerr << "WARNING: you used the --language=" << languages << " option"
<< " with more then one language " << endl
<< "\t specified. These values will be handled to the tokenizer,"
<< " but Frog"<< endl
<< "\t will only handle the first language: '"
<< options.default_language
<< "' for further processing!" << endl;
options.do_language_detection = true;
}
configFileName = FrogAPI::defaultConfigFile(options.default_language);
if ( !TiCC::isFile( configFileName ) ){
cerr << "configuration file: " << configFileName << " not found" << endl;
cerr << "Did you correctly install the frogdata package for language="
<< options.default_language << "?" << endl;
return false;
}
}
}
LOG << "requested configuration: " << configFileName << endl;
if ( !TiCC::isFile( configFileName ) && !(configFileName.rfind("/",0) == 0)){
// if only a suffix was specified, look in the local resp. global defaults dirs:
string localConfigFileName = localConfigDir + configFileName;
if (TiCC::isFile( localConfigFileName )) {
configFileName = localConfigFileName;
}
else {
LOG << " not found locally (" << configFileName << ")" << endl;
//global (final fallback)
configFileName = configDir + configFileName;
if (!TiCC::isFile( configFileName )) {
LOG << " not found globally (" << configFileName << ")" << endl;
}
}
}
if ( configuration.fill( configFileName ) ){
LOG << "config read from: " << configFileName << endl;
string vers = configuration.lookUp( "version" );
if ( !vers.empty() ){
LOG << "configuration version = " << vers << endl;
}
string tmp = configuration.lookUp( "languages", "tokenizer" );
// We will override the language options from the commandline
// with those from the config file (if available)
if ( tmp.empty() ){
// not in the config, so use the commandline then
tmp = languages;
}
if ( !parse_language_option( tmp ) ){
return false;
}
}
else {
cerr << "failed to read configuration from '" << configFileName << "' !!" << endl;
cerr << "Did you correctly install the frogdata package for language="
<< options.default_language << "?" << endl;
return false;
}
string conf_languages = TiCC::join( options.languages, "," );
if ( options.do_und_language ){
conf_languages += ",und";
}
if ( !conf_languages.empty() ){
LOG << "SET config " << conf_languages << endl;
configuration.setatt( "languages", conf_languages, "tokenizer" );
}
string opt_val;
// debug opts
if ( Opts.extract ('d', opt_val) ) {
if ( !TiCC::stringTo<int>( opt_val, options.debugFlag ) ){
LOG << "-d value should be an integer" << endl;
return false;
}
configuration.setatt( "debug", opt_val );
}
else {
configuration.setatt( "debug", "0" );
}
bool old_mwu = Opts.extract("OLDMWU");
if ( Opts.extract( "debug", opt_val ) ) {
vector<string> vec = TiCC::split_at( opt_val, "," );
for ( const auto& val : vec ){
char mod = val[0];
string value = val.substr(1);
int dbval = 0;
if ( !TiCC::stringTo<int>( value, dbval ) ){
cerr << "expected integer value for --debug=" << mod << value << endl;
return false;
}
switch ( mod ){
case 'T':
configuration.setatt( "debug", value, "tagger" );
break;
case 't':
configuration.setatt( "debug", value, "tokenizer" );
break;
case 'l':
configuration.setatt( "debug", value, "mblem" );
break;
case 'a':
configuration.setatt( "debug", value, "mbma" );
break;
case 'm':
configuration.setatt( "debug", value, "mwu" );
break;
case 'c':
configuration.setatt( "debug", value, "IOB" );
break;
case 'n':
configuration.setatt( "debug", value, "NER" );
break;
case 'p':
configuration.setatt( "debug", value, "parser" );
break;
default:
cerr << "unknown module code:'" << mod << "'" << endl;
return false;
}
}
}
string redundancy;
Opts.extract( 'T', redundancy );
Opts.extract( "textredundancy", redundancy );
if ( !redundancy.empty() ){
if ( redundancy != "full"
&& redundancy != "minimal"
&& redundancy != "none" ){
LOG << "unknown textredundancy level: " << redundancy << endl;
return false;
}
options.textredundancy = redundancy;
}
options.doSentencePerLine = Opts.extract( 'n' );
options.correct_words = Opts.extract( "allow-word-corrections" );
options.doQuoteDetection = Opts.extract( 'Q' );
if ( options.doQuoteDetection ){
LOG << "Quote detection is NOT supported!" << endl;
return false;
}
if ( Opts.extract( "skip", opt_val )) {
string skip = opt_val;
if ( skip.find_first_of("tT") != string::npos ){
options.doTok = false;
}
if ( skip.find_first_of("lL") != string::npos ){
options.doLemma = false;
}
if ( skip.find_first_of("aA") != string::npos ){
options.doMbma = false;
}
if ( skip.find_first_of("mM") != string::npos ){
if ( options.doAlpino ){
LOG << "option skip=m conflicts with --alpino"
<< endl;
return false;
}
options.doMwu = false;
}
if ( skip.find_first_of("cC") != string::npos ){
options.doIOB = false;
}
if ( skip.find_first_of("nN") != string::npos ){
options.doNER = false;
}
if ( skip.find_first_of("gG") != string::npos ){
options.doTagger = false;
}
if ( skip.find_first_of("pP") != string::npos ){
if ( options.doAlpino ){
LOG << "option skip=p conflicts with --alpino"
<< endl;
return false;
}
else if ( options.doMwu && !old_mwu ){
LOG << " MWU disabled, because the Parser is deselected" << endl;
options.doMwu = false;
}
options.doParse = false;
}
else if ( !options.doMwu && options.doParse ){
LOG << " Parser disabled, because MWU is deselected" << endl;
options.doParse = false;
}
Opts.remove("skip");
};
if ( Opts.extract( "deep-morph" ) ) {
if ( !options.doMbma ){
LOG << " Option --deep-morph NOT possible, because of --skip=a" << endl;
return false;
}
options.doDeepMorph = true;
}
if ( Opts.extract( "compounds" ) ){
if ( !options.doMbma ){
LOG << " Option --compounds NOT possible, because of --skip=a" << endl;
return false;
}
options.doCompounds = true;
}
options.doRetry = Opts.extract( "retry" );
options.noStdOut = Opts.extract( "nostdout" );
Opts.extract( 'e', options.encoding );
if ( Opts.extract( "max-parser-tokens", opt_val ) ){
if ( !TiCC::stringTo<unsigned int>( opt_val, options.maxParserTokens ) ){
LOG << "max-parser-tokens value should be an integer" << endl;
return false;
}
}
if ( Opts.extract( "ner-override", opt_val ) ){
configuration.setatt( "ner_override", opt_val, "NER" );
}
options.doServer = Opts.extract('S', options.listenport );
options.doJSONin = Opts.extract( "JSONin" );
if ( options.doJSONin && !options.doServer ){
LOG << "option JSONin is only allowed for server mode. (-S option)" << endl;
return false;
}
options.doAlpino = Opts.extract("alpino", opt_val);
if ( options.doAlpino ){
options.doParse = false;
options.doMwu = false;
if ( !opt_val.empty() ){
if ( opt_val == "server" ){
options.doAlpinoServer = true;
configuration.setatt( "alpinoserver", "true", "parser" );
}
else {
LOG << "unsupported value for --alpino: " << opt_val << endl;
return false;
}
}
}
#ifdef HAVE_OPENMP
if ( options.doServer ) {
// run in one thread in server mode, forking is too expensive for lots of small snippets
options.numThreads = 1;
Opts.extract( "threads", opt_val ); //discard threads option
}
else if ( Opts.extract( "threads", opt_val ) ){
int num;
if ( !TiCC::stringTo<int>( opt_val, num ) || num < 1 ){
LOG << "threads value should be a positive integer" << endl;
return false;
}
options.numThreads = num;
}
#else
if ( Opts.extract( "threads", opt_val ) ){
LOG << "WARNING!\n---> There is NO OpenMP support enabled\n"
<< "---> --threads=" << opt_val << " is ignored.\n"
<< "---> Will continue on just 1 thread." << endl;
}
#endif
if ( Opts.extract( "keep-parser-files" ) ){
LOG << "keep-parser-files option not longer supported. (ignored)" << endl;
}
if ( Opts.extract( "tmpdir" ) ){
LOG << "tmpdir option not longer supported. (ignored)" << endl;
}
string TestFileName;
if ( Opts.extract( "testdir", TestFileName ) ) {
if ( options.doServer ){
LOG << "a testdir is not possible when running a Server" << endl;
return false;
}
options.testDirName = TestFileName;
if ( options.testDirName.back() != '/' ){
options.testDirName += "/";
}
if ( !TiCC::isDir( options.testDirName ) ){
LOG << "input dir " << options.testDirName << " not readable" << endl;
return false;
}
}
else if ( Opts.extract( 't', TestFileName ) ) {
if ( options.doServer ){
LOG << "an input file is not possible when running a Server" << endl;
return false;
}
if ( !TiCC::isFile( TestFileName ) ){
LOG << "input stream " << TestFileName << " is not readable" << endl;
return false;
}
};
options.wantOUT = false;
if ( Opts.extract( "outputdir", options.outputDirName )) {
if ( options.doServer ){
LOG << "an outputdir is not possible when running a Server" << endl;
return false;
}
if ( options.outputDirName.back() != '/' ){
options.outputDirName += "/";
}
if ( !TiCC::createPath( options.outputDirName ) ){
LOG << "output dir " << options.outputDirName << " not readable" << endl;
return false;
}
options.wantOUT = true;
}
else if ( Opts.extract( 'o', options.outputFileName ) ){
options.wantOUT = true;
};
string json_pps;
options.doJSONout = Opts.extract( "JSONout", json_pps );
if ( options.doJSONout ){
if ( json_pps.empty() ){
options.JSON_pp = 0;
}
else {
int tmp;
if ( !TiCC::stringTo<int>( json_pps, tmp )
|| tmp < 0 ){
LOG << "--JSONout value should be an integer >=0 " << endl;
return false;
}
else {
options.JSON_pp = tmp;
}
}
}
else {
if ( options.doJSONin ){
options.doJSONout = true;
}
}
options.doXMLout = false;
Opts.extract( "id", options.docid );
if ( !options.docid.empty() ){
if ( !folia::isNCName(options.docid) ){
LOG << "Invalid value for 'id': " << options.docid << " (not a valid NCName)" << endl;
return false;
}
}
if ( Opts.extract( "xmldir", options.xmlDirName ) ){
if ( options.xmlDirName.back() != '/' ){
options.xmlDirName += "/";
}
if ( !TiCC::createPath( options.xmlDirName ) ){
LOG << "XML output dir " << options.xmlDirName << " not readable" << endl;
return false;
}
options.doXMLout = true;
}
else if ( Opts.extract('X',options.XMLoutFileName ) ){
options.doXMLout = true;
}
options.doKanon = Opts.extract("KANON");
options.test_API = Opts.extract("TESTAPI");
options.doXMLin = false;
if ( Opts.extract ('x', opt_val ) ){
options.doXMLin = true;
if ( !opt_val.empty() ){
if ( !options.xmlDirName.empty() || !options.testDirName.empty() ){
LOG << "-x may not provide a value when --testdir or --xmldir is provided" << endl;
return false;
}
TestFileName = opt_val;
if ( !TiCC::isFile( TestFileName ) ){
LOG << "input stream " << opt_val << " is not readable" << endl;
return false;
}
}
}
string textclass;
string inputclass;
string outputclass;
Opts.extract( "textclass", textclass );
Opts.extract( "inputclass", inputclass );
Opts.extract( "outputclass", outputclass );
if ( !textclass.empty() ){
if ( !inputclass.empty() || !outputclass.empty() ){
LOG << "when --textclass is specified, --inputclass or --outputclass may NOT be present." << endl;
return false;
}
options.inputclass = textclass;
options.outputclass = textclass;
configuration.setatt( "inputclass", textclass );
configuration.setatt( "outputclass", textclass );
}
else {
if ( !inputclass.empty() ){
options.inputclass = inputclass;
configuration.setatt( "inputclass", inputclass );
if ( outputclass.empty() ){
options.outputclass = inputclass;
configuration.setatt( "outputclass", inputclass );
}
}
if ( !outputclass.empty() ){
options.outputclass = outputclass;
configuration.setatt( "outputclass", outputclass );
}
}
if ( !options.XMLoutFileName.empty() && !options.testDirName.empty() ){
LOG << "useless -X value" << endl;
return false;
}
Opts.extract ("uttmarker", options.uttmark );
if ( !options.testDirName.empty() ){
if ( options.doXMLin ){
options.fileNames = getFileNames( options.testDirName, ".xml" );
}
else {
options.fileNames = getFileNames( options.testDirName, "" );
}
if ( options.fileNames.empty() ){
LOG << "error: couldn't find any files in directory: "
<< options.testDirName << endl;
return false;
}
}
if ( options.fileNames.empty() ){
if ( !TestFileName.empty() ){
if ( !TiCC::isFile( TestFileName ) ){
LOG << "Input file not found: " << TestFileName << endl;
return false;
}
options.fileNames.insert( TestFileName );
}
else {
vector<string> mass = Opts.getMassOpts();
for ( const auto& name : mass ){
if ( !TiCC::isFile( name ) ){
LOG << "Input file not found: " << name << endl;
return false;
}
options.fileNames.insert( name );
}
}
}
if ( options.fileNames.size() > 1 ){
if ( options.doServer ){
LOG << "specifying inputfiles not possible when running a Server" << endl;
return false;
}
if ( !options.XMLoutFileName.empty() ){
LOG << "'-X " << options.XMLoutFileName
<< "' is invalid for multiple inputfiles." << endl;
return false;
}
}
string overridestatement;
while ( Opts.extract("override", overridestatement )) {
vector<string> values = TiCC::split_at( overridestatement, "=" );
const int num = values.size();
if ( num == 2 ) {
vector<string> module_param = TiCC::split_at(values[0], "." );
const int num2 = module_param.size();
if (num2 == 2) {
LOG << "Overriding configuration parameter " << module_param[0] << "." << module_param[1] << " with " << values[1] << endl;
configuration.setatt( module_param[1] , values[1], module_param[0] );
}
else if (num2 == 1) {
LOG << "Overriding configuration parameter " << module_param[0] << " with " << values[1] << endl;
configuration.setatt( module_param[0] , values[1]);
}
else {
LOG << "Invalid syntax for --override option" << endl;
}
}
else {
LOG << "Invalid syntax for --override option" << endl;
}
}
if ( !Opts.empty() ){
LOG << "unhandled commandline options: " << Opts.toString() << endl;
return false;
}
if ( options.doTagger && !configuration.hasSection("tagger") ){
LOG << "Missing [[tagger]] section in config file." << endl;
LOG << "cannot run without a tagger!" << endl;
return false;
}
if ( options.doTok && !configuration.hasSection("tokenizer") ){
LOG << "Missing [[tokenizer]] section in config file." << endl;
LOG << "Disabled the tokenizer." << endl;
options.doTok = false;
}
if ( options.doMbma && !configuration.hasSection("mbma") ){
LOG << "Missing [[mbma]] section in config file." << endl;
LOG << "Disabled the Morhological analyzer." << endl;
options.doMbma = false;
}
if ( options.doIOB ){
if ( !configuration.hasSection("IOB") ){
LOG << "Missing [[IOB]] section in config file." << endl;
LOG << "Disabled the IOB Chunker." << endl;
options.doIOB = false;
}
else {
test_version( configuration, "IOB", 2.0 );
}
}
if ( options.doNER ) {
if ( !configuration.hasSection("NER") ){
LOG << "Missing [[NER]] section in config file." << endl;
LOG << "Disabled the NER." << endl;
options.doNER = false;
}
else {
test_version( configuration, "NER", 2.0 );
}
}
if ( options.doMwu && !configuration.hasSection("mwu") ){
LOG << "Missing [[mwu]] section in config file." << endl;
LOG << "Disabled the Multi Word Unit." << endl;
LOG << "Also disabled the parser." << endl;
options.doMwu = false;
options.doParse = false;
}
if ( options.doParse && !configuration.hasSection("parser") ){
LOG << "Missing [[parser]] section in config file." << endl;
LOG << "Disabled the parser." << endl;
options.doParse = false;
}
if ( !options.outputFileName.empty() ){
if ( options.doRetry && TiCC::isFile( options.outputFileName ) ){
LOG << "retry, skip: " << options.outputFileName << " already exists" << endl;
return EXIT_SUCCESS;
}
if ( !TiCC::createPath( options.outputFileName ) ) {
LOG << "problem: unable to create outputfile: "
<< options.outputFileName << endl;
return EXIT_FAILURE;
}
outS = new ofstream( options.outputFileName );
}
return true;
}
FrogAPI::FrogAPI( TiCC::CL_Options& Opts,
TiCC::LogStream *err_log,
TiCC::LogStream *dbg_log ):
outS(0),
theErrLog(err_log),
theDbgLog(dbg_log),
myMbma(0),
myMblem(0),
myMwu(0),
myParser(0),
myCGNTagger(0),
myIOBTagger(0),
myNERTagger(0),
tokenizer(0)
{
/// Initialize an FrogAPI class
/*!
\param Opts CommandLine Options as found by parsing the command line
\param err_log A LogStream for error messages
\param dbg_log A LogStream for debugging purposes
This will throw on problems with 'Opts'
Otherwise a fully instantiated Frog will be available for further use
*/
// for some modules init can take a long time
// so first make sure it will not fail on some trivialities
//
TiCC::Configuration configuration;
bool parsed = collect_options( Opts, configuration, theErrLog );
if (!parsed) {
throw runtime_error( "init failed" );
}
run_api( configuration );
}
void FrogAPI::run_api( const TiCC::Configuration& configuration ){
if ( options.doServer ){
// we use fork(). omp (GCC version) doesn't do well when omp is used
// before the fork!
// see: https://bisqwit.iki.fi/story/howto/openmp/#OpenmpAndFork
tokenizer = new UctoTokenizer(theErrLog,theDbgLog);
bool stat = tokenizer->init( configuration );
if ( stat ){
tokenizer->setPassThru( !options.doTok );
tokenizer->setDocID( options.docid );
tokenizer->setSentencePerLineInput( options.doSentencePerLine );
tokenizer->setQuoteDetection( options.doQuoteDetection );
tokenizer->setInputEncoding( options.encoding );
tokenizer->setInputXml( options.doXMLin );
tokenizer->setUttMarker( options.uttmark );
tokenizer->setInputClass( options.inputclass );
tokenizer->setOutputClass( options.outputclass );
tokenizer->setWordCorrection( options.correct_words );
tokenizer->setUndLang( options.do_und_language );
tokenizer->setLangDetection( options.do_language_detection );
myCGNTagger = new CGNTagger( theErrLog,theDbgLog );
stat = myCGNTagger->init( configuration );
if ( stat ){
UnicodeString u_mark = TiCC::UnicodeFromUTF8( options.uttmark );
myCGNTagger->set_eos_mark( u_mark );
if ( options.doIOB ){
myIOBTagger = new IOBTagger( theErrLog, theDbgLog );
stat = myIOBTagger->init( configuration );
if ( stat ){
myIOBTagger->set_eos_mark( u_mark );
}
}
if ( stat && options.doNER ){
myNERTagger = new NERTagger( theErrLog, theDbgLog );
stat = myNERTagger->init( configuration );
if ( stat ){
myNERTagger->set_eos_mark( u_mark );
}
}
if ( stat && options.doLemma ){
myMblem = new Mblem(theErrLog,theDbgLog);
stat = myMblem->init( configuration );
}
if ( stat && options.doMbma ){
myMbma = new Mbma(theErrLog,theDbgLog);
stat = myMbma->init( configuration );
if ( stat ) {
if ( options.doDeepMorph ){
myMbma->setDeepMorph(true);
}
}
}
if ( stat && options.doAlpino ){
myParser = new AlpinoParser(theErrLog,theDbgLog);
stat = myParser->init( configuration );
}
else if ( stat && options.doMwu ){
myMwu = new Mwu(theErrLog,theDbgLog);
stat = myMwu->init( configuration );
if ( stat && options.doParse ){
myParser = new Parser(theErrLog,theDbgLog);
stat = myParser->init( configuration );
}
}
}
}
if ( !stat ){
LOG << "Frog initialization failed." << endl;
throw runtime_error( "Frog initialization failed" );
}
}
else {
#ifdef HAVE_OPENMP
omp_set_num_threads( options.numThreads );
int curt = omp_get_max_threads();
if ( curt != options.numThreads ){
LOG << "attempt to set to " << options.numThreads
<< " threads FAILED, running on " << curt
<< " threads instead" << endl;
}
else if ( options.debugFlag ){
DBG << "running on " << curt << " threads" << endl;
}
#endif
bool tokStat = true;
string tokWhat;
bool lemStat = true;
string lemWhat;
bool mwuStat = true;
string mwuWhat;
bool mbaStat = true;
string mbaWhat;
bool parStat = true;
string parWhat;
bool tagStat = true;
string tagWhat;
bool iobStat = true;
string iobWhat;
bool nerStat = true;
string nerWhat;
#pragma omp parallel sections
{
#pragma omp section
{
try {
tokenizer = new UctoTokenizer(theErrLog,theDbgLog);
tokStat = tokenizer->init( configuration );
}
catch ( const exception& e ){
tokWhat = e.what();
tokStat = false;