LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
automatonReaderWriter.cpp
Go to the documentation of this file.
1// Copyright 2002-2020 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6/************************************************************************
7 *
8 * @file automatonReaderWriter.cpp
9 * @author besancon (besanconr@zoe.cea.fr)
10 * @date Fri Mar 18 2005
11 * copyright Copyright (C) 2005-2020 by CEA LIST
12 *
13 ***********************************************************************/
14
16#include "automatonCommon.h"
17#include "recognizer.h"
18#include "rule.h"
19#include "automaton.h"
20#include "constraintFunction.h"
21
22
23// transitions
24#include "transitionUnit.h"
25#include "posTransition.h"
26#include "wordTransition.h"
27#include "lemmaTransition.h"
28#include "numericTransition.h"
29#include "epsilonTransition.h"
30#include "starTransition.h"
31#include "tstatusTransition.h"
32#include "andTransition.h"
33#include "setTransition.h"
35#include "entityTransition.h"
37
40
41
42using namespace std;
43using namespace Lima::Common;
45using namespace Lima::Common::MediaticData;
46
47namespace Lima {
48namespace LinguisticProcessing {
49namespace Automaton {
50
51/***********************************************************************/
52// read/write utility function for type of transition
53/***********************************************************************/
54TypeTransition readTypeTransition(std::ifstream& file) {
55 return static_cast<TypeTransition>(Common::Misc::readCodedInt(file));
56}
57void writeTypeTransition(std::ofstream& file, const TypeTransition t) {
59}
60
61
62#define RECOGNIZER_VERSION "1.30"
63#define RECOGNIZER_DEBUG_VERSION ".debug"
64
65//----------------------------------------------------------------------
66// Reader
67//----------------------------------------------------------------------
69m_constraintFunctionMap(),
70m_entityGroupMapping(),
71m_entityTypeMapping(),
72m_debug(false)
73{}
75
77readRecognizer(const std::string& filename,
78 Recognizer& reco)
79{
80#ifdef LDEBUG
82 LDEBUG << "AutomatonReader::readRecognizer" << filename;
83#endif
84 if (! filename.empty() &&
85 filename[filename.size()-1] == '/') {
87 ostringstream oss;
88 oss << "Cannot open [" << filename
89 << "] ; it is a directory";
90 LERROR << oss.str();
91 throw OpenFileException(oss.str());
92 }
93 ifstream file(filename.c_str(), std::ios::in | std::ios::binary);
94 if (!(file.is_open() && file.good()) ) {
96 ostringstream oss;
97 oss << "Cannot open file [" << filename << "]";
98 LERROR << oss.str();
99 throw OpenFileException(oss.str());
100 }
101 try {
102 readRecognizer(file,reco);
103 }
104 catch (std::exception& ) {
105 AULOGINIT;
106 LERROR << "failed reading file: " << filename;
107 throw;
108 }
109 file.close();
110}
111
112void AutomatonReader::readRecognizer(std::ifstream& file,
113 Recognizer& reco)
114{
115#ifdef LDEBUG
116 AULOGINIT;
117 LDEBUG << "AutomatonReader::readRecognizer";
118#endif
119
120 MediaId language=readHeader(file);
121 reco.m_language = language;
122
123 // read all rules in storage
124 reco.m_ruleStorage.clear();
125 uint64_t nrules=Misc::readCodedInt(file);
126 reco.m_ruleStorage.reserve(nrules);
127 for (uint64_t i(0); i<nrules; i++)
128 {
129 Rule* r = readRule(file,reco.m_language);
130 reco.m_ruleStorage.push_back(r);
131 }
132
133 uint64_t n=Misc::readCodedInt(file);
134 reco.m_rules.clear();
135 reco.m_rules.reserve(n);
136 for (uint64_t i(0); i<n; i++)
137 {
138 TransitionUnit *t = readTransitionUnit(file,reco.m_language); // read trigger
139 uint64_t nRulesTransition=Misc::readCodedInt(file);
140 if (nRulesTransition)
141 {
142 uint64_t index=Misc::readCodedInt(file);
143 reco.m_rules.push_back(Recognizer::TriggerRule(t,SetOfRules(1,reco.m_ruleStorage[index])));
144 reco.m_rules[i].second.reserve(nRulesTransition);
145 for (uint64_t j(1); j<nRulesTransition; j++)
146 {
147 index=Misc::readCodedInt(file);
148 reco.m_rules[i].second.push_back(reco.m_ruleStorage[index]);
149 }
150 }
151 }
152
154}
155
157readHeader(std::ifstream& file)
158{
159 // read version number
160 string version;
161 Misc::readStringField(file,version);
162 string::size_type deb=version.find(RECOGNIZER_DEBUG_VERSION);
163 if (deb!=string::npos) {
164 m_debug=true;
165 version=string(version,0,deb);
166 }
167 if (version != RECOGNIZER_VERSION)
168 {
169 AULOGINIT;
170 ostringstream oss;
171 oss << "incompatible version of binary rules format: "
172 << version << ", current version is "
174 LERROR << oss.str();
175 throw runtime_error(oss.str());
176 }
177
178 // read language
179 std::string language;
180 Misc::readStringField(file,language);
181
182 MediaId lang=static_cast<MediaId>(0);
183 // if language is specified
184 if (!language.empty())
185 {
186// // see if it is already initialized (if not, do it)
187// if (Common::MediaticData::MediaticData::single().getMediasIds().find(language)
188// ==Common::MediaticData::MediaticData::single().getMediasIds().end())
189// {
190// AULOGINIT;
191// ostringstream oss;
192// oss << "language " << language
193// << " is not initialized. ";
194// #if __cplusplus >= 201703L
195// oss << "Initialized languages are: ";
196// for (const auto &[k, v] : Common::MediaticData::MediaticData::single().getMediasIds())
197// oss << k << ", ";
198// #endif
199//
200// LERROR << oss.str();
201// throw runtime_error(oss.str());
202// }
204 }
205
206 // readEntityType and save mapping
207 Common::MediaticData::MediaticData::changeable().readEntityTypes(file, m_entityGroupMapping, m_entityTypeMapping);
208
209 // initialize constraint functions
210 readRegisteredConstraints(file,lang);
211
212 return lang;
213}
214
216readRule(std::ifstream& file, MediaId language)
217{
218 Rule* rule=new Rule;
219
220 //read rule id if debug
221 if (m_debug) {
222 std::string id;
223 Misc::readStringField(file,id);
224 rule->setRuleId(id);
225 }
226
227 TransitionUnit *t=readTransitionUnit(file,language);
228 rule->m_trigger=t;
229 readAutomaton(file,rule->m_left,language);
230 readAutomaton(file,rule->m_right,language);
231
232 // read type of expression
233 EntityGroupId groupId=static_cast<EntityGroupId>(Misc::readCodedInt(file));
234 EntityTypeId typeId=static_cast<EntityTypeId>(Misc::readCodedInt(file));
235 // use EntityType mapping
236 rule->setType(m_entityTypeMapping[EntityType(typeId,groupId)]);
238
239 // read list attribute values of normalized form
241
243 file.read((char *) &(rule->m_contextual), sizeof(bool));
244 file.read((char *) &(rule->m_negative), sizeof(bool));
245 file.read((char *) &(rule->m_hasLeftRightConstraint), sizeof(bool));
246
247 // read actions attached to the rule
248 uint64_t nbActions=Misc::readCodedInt(file);
249 rule->m_actions.reserve(nbActions);
250 for (uint64_t i(0); i<nbActions; i++) {
251 Constraint c;
252 readConstraint(file,c);
253 rule->m_actions.push_back(c);
254 }
255 uint64_t nbActionsWithOneArgument=Misc::readCodedInt(file);
256 for (uint64_t i(0); i<nbActionsWithOneArgument; i++) {
257 Constraint c;
258 readConstraint(file,c);
259 LimaString id;
261 rule->m_actionsWithOneArgument.push_back(std::pair<LimaString,Constraint>(id,c));
262 }
263 file.read((char*) &(rule->m_weight),sizeof(double));
264 return rule;
265}
266
268readAutomaton(std::ifstream& file,
269 Automaton& a,
270 MediaId language)
271{
272 a.reinit();
273 Tstate nbStates;
274 // first read the number of states
275 nbStates=readTstate(file);
276
277 // read deterministic status
278 file.read((char*) &(a.m_deterministic), sizeof(bool));
279
280 // read about the final states
281 a.m_finalStates = vector<bool>(nbStates,false);
282 Tstate nbFinals(0);
283 nbFinals=readTstate(file);
284 for (uint64_t i(0); i<nbFinals; i++) {
285 Tstate s=readTstate(file);
286 a.m_finalStates[s] = true;
287 }
288 // read the informations about each state
289 for (uint64_t i(0); i<nbStates; i++) {
290 Tstate lastStateAdded=a.addState();
291 // the number of transitions from this state
292 Tstate nbTrans=Misc::readCodedInt(file);
293 // the transitions
294 a.m_transitions[lastStateAdded].reserve(nbTrans);
295 for (uint64_t j(0); j<nbTrans; j++) {
296 Transition c;
297 c.setTransitionUnit(readTransitionUnit(file,language));
298 Tstate state=readTstate(file);
299 c.setNextState(state);
300 a.m_transitions[lastStateAdded].push_back(c);
301 }
302 }
303 //a.initializeSearchStructures(language);
304}
305
307readConstraint(std::ifstream& file,
308 Constraint& c)
309{
310 c.reinit();
311 // read id of constraint function
312 uint64_t id=Misc::readCodedInt(file);
313 c.m_functionAddr= m_constraintFunctionMap[id];
314
315 c.m_index=Misc::readCodedInt(file);
316 c.m_action=static_cast<ConstraintAction>(Misc::readOneByteInt(file));
317 file.read((char *) &(c.m_negative), sizeof(bool));
318}
319
321readRegisteredConstraints(std::ifstream& file,
322 MediaId language) {
323 m_constraintFunctionMap.clear();
324 uint64_t nbFunctions=Misc::readCodedInt(file);
325 for (uint64_t i(0); i!=nbFunctions; i++) {
326 // write constraint id for binary file
327 uint64_t n=Misc::readCodedInt(file);
328 string functionName("");
329 LimaString complement;
330 // write function name
331 Misc::readStringField(file,functionName);
332 // write function complement
333 Misc::readUTF8StringField(file,complement);
334
335 // store id <-> function in map
336 ConstraintFunction* functionPtr=
338 getConstraintFunction(functionName,language,complement);
339 m_constraintFunctionMap[n]=functionPtr;
340 }
341}
342
344readTransitionUnit(std::ifstream& file,MediaId language)
345{
346 TransitionUnit *t=0;
347 TypeTransition codeTrans=
348 static_cast<TypeTransition>(Misc::readOneByteInt(file));
349 switch(codeTrans) {
350 case T_WORD: {
352 Tword s;
353 readTword(file,s,sp);
354 t=new WordTransition(s);
355 break; }
356 case T_NUM: {
357 uint64_t val=Misc::readCodedInt(file);
358 uint64_t min=Misc::readCodedInt(file);
359 uint64_t max=Misc::readCodedInt(file);
360 t=new NumericTransition(val,min,max);
361 break; }
362 case T_POS: {
363 const Common::PropertyCode::PropertyAccessor* macroAccessor=&(static_cast<const Common::MediaticData::LanguageData&>(Common::MediaticData::MediaticData::single().mediaData(language)).getPropertyCodeManager().getPropertyAccessor("MACRO"));
364 const Common::PropertyCode::PropertyAccessor* microAccessor=&(static_cast<const Common::MediaticData::LanguageData&>(Common::MediaticData::MediaticData::single().mediaData(language)).getPropertyCodeManager().getPropertyAccessor("MICRO"));
365 Tpos p;
366 readTpos(file,p);
367 t=new PosTransition(p,macroAccessor,microAccessor);
368 break; }
369 case T_LEMMA: {
370 const Common::PropertyCode::PropertyAccessor* macroAccessor=&(static_cast<const Common::MediaticData::LanguageData&>(Common::MediaticData::MediaticData::single().mediaData(language)).getPropertyCodeManager().getPropertyAccessor("MACRO"));
371 const Common::PropertyCode::PropertyAccessor* microAccessor=&(static_cast<const Common::MediaticData::LanguageData&>(Common::MediaticData::MediaticData::single().mediaData(language)).getPropertyCodeManager().getPropertyAccessor("MICRO"));
373 Tpos p;
374 Tword s;
375 readTword(file,s,sp);
376 readTpos(file,p);
377 t=new LemmaTransition(s,p,macroAccessor,microAccessor);
378 break; }
379 case T_TSTATUS: {
380 //enum types coded in file as uint64_t
381 StatusType s=static_cast<StatusType>(Misc::readOneByteInt(file));
384 NumericType n=static_cast<NumericType>(Misc::readOneByteInt(file));
385 unsigned char h;
386 unsigned char p;
387 file.read((char *) &h, sizeof(unsigned char));
388 file.read((char *) &p, sizeof(unsigned char));
389
390 LimaString defaultKey;
391 Misc::readUTF8StringField(file,defaultKey);
392
393 TStatus status(c,r,h,p,false,false,n,s);
394 status.setDefaultKey(defaultKey);
395
396 t=new TStatusTransition(status);
397 break;
398 }
399 case T_GAZETEER: {
400 // read alias
401 LimaString alias;
402 Misc::readUTF8StringField(file,alias);
403 // read set of words
404 std::vector<LimaString> wordVector;
405 readWordVector(file,wordVector);
406 // read keep
407 int keepVal = Misc::readCodedInt(file);
408 // create transition
409 t=new GazeteerTransition(wordVector, alias, keepVal == 1);
410 break; }
411 case T_AND: {
412 uint64_t size=Misc::readCodedInt(file);
413 vector<TransitionUnit*> tmp(size);
414 for (uint64_t i(0); i<size; i++) {
415 tmp[i]=readTransitionUnit(file,language);
416 }
417 t=new AndTransition(tmp);
419 break;
420 }
421 case T_SET: {
423 set<Tword> tmp;
424 uint64_t size=Misc::readCodedInt(file);
425 for (uint64_t i(0); i<size; i++) {
426 LimaString elt;
428 tmp.insert(sp[elt]);
429 }
430 t=new SetTransition(tmp);
431 break;
432 }
433 case T_DEACCENTUATED: {
434 LimaString deaccentuatedForm;
435 MediaId language;
436 Misc::readUTF8StringField(file,deaccentuatedForm);
437 file.read((char*)&language,sizeof(unsigned char));
438 t=new DeaccentuatedTransition(deaccentuatedForm,language);
439 break;
440 }
441 case T_EPSILON: {
442 t=new EpsilonTransition();
443 break;
444 }
445 case T_STAR: {
446 t=new StarTransition();
447 break;
448 }
449 case T_ENTITY: {
450 EntityGroupId groupId=static_cast<EntityGroupId>(Misc::readCodedInt(file));
451 EntityTypeId typeId=static_cast<EntityTypeId>(Misc::readCodedInt(file));
452 // use entity type mapping
453 t=new EntityTransition(m_entityTypeMapping[EntityType(typeId,groupId)]);
454 break;
455 }
456 case T_ENTITY_GROUP: {
457 EntityGroupId groupId=static_cast<EntityGroupId>(Misc::readCodedInt(file));
458 // use entityGroup mapping
459 t=new EntityGroupTransition(m_entityGroupMapping[groupId]);
460 break;
461 }
462 default: {
463 AULOGINIT;
464 LERROR << "Undefined type of transition: " << codeTrans;
465 }
466 }
467 // read properties
468 if (t!=0) {
469 bool keep, neg, head;
470 int len;
471 file.read((char*)&keep, sizeof(bool));
472 t->setKeep(keep);
473 file.read((char*)&neg, sizeof(bool));
474 t->setNegative(neg);
475 file.read((char*)&head, sizeof(bool));
476 t->setHead(head);
477 file.read((char*)&len, sizeof(len));
478 char *buf = new char [len];
479 file.read(buf, len);
480 t->setId(std::string(buf,len));
481 delete[] buf;
482 uint64_t n=Misc::readCodedInt(file);
483 Constraint c;
484 for (uint64_t i(0); i<n; i++) {
485 readConstraint(file,c);
486 t->addConstraint(c);
487 }
488 }
489 return t;
490}
491
492//----------------------------------------------------------------------
493// Writer
494//----------------------------------------------------------------------
496m_constraintFunctionMap(),
497m_debug(false)
498{}
499
501
503writeRecognizer(const Recognizer& reco,
504 const std::string& filename,
505 MediaId language,
506 bool debug)
507{
508 ofstream file(filename.c_str(), std::ios::out | std::ios::binary);
509 if (!file) {
510 AULOGINIT;
511 ostringstream oss;
512 oss << "Cannot open file [" << filename << "]";
513 LERROR << oss.str();
514 throw OpenFileException(oss.str());
515 }
516 writeRecognizer(file,reco,language,debug);
517 file.close();
518}
519
521writeRecognizer(std::ofstream& file,
522 const Recognizer& reco,
523 MediaId language,
524 bool debug)
525{
526 m_debug=debug;
527 writeHeader(file,language);
528
529 //write all rules
530 uint64_t nRule(reco.m_ruleStorage.size());
531 Common::Misc::writeCodedInt(file,nRule);
532 for (uint64_t i(0); i<nRule; i++)
533 {
534 writeRule(file,*(reco.m_ruleStorage[i]),language);
535 }
536
537 // then write the structure with only indexes of rules (instead of pointers)
538 uint64_t n(reco.m_rules.size());
539 Misc::writeCodedInt(file,n);
540 for (uint64_t i(0); i<n; i++)
541 {
542 writeTransitionUnit(file,reco.m_rules[i].first,language); // write transition
543 uint64_t nRulesTransition(reco.m_rules[i].second.size());
544 Misc::writeCodedInt(file,nRulesTransition);
545 for (uint64_t j(0); j<nRulesTransition; j++)
546 {
547 Misc::writeCodedInt(file,reco.findRuleIndex(reco.m_rules[i].second[j]));
548 }
549 }
550}
551
553writeHeader(std::ofstream& file,
554 MediaId language)
555{
556 std::string version(RECOGNIZER_VERSION);
557 if (m_debug) {
558 version.append(RECOGNIZER_DEBUG_VERSION);
559 }
560
561 // write version number
562 Misc::writeStringField(file,version);
563
564 // write language
566
567 // write entity types
569
570 // write registered constraints (after language, because
571 // language is needed when reinitializing constraints)
573}
574
576writeRule(std::ofstream& file,
577 const Rule& rule,
578 MediaId language)
579{
580 // write id if debug
581 if (m_debug) {
582 //LOGINIT("LP::Automaton");
583 //LDEBUG<< "write rule id '"<< rule.m_ruleId<< "'";
585 }
586
587 writeTransitionUnit(file,rule.m_trigger,language);
588 writeAutomaton(file,rule.m_left,language);
589 writeAutomaton(file,rule.m_right,language);
590
591 // write type of expression
594
596
597 // write normalized form (LimaString)
599
601 file.write((char *) &(rule.m_contextual), sizeof(bool));
602 file.write((char *) &(rule.m_negative), sizeof(bool));
603 file.write((char *) &(rule.m_hasLeftRightConstraint), sizeof(bool));
604
605 // write actions attached to the rule
606 uint64_t nbActions=rule.m_actions.size();
607 Common::Misc::writeCodedInt(file,nbActions);
608 for (uint64_t i(0); i<nbActions; i++) {
609 writeConstraint(file,rule.m_actions[i]);
610 }
611
612 // write actions (with one argument) attached to the rule
613 uint64_t nbActionsWithOneArgument=rule.m_actionsWithOneArgument.size();
614 Common::Misc::writeCodedInt(file,nbActionsWithOneArgument);
615 for (std::vector<std::pair<LimaString,Constraint>>::const_iterator it = rule.m_actionsWithOneArgument.begin() ;
616 it != rule.m_actionsWithOneArgument.end() ; it++ ) {
617 writeConstraint(file,it->second);
618 Misc::writeUTF8StringField(file,it->first);
619 }
620 file.write((char*) &(rule.m_weight),sizeof(double));
621}
622
624writeAutomaton(std::ofstream& file,
625 const Automaton& a,
626 MediaId language)
627{
628 // first write the number of states
629 writeTstate(file,a.m_numberStates);
630
631 // read deterministic status
632 file.write((char*) &(a.m_deterministic), sizeof(bool));
633
634 // write the final states
635 // (list of final states instead of full boolean vector)
636 vector<Tstate> finals(a.finalStates());
637 Tstate nbFinals(finals.size());
638 writeTstate(file,nbFinals);
639 for (uint64_t i(0); i<nbFinals; i++) {
640 writeTstate(file,finals[i]);
641 }
642
643 // then write the informations about each state
644 for (uint64_t i(0); i<a.m_numberStates; i++) {
645 // the number of transitions from this state
646 Tstate nbTrans(a.m_transitions[i].size());
647// std::cerr << "State: " << i << ": " << nbTrans << " transitions" << endl;
648 Common::Misc::writeCodedInt(file,nbTrans);
649 // the transitions
650 for (uint64_t j(0); j<nbTrans; j++) {
651// std::cerr << "Transition: " << *(m_transitions[i][j].transitionUnit()) << endl;
652 writeTransitionUnit(file,a.m_transitions[i][j].transitionUnit(),language);
653 Tstate state(a.m_transitions[i][j].nextState());
654 writeTstate(file,state);
655 }
656 }
657}
658
660writeTransitionUnit(std::ofstream& file,
661 TransitionUnit* transition,
662 MediaId language)
663{
664 // write specific informations
665 TypeTransition type=transition->type();
666 Misc::writeOneByteInt(file,type);
667 switch (type) {
668 case T_WORD: {
670 WordTransition* t=static_cast<WordTransition*>(transition);
671 writeTword(file,t->word(),sp);
672 break;
673 }
674 case T_POS: {
675 PosTransition* t=static_cast<PosTransition*>(transition);
676 writeTpos(file,t->pos());
677 break;
678 }
679 case T_LEMMA: {
681 LemmaTransition* t=static_cast<LemmaTransition*>(transition);
682 writeTword(file,t->lemma(),sp);
683 writeTpos(file,t->partOfSpeech());
684 break;
685 }
686 case T_GAZETEER: {
687 GazeteerTransition* t=static_cast<GazeteerTransition*>(transition);
689 writeWordSet(file,t->wordSet());
690 if( t->keep() )
691 Misc::writeCodedInt(file,1);
692 else
693 Misc::writeCodedInt(file,0);
694 break;
695 }
696 case T_NUM: {
697 NumericTransition* t=static_cast<NumericTransition*>(transition);
698 Misc::writeCodedInt(file,t->value());
699 Misc::writeCodedInt(file,t->min());
700 Misc::writeCodedInt(file,t->max());
701 break;
702 }
703 case T_TSTATUS: {
704 TStatusTransition* t=static_cast<TStatusTransition*>(transition);
705
706 // write the Tstatus
707 const TStatus& status=t->status();
708 StatusType s(status.getStatus());
710 AlphaRomanType r(status.getAlphaRoman());
711 NumericType n(status.getNumeric());
712 unsigned char h(status.isAlphaHyphen());
713 unsigned char p(status.isAlphaPossessive());
714
715 Misc::writeOneByteInt(file,s); // enum type StatusType as int
716 Misc::writeOneByteInt(file,a); // enum type AlphaCapitalType as int
717 Misc::writeOneByteInt(file,r); // enum type AlphaRomanType as int
718 Misc::writeOneByteInt(file,n); // enum type NumericType as int
719 file.write((char *) &h, sizeof(unsigned char));
720 file.write((char *) &p, sizeof(unsigned char));
722 break;
723 }
724 case T_AND: {
725 AndTransition* t=static_cast<AndTransition*>(transition);
726 const vector<TransitionUnit*>& transitions=t->getTransitions();
727 uint64_t size(transitions.size());
728 Misc::writeCodedInt(file,size);
729 vector<TransitionUnit*>::const_iterator
730 it=transitions.begin(),
731 it_end=transitions.end();
732 for (; it!=it_end; it++) {
733 writeTransitionUnit(file,*it,language);
734 }
735 break;
736 }
737 case T_SET: {
738 SetTransition* t=static_cast<SetTransition*>(transition);
739 const std::set<Tword>& words=t->getWords();
740 Misc::writeCodedInt(file,words.size());
741 set<Tword>::const_iterator
742 w=words.begin(),
743 w_end=words.end();
744 for (;w!=w_end; w++) {
746 }
747 break;
748 }
749 case T_DEACCENTUATED: {
750 DeaccentuatedTransition* t=static_cast<DeaccentuatedTransition*>(transition);
752 MediaId lang=t->getLanguage();
753 file.write((char*) &lang,sizeof(unsigned char));
754 break;
755 }
756 case T_ENTITY_GROUP: {
757 EntityGroupTransition* t=static_cast<EntityGroupTransition*>(transition);
758 EntityGroupId entityGroupId=t->entityGroupId();
759 Misc::writeCodedInt(file,entityGroupId);
760 break;
761 }
762 case T_ENTITY: {
763 EntityTransition* t=static_cast<EntityTransition*>(transition);
764 EntityType entityType=t->entityType();
765 Misc::writeCodedInt(file,entityType.getGroupId());
766 Misc::writeCodedInt(file,entityType.getTypeId());
767 break;
768 }
769 case T_EPSILON:
770 case T_STAR:
771 // nothing to do
772 break;
773 default: {
774 AULOGINIT;
775 LERROR << "Undefined type of transition: " << type;
776 }
777 }
778
779 // write common transition unit properties
780 bool keep=transition->keep();
781 bool negative=transition->negative();
782 bool head=transition->head();
783 std::string id = transition->getId();
784 file.write((char *) &keep, sizeof(bool));
785 file.write((char *) &negative, sizeof(bool));
786 file.write((char *) &head, sizeof(bool));
787 int len = id.size();
788 std::string(id.c_str(),len);
789#ifdef DEBUG_LP
790 LOGINIT("Automaton::Compiler");
791 LDEBUG << "Transition Writer: write id = " << id;
792#endif
793 file.write((char *) &len, sizeof(len));
794 file.write((char *) id.c_str(), len);
795 uint64_t n=transition->numberOfConstraints();
796 Misc::writeCodedInt(file,n);
797 for (uint64_t i(0); i<n; i++) {
798 writeConstraint(file,transition->constraint(i));
799 }
800}
801
803writeConstraint(std::ofstream& file,
804 const Constraint& c)
805{
806 // write id of constraint function
807 Misc::writeCodedInt(file,m_constraintFunctionMap[c.m_functionAddr]);
808
809 Misc::writeCodedInt(file,c.m_index);
810 Misc::writeOneByteInt(file,c.m_action);
811 file.write((char *) &(c.m_negative), sizeof(bool));
812}
813
815writeRegisteredConstraints(std::ofstream& file)
816{
817 const std::multimap<std::string,ConstraintFunction*>&
818 registered=
820 getRegisteredFunctions();
821
822 std::multimap<std::string,ConstraintFunction*>::
823 const_iterator
824 c=registered.begin(),
825 c_end=registered.end();
826
827 uint64_t nbFunctions=registered.size();
828 Misc::writeCodedInt(file,nbFunctions);
829 uint64_t n(1);
830 for (; c!=c_end; c++) {
831 // write constraint id for binary file
832 Misc::writeCodedInt(file,n);
833 // write function name
834 Misc::writeStringField(file,(*c).first);
835 // write function complement
836 Misc::writeUTF8StringField(file,(*c).second->getComplementString());
837 // store function <-> id in map
838 m_constraintFunctionMap[(*c).second]=n;
839 n++;
840 }
841}
842
843} // end namespace
844} // end namespace
845} // end namespace
#define LOGINIT(X)
Definition LimaCommon.h:187
#define LDEBUG
Definition LimaCommon.h:157
#define LERROR
Definition LimaCommon.h:161
#define AULOGINIT
#define RECOGNIZER_VERSION
#define RECOGNIZER_DEBUG_VERSION
Holds linguistic data for one language.
const FsaStringsPool & stringsPool(MediaId med) const
const MediaData & mediaData(MediaId media) const
void writeEntityTypes(std::ostream &os) const
void readEntityTypes(std::istream &os, std::map< EntityGroupId, EntityGroupId > &EntityGroupIdMapping, std::map< EntityType, EntityType > &EntityTypeMapping)
const std::string & media(MediaId media) const
Provide function to read write and check a property.
static std::ostream & encodeToBinary(std::ostream &os, const LinguisticCode &value)
Definition StdBitset.h:173
static LinguisticCode decodeFromBinary(std::istream &is)
Definition StdBitset.h:197
std::vector< TransitionUnit * > & getTransitions()
void readRecognizer(const std::string &filename, Recognizer &reco)
Rule * readRule(std::ifstream &file, MediaId language)
void readConstraint(std::ifstream &file, Constraint &c)
void readRegisteredConstraints(std::ifstream &file, MediaId language)
TransitionUnit * readTransitionUnit(std::ifstream &file, MediaId language)
void readAutomaton(std::ifstream &file, Automaton &a, MediaId language)
void writeRule(std::ofstream &file, const Rule &rule, MediaId language)
void writeConstraint(std::ofstream &file, const Constraint &c)
void writeTransitionUnit(std::ofstream &file, TransitionUnit *transition, MediaId language)
void writeAutomaton(std::ofstream &file, const Automaton &automaton, MediaId language)
void writeRecognizer(const Recognizer &reco, const std::string &filename, MediaId language, bool debug=false)
␈rief A class for the description of automata
Definition automaton.h:87
Common::MediaticData::EntityGroupId entityGroupId() const
Common::MediaticData::EntityType entityType() const
␈rief a class for the definition of a complete recognizer
Definition recognizer.h:78
std::vector< Constraint > m_actions
Definition rule.h:139
LinguisticCode getLinguisticProperties() const
Definition rule.h:161
void setType(const Common::MediaticData::EntityType &)
Definition rule.h:188
void setRuleId(const std::string &ruleId)
Definition rule.h:84
void setLinguisticProperties(const LinguisticCode &)
Definition rule.h:189
std::vector< std::pair< LimaString, Constraint > > m_actionsWithOneArgument
Definition rule.h:140
Common::MediaticData::EntityType getType() const
Definition rule.h:160
const LinguisticAnalysisStructure::TStatus & status() const
const Constraint & constraint(const uint64_t) const
void setDefaultKey(const Lima::LimaString &defaultKey)
Definition TStatus.cpp:325
static const MediaticData & single()
const singleton accessor
Definition Singleton.h:51
static MediaticData & changeable()
singleton accessor
Definition Singleton.h:71
void writeUTF8StringField(std::ostream &file, const Lima::LimaString &s)
Binary write a a LimaString in a file as an utf8 string.
uint8_t readOneByteInt(std::istream &file)
read a integer coded on one byte in a file
uint64_t readCodedInt(std::istream &file)
read a integer coded in variable-byte format in a file
void readStringField(istream &file, string &s)
void writeOneByteInt(std::ostream &file, const uint8_t number)
write a integer coded on one byte format in a file
void writeStringField(ostream &file, const string &s)
void readUTF8StringField(std::istream &file, Lima::LimaString &s)
Binary read an utf8 string from a file and store it in a LimaString.
void writeCodedInt(std::ostream &file, const uint64_t number)
write a integer coded in variable-byte format in a file
void writeTpos(std::ofstream &file, const Tpos &p)
void readTword(std::ifstream &file, Tword &s, FsaStringsPool &sp)
void readTpos(std::ifstream &file, Tpos &p)
void writeTstate(std::ofstream &file, const Tstate s)
void writeTypeTransition(std::ofstream &file, const TypeTransition t)
void readWordVector(std::ifstream &file, std::vector< LimaString > &wordVector)
void writeTword(std::ofstream &file, const Tword &s, const FsaStringsPool &sp)
void writeWordSet(std::ofstream &file, const std::set< LimaString > &wordSet)
Tstate readTstate(std::ifstream &file)
TypeTransition readTypeTransition(std::ifstream &file)
std::vector< Rule * > SetOfRules
the SetOfRules type is defined as a vector of pointers on Rule
Definition recognizer.h:66
NAUTITIA.
QString LimaString
Definition LimaString.h:33
STL namespace.