| 1 | /* regle.cpp */ |
| 2 | /* |
| 3 | * This file is part of PRAELECTOR. |
| 4 | * PRAELECTOR is free software; you can redistribute it and/or modify * it |
| 5 | * under the terms of the GNU General Public License as published by * the Free |
| 6 | * Software Foundation; either version 2 of the License, or * (at your option) |
| 7 | * any later version. |
| 8 | * |
| 9 | * COLLATINVS is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * |
| 12 | * This file is part of PRAELECTOR. |
| 13 | * |
| 14 | * PRAELECTOR is free software; you can redistribute it and/or modify |
| 15 | * it under the terms of the GNU General Public License as published by |
| 16 | * the Free Software Foundation; either version 2 of the License, or |
| 17 | * (at your option) any later version. |
| 18 | * |
| 19 | * PRAELECTOR is distributed in the hope that it will be useful, |
| 20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 22 | * GNU General Public License for more details. |
| 23 | * |
| 24 | * You should have received a copy of the GNU General Public License |
| 25 | * along with PRAELECTOR; if not, write to the Free Software |
| 26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 27 | * |
| 28 | * © Yves Ouvrard, 2009 - 2017 |
| 29 | */ |
| 30 | |
| 31 | // TODO |
| 32 | // - Ajouter dans Regle le moyen de fixer la morpho française de |
| 33 | // <sub> et <sup>, au moyen de deux nouvelles clés |
| 34 | // frsup: et frsub:. Il faudra overloader MotFlechi::tr() et |
| 35 | // MotFlechi::trGroupe(). |
| 36 | |
| 37 | #include <iostream> |
| 38 | #include <lemme.h> |
| 39 | #include <regle.h> |
| 40 | |
| 41 | QStringList const clesR = QStringList() |
| 42 | <<"accord" // 0 |
| 43 | <<"aff" |
| 44 | <<"conflit" |
| 45 | <<"contig" |
| 46 | <<"doc" |
| 47 | <<"id" // 5 |
| 48 | <<"filtre" |
| 49 | <<"lemmeSub" |
| 50 | <<"lemmeSup" |
| 51 | <<"lsSub" |
| 52 | <<"lsSup" // 10 |
| 53 | <<"morphoSub" |
| 54 | <<"morphoSup" |
| 55 | <<"poids" |
| 56 | <<"posSub" |
| 57 | <<"posSup" // 15 |
| 58 | <<"sens" |
| 59 | <<"subEstSup" |
| 60 | <<"supEstSub" |
| 61 | <<"supEstSup" |
| 62 | <<"tr" // 20 |
| 63 | <<"x"; |
| 64 | |
| 65 | ////////////////////////////// |
| 66 | // Classe Regle // |
| 67 | ////////////////////////////// |
| 68 | |
| 69 | Regle::Regle(QStringList ll) |
| 70 | { |
| 71 | _contig = 0; |
| 72 | for (int i=0;i<ll.count();++i) |
| 73 | { |
| 74 | QString lin = ll.at(i).simplified(); |
| 75 | QStringList ecl=lin.split(':', QString::KeepEmptyParts); |
| 76 | if (ecl.count() < 2) |
| 77 | { |
| 78 | std::cerr << "regles.la, ligne mal formée :"<<qPrintable(lin); |
| 79 | continue; |
| 80 | } |
| 81 | int p = clesR.indexOf(ecl.at(0)); |
| 82 | QString v = ecl.at(1); |
| 83 | switch(p) |
| 84 | { |
| 85 | case 0: _accord = v; break; |
| 86 | case 1: _aff = v; break; |
| 87 | case 2: _conflits = v.split(','); break; |
| 88 | case 3: _contig = v.toInt(); break; |
| 89 | case 4: _doc = v; break; |
| 90 | case 5: _id = v; break; |
| 91 | case 6: _filtre = v.split(','); break; |
| 92 | case 7: |
| 93 | { |
| 94 | QStringList eclats = v.split(','); |
| 95 | for (int i=0;i<eclats.count();++i) |
| 96 | { |
| 97 | QString ecl = eclats.at(i); |
| 98 | if (ecl.startsWith('-')) |
| 99 | { |
| 100 | ecl.remove(0,1); |
| 101 | _lSubExclus.append(ecl); |
| 102 | } |
| 103 | else _lemmeSub.append(ecl); |
| 104 | } |
| 105 | break; |
| 106 | } |
| 107 | case 8: |
| 108 | { |
| 109 | QStringList eclats = v.split(','); |
| 110 | for (int i=0;i<eclats.count();++i) |
| 111 | { |
| 112 | QString ecl = eclats.at(i); |
| 113 | if (ecl.startsWith('-')) |
| 114 | { |
| 115 | ecl.remove(0,1); |
| 116 | _lSupExclus.append(ecl); |
| 117 | } |
| 118 | else _lemmeSup.append(ecl); |
| 119 | } |
| 120 | break; |
| 121 | } |
| 122 | case 9: _lsSub = v.split(','); break; |
| 123 | case 10: _lsSup = v.split(','); break; |
| 124 | case 11: _morphoSub = v.split(','); break; |
| 125 | case 12: _morphoSup = v.split(','); break; |
| 126 | case 13: _poids = v.toInt(); break; |
| 127 | case 14: _posSub = v; break; |
| 128 | case 15: _posSup = v; break; |
| 129 | case 16: _sens = v[0]; break; |
| 130 | case 17: _subEstSup = v; break; |
| 131 | case 18: _supEstSub = v; break; |
| 132 | case 19: _supEstSup = v.split(','); break; |
| 133 | case 20: _tr.append(v); break; |
| 134 | case 21: _exclus = v.split(','); break; |
| 135 | |
| 136 | default: |
| 137 | { |
| 138 | std::cerr << qPrintable("regles.la:"+ecl.at(0)+"erreur de clé"); |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | // _tr manquant ? |
| 144 | if (_tr.isEmpty()) _tr.append("<sup> <sub>"); |
| 145 | // analyse des filtres |
| 146 | _antepos = 0; |
| 147 | for (int i=0;i<_filtre.count();++i) |
| 148 | { |
| 149 | QString f = _filtre.at(i); |
| 150 | if (f.startsWith("antep")) |
| 151 | _antepos = f.mid(5).toInt(); |
| 152 | } |
| 153 | _multi = _filtre.contains("multi"); |
| 154 | } |
| 155 | |
| 156 | QString Regle::accord() |
| 157 | { |
| 158 | return _accord; |
| 159 | } |
| 160 | |
| 161 | QString Regle::aff() |
| 162 | { |
| 163 | if (_aff.isEmpty()) return _id; |
| 164 | return _aff; |
| 165 | } |
| 166 | |
| 167 | int Regle::antepos() |
| 168 | { |
| 169 | return _antepos; |
| 170 | } |
| 171 | |
| 172 | bool Regle::conflit(QString id) |
| 173 | { |
| 174 | return _conflits.contains(id); |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | Regle::compatibleSu?Su? |
| 179 | Quatre fonctions qui disent si deux requêtes qui ont un |
| 180 | noeud en commun ont au moin un pos compatible pour ce |
| 181 | noeud. |
| 182 | */ |
| 183 | bool Regle::compatibleSubSub(Regle* r) |
| 184 | { |
| 185 | if (_posSub.isEmpty() || r->posSub().isEmpty()) return true; |
| 186 | QSet<QString> st = QSet<QString>::fromList(_posSub.split(',')); |
| 187 | QSet<QString> sr = QSet<QString>::fromList(r->posSub().split(',')); |
| 188 | return !(st.intersect(sr).empty()); |
| 189 | } |
| 190 | |
| 191 | bool Regle::compatibleSubSup(Regle* r) |
| 192 | { |
| 193 | if (_posSub.isEmpty() || r->posSup().isEmpty()) return true; |
| 194 | QSet<QString> st = QSet<QString>::fromList(_posSub.split(',')); |
| 195 | QSet<QString> sr = QSet<QString>::fromList(r->posSup().split(',')); |
| 196 | return !(st.intersect(sr).empty()); |
| 197 | } |
| 198 | |
| 199 | bool Regle::compatibleSupSub(Regle* r) |
| 200 | { |
| 201 | if (_posSup.isEmpty() || r->posSub().isEmpty()) return true; |
| 202 | QSet<QString> st = QSet<QString>::fromList(_posSup.split(',')); |
| 203 | QSet<QString> sr = QSet<QString>::fromList(r->posSub().split(',')); |
| 204 | return !(st.intersect(sr).empty()); |
| 205 | } |
| 206 | |
| 207 | bool Regle::compatibleSupSup(Regle* r) |
| 208 | { |
| 209 | if (_posSup.isEmpty() || r->posSup().isEmpty()) return true; |
| 210 | QSet<QString> st = QSet<QString>::fromList(_posSup.split(',')); |
| 211 | QSet<QString> sr = QSet<QString>::fromList(r->posSup().split(',')); |
| 212 | return !(st.intersect(sr).empty()); |
| 213 | } |
| 214 | |
| 215 | int Regle::contig() |
| 216 | { |
| 217 | return _contig; |
| 218 | } |
| 219 | |
| 220 | QString Regle::doc() |
| 221 | { |
| 222 | return _id + " " + _doc; |
| 223 | } |
| 224 | |
| 225 | bool Regle::estSub(MotFlechi* mf) |
| 226 | { |
| 227 | // lemme |
| 228 | Lemme* l = mf->lemme(); |
| 229 | QString cle = l->cle(); |
| 230 | if (subExclu(cle)) return false; |
| 231 | if (!_lemmeSub.empty() && !_lemmeSub.contains(cle)) return false; |
| 232 | // pos |
| 233 | if (!_posSub.isEmpty() |
| 234 | && !_posSub.contains(mf->pos()) |
| 235 | && !_posSub.contains(mf->posO())) |
| 236 | return false; |
| 237 | // morpho |
| 238 | QString m = mf->morpho(); |
| 239 | bool okm = _morphoSub.empty(); |
| 240 | if (!okm) for (int i=0;i<_morphoSub.count();++i) |
| 241 | okm = okm || inclus(_morphoSub.at(i), m); |
| 242 | if (!okm) return false; |
| 243 | // lexicosyntaxe |
| 244 | if (!_lsSub.empty()) |
| 245 | { |
| 246 | for (int i=0;i<_lsSub.count();++i) |
| 247 | { |
| 248 | if (l->synt(_lsSub.at(i))) |
| 249 | { |
| 250 | return true; |
| 251 | } |
| 252 | } |
| 253 | return false; |
| 254 | } |
| 255 | return true; |
| 256 | } |
| 257 | |
| 258 | bool Regle::estSuper(MotFlechi* mf) |
| 259 | { |
| 260 | // lemme |
| 261 | Lemme* l = mf->lemme(); |
| 262 | QString cle = l->cle(); |
| 263 | // déf de lemme |
| 264 | if (!_lemmeSup.empty() && !_lemmeSup.contains(cle)) return false; |
| 265 | // exclusions |
| 266 | if (_lSupExclus.contains(cle)) return false; |
| 267 | // pos |
| 268 | if (!_posSup.isEmpty() |
| 269 | && !_posSup.contains(mf->pos()) |
| 270 | && !_posSup.contains(mf->posO()) |
| 271 | && !(_posSup.contains("w") && mf->lemme()->synt("attr"))) |
| 272 | { |
| 273 | return false; |
| 274 | } |
| 275 | // morpho |
| 276 | QString m = mf->morpho(); |
| 277 | bool okm = _morphoSup.empty(); |
| 278 | if (!okm) for (int i=0;i<_morphoSup.count();++i) |
| 279 | { |
| 280 | okm = okm || inclus(_morphoSup.at(i), m); |
| 281 | } |
| 282 | if (!okm) return false; |
| 283 | // lexicosyntaxe |
| 284 | if (!_lsSup.empty()) |
| 285 | { |
| 286 | for (int i=0;i<_lsSup.count();++i) |
| 287 | { |
| 288 | if (l->synt(_lsSup.at(i))) return true; |
| 289 | } |
| 290 | return false; |
| 291 | } |
| 292 | return true; |
| 293 | } |
| 294 | |
| 295 | bool Regle::exclut(QString id) |
| 296 | { |
| 297 | return _exclus.contains(id); |
| 298 | } |
| 299 | |
| 300 | QString Regle::id() |
| 301 | { |
| 302 | return _id; |
| 303 | } |
| 304 | |
| 305 | QStringList Regle::filtre() |
| 306 | { |
| 307 | return _filtre; |
| 308 | } |
| 309 | |
| 310 | // tous les traits demandés par a doivent être dans b |
| 311 | bool Regle::inclus(QString a, QString b) |
| 312 | { |
| 313 | QStringList la = a.split(QRegularExpression("\\W")); |
| 314 | for (int i=0;i<la.count();++i) |
| 315 | if (!b.contains(la.at(i))) return false; |
| 316 | return true; |
| 317 | } |
| 318 | |
| 319 | bool Regle::intersect(QString a, QString b) |
| 320 | { |
| 321 | for (int i=0;i<a.length();++i) |
| 322 | if (b.contains(a.at(i))) return true; |
| 323 | return false; |
| 324 | } |
| 325 | |
| 326 | QStringList Regle::lemmeSub() |
| 327 | { |
| 328 | return _lemmeSub; |
| 329 | } |
| 330 | |
| 331 | QString Regle::subEstSup() |
| 332 | { |
| 333 | return _subEstSup; |
| 334 | } |
| 335 | |
| 336 | QString Regle::supEstSub() |
| 337 | { |
| 338 | return _supEstSub; |
| 339 | } |
| 340 | |
| 341 | QStringList Regle::supEstSup() |
| 342 | { |
| 343 | return _supEstSup; |
| 344 | } |
| 345 | |
| 346 | bool Regle::lsSub(QString l) |
| 347 | { |
| 348 | return _lsSub.contains(l); |
| 349 | } |
| 350 | |
| 351 | QString Regle::lsSub(int i) |
| 352 | { |
| 353 | return _lsSub.at(i); |
| 354 | } |
| 355 | |
| 356 | bool Regle::lsSup(QString l) |
| 357 | { |
| 358 | return _lsSup.contains(l); |
| 359 | } |
| 360 | |
| 361 | QString Regle::lsSup(int i) |
| 362 | { |
| 363 | return _lsSup.at(i); |
| 364 | } |
| 365 | |
| 366 | QStringList Regle::morphoSub() |
| 367 | { |
| 368 | return _morphoSub; |
| 369 | } |
| 370 | |
| 371 | QStringList Regle::morphoSup() |
| 372 | { |
| 373 | return _morphoSup; |
| 374 | } |
| 375 | |
| 376 | bool Regle::multi() |
| 377 | { |
| 378 | return _multi; |
| 379 | } |
| 380 | |
| 381 | int Regle::nbLsSub() |
| 382 | { |
| 383 | return _lsSub.count(); |
| 384 | } |
| 385 | |
| 386 | int Regle::nbLsSup() |
| 387 | { |
| 388 | return _lsSup.count(); |
| 389 | } |
| 390 | |
| 391 | int Regle::nbTr() |
| 392 | { |
| 393 | return _tr.count(); |
| 394 | } |
| 395 | |
| 396 | int Regle::poids() |
| 397 | { |
| 398 | return _poids; |
| 399 | } |
| 400 | |
| 401 | QString Regle::posSub() |
| 402 | { |
| 403 | return _posSub; |
| 404 | } |
| 405 | |
| 406 | QString Regle::posSup() |
| 407 | { |
| 408 | return _posSup; |
| 409 | } |
| 410 | |
| 411 | QChar Regle::sens() |
| 412 | { |
| 413 | return _sens; |
| 414 | } |
| 415 | |
| 416 | bool Regle::subExclu(QString s) |
| 417 | { |
| 418 | return _lSubExclus.contains(s); |
| 419 | } |
| 420 | |
| 421 | bool Regle::supExclu(QString s) |
| 422 | { |
| 423 | return _lSupExclus.contains(s); |
| 424 | } |
| 425 | |
| 426 | QString Regle::tr(int i) |
| 427 | { |
| 428 | return _tr.at(i); |
| 429 | } |