-
Notifications
You must be signed in to change notification settings - Fork 11
/
ner_prog.cxx
189 lines (166 loc) · 5.37 KB
/
ner_prog.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
/* 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/ner_tagger_mod.h"
#include <string>
#include <iostream>
#include <vector>
#include "config.h"
#include "ticcutils/LogStream.h"
#include "ticcutils/Configuration.h"
#include "ticcutils/CommandLine.h"
#include "ticcutils/PrettyPrint.h"
#include "libfolia/folia.h"
#include "mbt/MbtAPI.h"
#include "frog/ucto_tokenizer_mod.h"
using namespace std;
using namespace Timbl;
using namespace Tagger;
TiCC::LogStream my_default_log( cerr, "", StampMessage ); // fall-back
TiCC::LogStream *theErrLog = &my_default_log; // fill the externals
vector<string> fileNames;
TiCC::Configuration configuration;
static string configDir = string(SYSCONF_PATH) + "/" + PACKAGE + "/";
static string configFileName = configDir + "frog.cfg";
static UctoTokenizer tokenizer(theErrLog);
void usage( ) {
cout << endl << "Options:\n";
cout << "\t============= INPUT MODE (mandatory, choose one) ========================\n"
<< "\t -t <testfile> Run NER on this file\n"
<< "\t -c <filename> Set configuration file (default " << configFileName << ")\n"
<< "\t============= OTHER OPTIONS ============================================\n"
<< "\t -h. give some help.\n"
<< "\t -V or --version . Show version info.\n"
<< "\t -d <debug level> (for more verbosity)\n";
}
static NERTagger tagger(theErrLog);
bool parse_args( TiCC::CL_Options& Opts ) {
if ( Opts.is_present( 'V' ) || Opts.is_present("version") ){
// we already did show what we wanted.
exit( EXIT_SUCCESS );
}
if ( Opts.is_present('h') ){
usage();
exit( EXIT_SUCCESS );
};
// is a config file specified?
Opts.extract( 'c', configFileName );
if ( configuration.fill( configFileName ) ){
cerr << "config read from: " << configFileName << endl;
}
else {
cerr << "failed to read configuration from! '" << configFileName << "'" << endl;
cerr << "did you correctly install the frogdata package?" << endl;
return false;
}
string value;
// debug opts
if ( Opts.extract('d', value ) ){
int debug = 0;
if ( !TiCC::stringTo<int>( value, debug ) ){
cerr << "-d value should be an integer" << endl;
return false;
}
configuration.setatt( "debug", value, "NER" );
};
if ( Opts.extract( 't', value ) ){
ifstream is( value );
if ( !is ){
cerr << "input stream " << value << " is not readable" << endl;
return false;
}
fileNames.push_back( value );
}
else {
fileNames = Opts.getMassOpts();
}
return true;
}
bool init(){
// for some modules init can take a long time
// so first make sure it will not fail on some trivialities
//
if ( !tokenizer.init( configuration ) ){
cerr << "UCTO Initialization failed." << endl;
return false;
}
if ( !tagger.init( configuration ) ){
cerr << "NER Initialization failed." << endl;
return false;
}
cerr << "Initialization done." << endl;
return true;
}
void Test( istream& in ){
while ( in.good() ){
UnicodeString sentence = tokenizer.tokenizeStream( in );
if ( sentence.isEmpty() ){
break;
}
cout << "processing: " << sentence << endl;
vector<TagResult> tagv = tagger.tagLine(sentence);
for ( const auto& tr : tagv ){
cout << tr.word() << "\t" << tr.assigned_tag() << endl;
}
cout << "<utt>" << endl << endl;
}
}
int main(int argc, char *argv[]) {
std::ios_base::sync_with_stdio(false);
cerr << "ner " << VERSION << " (c) CLTS, ILK 2014 - 2024." << endl;
cerr << "CLST - Centre for Language and Speech Technology,"
<< "Radboud University" << endl
<< "ILK - Induction of Linguistic Knowledge Research Group,"
<< "Tilburg University" << endl;
TiCC::CL_Options Opts("Vt:d:hc:","version");
try {
Opts.init(argc, argv);
}
catch ( const exception& e ){
cerr << "fatal error: " << e.what() << endl;
return EXIT_FAILURE;
}
cerr << "based on [" << Timbl::VersionName() << "]" << endl;
cerr << "configdir: " << configDir << endl;
if ( parse_args(Opts) ){
if ( !init() ){
cerr << "terminated." << endl;
return EXIT_FAILURE;
}
for ( const auto& TestFileName : fileNames ){
ifstream in(TestFileName);
if ( in.good() ){
cerr << "Processing: " << TestFileName << endl;
Test( in );
}
else {
cerr << "unable to open: " << TestFileName << endl;
return EXIT_FAILURE;
}
}
}
else {
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}