~ubuntu-branches/ubuntu/trusty/frog/trusty

« back to all changes in this revision

Viewing changes to src/ner_tagger_mod.cxx

  • Committer: Package Import Robot
  • Author(s): Joost van Baal-Ilić
  • Date: 2012-06-23 08:09:59 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20120623080959-n65vdjojh02n3bjx
Tags: 0.12.15-2
Rebuild on amd64 to pull in libicu48 (was libicu44).

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
  $Id: ner_tagger_mod.cxx 14351 2012-02-29 10:07:03Z sloot $
 
2
  $Id: ner_tagger_mod.cxx 14481 2012-03-21 11:26:55Z sloot $
3
3
  $URL: https://ilk.uvt.nl/svn/sources/Frog/trunk/src/ner_tagger_mod.cxx $
4
4
 
5
5
  Copyright (c) 2006 - 2012
30
30
 
31
31
#include "mbt/MbtAPI.h"
32
32
#include "libfolia/folia.h"
 
33
#include "libfolia/document.h"
33
34
#include "frog/Frog.h"
34
35
#include "frog/ner_tagger_mod.h"
35
36
 
75
76
    *Log(nerLog) << "NER Tagger is already initialized!" << endl;
76
77
    return false;
77
78
  }  
78
 
  string tagset = conf.lookUp( "settings", "NER" );
79
 
  if ( tagset.empty() ){
 
79
  string val = conf.lookUp( "settings", "NER" );
 
80
  if ( val.empty() ){
80
81
    *Log(nerLog) << "Unable to find settings for NER" << endl;
81
82
    return false;
82
83
  }
83
 
  string init = "-s " + configuration.configDir() + tagset;
84
 
  if ( Tagger::Version() == "3.2.6" )
85
 
    init += " -vc";
86
 
  else
87
 
    init += " -vcf";
 
84
  string settings = val;
 
85
  val = conf.lookUp( "version", "NER" );
 
86
  if ( val.empty() ){
 
87
    version = "1.0";
 
88
  }
 
89
  else
 
90
    version = val;
 
91
  val = conf.lookUp( "set", "NER" );
 
92
  if ( val.empty() ){
 
93
    tagset = "http://ilk.uvt.nl/folia/sets/frog-ner-nl";
 
94
  }
 
95
  else
 
96
    tagset = val;
 
97
 
 
98
  string init = "-s " + configuration.configDir() + settings + " -vcf";
88
99
  tagger = new MbtAPI( init, *nerLog );
89
100
  return tagger != 0;
90
101
}
91
102
 
92
 
bool NERTagger::splitOneWT( const string& inp, string& word, 
93
 
                            string& tag, string& confidence ){
94
 
  bool isKnown = true;
95
 
  string in = inp;
96
 
  //     split word and tag, and store num of slashes
97
 
  if (debug)
98
 
    *Log(nerLog) << "split Classify starting with " << in << endl;
99
 
  string::size_type pos = in.rfind("/");
100
 
  if ( pos == string::npos ) {
101
 
    *Log(nerLog) << "no word/tag/confidence triple in this line: " << in << endl;
102
 
    exit( EXIT_FAILURE );
103
 
  }
104
 
  else {
105
 
    confidence = in.substr( pos+1 );
106
 
    in.erase( pos );
107
 
  }
108
 
  pos = in.rfind("//");
109
 
  if ( pos != string::npos ) {
110
 
    // double slash: lets's hope is is an unknown word
111
 
    if ( pos == 0 ){
112
 
      // but this is definitely something like //LET() 
113
 
      word = "/";
114
 
      tag = in.substr(pos+2);
115
 
    }
116
 
    else {
117
 
      word = in.substr( 0, pos );
118
 
      tag = in.substr( pos+2 );
119
 
    }
120
 
    isKnown = false;
121
 
  } 
122
 
  else {
123
 
    pos = in.rfind("/");
124
 
    if ( pos != string::npos ) {
125
 
      word = in.substr( 0, pos );
126
 
      tag = in.substr( pos+1 );
127
 
    }
128
 
    else {
129
 
      *Log(nerLog) << "no word/tag/confidence triple in this line: " << in << endl;
130
 
      exit( EXIT_FAILURE );
131
 
    }
132
 
  }
133
 
  if ( debug){
134
 
    if ( isKnown )
135
 
      *Log(nerLog) << "known";
136
 
    else
137
 
      *Log(nerLog) << "unknown";
138
 
    *Log(nerLog) << " word: " << word << "\ttag: " << tag << "\tconfidence: " << confidence << endl;
139
 
  }
140
 
  return isKnown;
141
 
}
142
 
 
143
 
int NERTagger::splitWT( const string& tagged,
144
 
                        vector<string>& tags, 
145
 
                        vector<double>& conf ){
146
 
  vector<string> words;
147
 
  vector<string> tagwords;
148
 
  vector<bool> known;
149
 
  tags.clear();
150
 
  conf.clear();
151
 
  size_t num_words = Timbl::split_at( tagged, tagwords, " " );
152
 
  num_words--; // the last "word" is <utt> which gets added by the tagger
153
 
  for( size_t i = 0; i < num_words; ++i ) {
154
 
    string word, tag, confs;
155
 
    bool isKnown = splitOneWT( tagwords[i], word, tag, confs );
156
 
    double confidence;
157
 
    if ( !stringTo<double>( confs, confidence ) ){
158
 
      *Log(nerLog) << "tagger confused. Expected a double, got '" << confs << "'" << endl;
159
 
      exit( EXIT_FAILURE );
160
 
    }
161
 
    words.push_back( word );
162
 
    tags.push_back( tag );
163
 
    known.push_back( isKnown );
164
 
    conf.push_back( confidence );
165
 
  }
166
 
  if (debug) {
167
 
    *Log(nerLog) << "#tagged_words: " << num_words << endl;
168
 
    for( size_t i = 0; i < num_words; i++) 
169
 
      *Log(nerLog)   << "\ttagged word[" << i <<"]: " << words[i] << (known[i]?"/":"//")
170
 
             << tags[i] << " <" << conf[i] << ">" << endl;
171
 
  }
172
 
  return num_words;
173
 
}
174
 
 
175
103
static void addEntity( EntitiesLayer *entities, 
 
104
                       const string& tagset,
176
105
                       const vector<Word*>& words,
177
106
                       const vector<double>& confs,
178
107
                       const string& NER ){
182
111
  KWargs args;
183
112
  args["class"] = NER;
184
113
  args["confidence"] =  toString(c);
185
 
  args["set"] = "http://ilk.uvt.nl/folia/sets/frog-ner-nl";
 
114
  args["set"] = tagset;
186
115
  Entity *e = 0;
187
116
#pragma omp critical(foliaupdate)
188
117
  {
226
155
          using folia::operator<<;
227
156
          *Log(nerLog) << "spit out " << stack << endl; 
228
157
        }
229
 
        addEntity( el, stack, dstack, curNER );
 
158
        addEntity( el, tagset, stack, dstack, curNER );
230
159
        dstack.clear();
231
160
        stack.clear();
232
161
      }
250
179
          using folia::operator<<;
251
180
          *Log(nerLog) << "spit out " << stack << endl; 
252
181
        }
253
 
        addEntity( el, stack, dstack, curNER );
 
182
        addEntity( el, tagset, stack, dstack, curNER );
254
183
        dstack.clear();
255
184
        stack.clear();
256
185
      }
265
194
      using folia::operator<<;
266
195
      *Log(nerLog) << "spit out " << stack << endl;     
267
196
    }
268
 
    addEntity( el, stack, dstack, curNER );
269
 
  }
270
 
}
271
 
 
272
 
string NERTagger::Classify( Sentence *sent ){
273
 
  string tagged;
274
 
  vector<Word*> swords = sent->words();
 
197
    addEntity( el, tagset, stack, dstack, curNER );
 
198
  }
 
199
}
 
200
 
 
201
void NERTagger::addDeclaration( Document& doc ) const {
 
202
  doc.declare( AnnotationType::ENTITY, 
 
203
               tagset,
 
204
               "annotator='frog-ner-" + version
 
205
               + "', annotatortype='auto'");
 
206
}
 
207
 
 
208
void NERTagger::Classify( Sentence *sent ){
 
209
  vector<Word*> swords;
 
210
#pragma omp critical(foliaupdate)
 
211
  {
 
212
    swords = sent->words();
 
213
  }
275
214
  if ( !swords.empty() ) {
276
215
    vector<string> words;
277
216
    string sentence; // the tagger needs the whole sentence
283
222
    }
284
223
    if (debug) 
285
224
      *Log(nerLog) << "NER in: " << sentence << endl;
286
 
    tagged = tagger->Tag(sentence);
287
 
    if (debug) {
288
 
      *Log(nerLog) << "sentence: " << sentence << endl
289
 
                   << "NER tagged: "<< tagged
290
 
                   << endl;
 
225
    vector<TagResult> tagv = tagger->TagLine(sentence);
 
226
    if ( tagv.size() != swords.size() ){
 
227
      throw runtime_error( "NER tagger is confused" );
 
228
    }
 
229
    if ( debug ){
 
230
      *Log(nerLog) << "NER tagger out: " << endl;
 
231
      for ( size_t i=0; i < tagv.size(); ++i ){
 
232
        *Log(nerLog) << "[" << i << "] : word=" << tagv[i].word() 
 
233
                     << " tag=" << tagv[i].assignedTag() 
 
234
                     << " confidence=" << tagv[i].confidence() << endl;
 
235
      }
291
236
    }
292
237
    vector<double> conf;
293
 
    vector<string> tags;
294
 
    splitWT( tagged, tags, conf );
 
238
    vector<string> tags;    
 
239
    for ( size_t i=0; i < tagv.size(); ++i ){
 
240
      tags.push_back( tagv[i].assignedTag() );
 
241
      conf.push_back( tagv[i].confidence() );
 
242
    }
295
243
    addNERTags( sent, swords, tags, conf );
296
244
  }
297
 
  return tagged;
298
245
}
299
246
 
300
247