forked from aymara/lima
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruleCompiler.cpp
225 lines (199 loc) · 7.02 KB
/
ruleCompiler.cpp
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
/*
Copyright 2002-2020 CEA LIST
This file is part of LIMA.
LIMA is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
LIMA 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with LIMA. If not, see <http://www.gnu.org/licenses/>
*/
/************************************************************************
*
* File : ruleCompiler.cpp
* Author : Besancon Romaric ([email protected])
* Created on : Mon Apr 7 2003
* Copyright : (c) 2003 by CEA
* Version : $Id: ruleCompiler.cpp 9081 2008-02-25 18:34:51Z de-chalendarg $
*
************************************************************************/
#include "ruleCompiler.h"
#include "ruleString.h"
#include "automatonCompiler.h"
#include "transitionCompiler.h"
#include "compilerExceptions.h"
#include "ruleFormat.h"
#include "tstring.h"
#include "linguisticProcessing/core/Automaton/rule.h"
#include "linguisticProcessing/core/Automaton/constraint.h"
#include "common/MediaticData/mediaticData.h"
#include "common/Data/strwstrtools.h"
#include <iostream>
#include "common/time/timeUtilsController.h"
using namespace std;
namespace Lima {
using namespace Common;
namespace LinguisticProcessing {
namespace Automaton {
using namespace AutomatonCompiler;
namespace RuleCompiler {
/***********************************************************************/
// building the rule from a string
/***********************************************************************/
LimaString initRule(Rule& r,
const LimaString& str,
MediaId language,
const std::vector<Gazeteer>& gazeteers,
const std::vector<SubAutomaton>& subAutomatons,
const std::vector<LimaString>& activeEntityGroups,
const std::string& filename,
uint64_t lineNumber)
{
AUCLOGINIT;
#ifdef DEBUG_LP
LDEBUG << "RuleCompiler:initializing rule " << str;
#endif
//LimaString ruleString;
// Lima::TimeUtilsController* ctrl6 = new Lima::TimeUtilsController("build RuleString and function inside compiler", true);
RuleString s(str,language,gazeteers,subAutomatons);
try
{
// copy constraint informations to the rule
// set the number of constraints
r.setNumberOfConstraints(s.getNbConstraints());
r.setHasLeftRightConstraint(s.hasLeftRightConstraint());
// add the actions to the rule
for (std::vector<Constraint>::const_iterator a=s.getActions().begin();
a!=s.getActions().end(); a++)
{
r.addAction(*a);
}
for (std::vector<std::pair<LimaString,Constraint> >::const_iterator a=s.getActionsWithOneArgument().begin();
a!=s.getActionsWithOneArgument().end(); a++)
{
r.addAction(a->second,a->first);
}
//ruleString=s.getString();
}
catch (AutomatonCompilerException& exception)
{
LERROR << "Error on rule: " << str;
throw;
}
// delete ctrl6;
//LDEBUG << "RuleCompiler:string=" << ruleString;
// Lima::TimeUtilsController* ctrl7 = new Lima::TimeUtilsController("setTrigger", true);
r.setTrigger(createTransition(s.getTrigger(),language,"trigger",activeEntityGroups));
// delete ctrl7;
try
{
// Lima::TimeUtilsController* ctrl8 = new Lima::TimeUtilsController("setLeftAutomaton", true);
r.setLeftAutomaton(AutomatonCompiler::buildAutomaton(s.getLeft(),
language,gazeteers,
BACKWARDSEARCH,
activeEntityGroups,
r.getActionsWithOneArgument()));
// delete ctrl8;
}
catch (AutomatonCompilerException& e)
{
LERROR << "Error: "<< e.what()
<< " on left part of rule: " << str;
throw;
}
try
{
// Lima::TimeUtilsController* ctrl9 = new Lima::TimeUtilsController("setRightAutomaton", true);
r.setRightAutomaton(AutomatonCompiler::buildAutomaton(s.getRight(),
language,gazeteers,
FORWARDSEARCH,
activeEntityGroups,
r.getActionsWithOneArgument()));
LDEBUG << "RuleCompiler:initRule: r.rightAutomaton = " << r.rightAutomaton();
// delete ctrl9;
}
catch (AutomatonCompilerException& e)
{
LERROR << "Error: "<< e.what()
<< " on left part of rule: " << str;
throw;
}
LimaString& stringType=s.getType();
try
{
if (stringType.indexOf(*STRING_NEGATIVE_TYPE_RULE)==0)
{
setType(r,
stringType.mid(LENGTH_NEGATIVE_TYPE_RULE),
activeEntityGroups);
r.setNegative(true);
}
else if (stringType.indexOf(*STRING_ABSOLUTE_TYPE_RULE)==0)
{
setType(r,
stringType.mid(
LENGTH_ABSOLUTE_TYPE_RULE),
activeEntityGroups);
r.setContextual(false);
}
else
{
setType(r,stringType,activeEntityGroups);
}
}
catch (UnknownTypeException&)
{
LERROR << "Error on rule [" << str << "]";
throw;
}
// Lima::TimeUtilsController* ctrl10 = new Lima::TimeUtilsController("setNormalizedForm", true);
r.setNormalizedForm(s.getNorm());
ostringstream oss;
oss << filename << ":" << lineNumber;
#ifdef DEBUG_LP
LDEBUG << "rule id is '" << oss.str() << "' / filename="<< filename << ",lineNumber=" << lineNumber;
#endif
r.setRuleId(oss.str());
// delete ctrl10;
// delete ctrl5;
return str;
}
//**********************************************************************
// find the numeric form of type of expression from a text form
//**********************************************************************
void setType(Rule& r,
const LimaString& s,
const std::vector<LimaString>& activeEntityGroups)
{
LimaString str;
//std::string::size_type i(findSpecialCharacter(s,CHAR_POS_TR,0));
int i(findSpecialCharacter(s,CHAR_POS_TR,0));
if (i != -1)
{
// there are linguistic properties
r.setLinguisticProperties(LinguisticCode::fromString(s.mid(i+1).toStdString()));
str=s.left(i);
}
else
{
r.setLinguisticProperties(L_NONE);
str=s;
}
Common::MediaticData::EntityType type=
resolveEntityName(str,activeEntityGroups);
if (type.isNull())
{
std::ostringstream oss;
oss << "type [" << str.toUtf8().data() << "] not recognized";
throw UnknownTypeException(oss.str());
}
r.setType(type);
}
} // end namespace
} // end namespace
} // end namespace
} // end namespace