| 1 | /* lemmatiseur.cpp |
| 2 | * |
| 3 | * This file is part of COLLATINUS. |
| 4 | * |
| 5 | * COLLATINUS is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * COLLATINVS is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with COLLATINUS; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | * |
| 19 | * © Yves Ouvrard, 2009 - 2016 |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * \file lemmatiseur.cpp |
| 24 | * \brief module de lemmatisation des formes latines |
| 25 | */ |
| 26 | |
| 27 | #include <QDir> |
| 28 | #include <QFile> |
| 29 | #include <QRegExp> |
| 30 | #include <QTextStream> |
| 31 | #include <iostream> |
| 32 | |
| 33 | #include "ch.h" |
| 34 | #include "lemmatiseur.h" |
| 35 | |
| 36 | #include <QDebug> |
| 37 | // #include <QElapsedTimer> |
| 38 | // #define DEBOG |
| 39 | |
| 40 | /** |
| 41 | * \fn Lemmat::Lemmat (QObject *parent) |
| 42 | * \brief Constructeur de la classe Lemmat. |
| 43 | * |
| 44 | * Il définit quelques constantes, initialise |
| 45 | * les options à false, et appelle les fonctions |
| 46 | * de lecture des données : modèles, lexique, |
| 47 | * traductions et irréguliers. |
| 48 | */ |
| 49 | Lemmat::Lemmat(QObject *parent, QString resDir) : QObject(parent) |
| 50 | { |
| 51 | if (resDir.isEmpty()) |
| 52 | _resDir = qApp->applicationDirPath() + "/data/"; |
| 53 | else if (resDir.endsWith("/")) _resDir = resDir; |
| 54 | else _resDir.append("/"); |
| 55 | // options |
| 56 | _alpha = false; |
| 57 | _formeT = false; |
| 58 | _html = false; |
| 59 | _majPert = false; |
| 60 | _morpho = false; |
| 61 | _extension = false; |
| 62 | _extLoaded = false; |
| 63 | // suffixes |
| 64 | suffixes.insert("ne", "nĕ"); |
| 65 | suffixes.insert("que", "quĕ"); |
| 66 | suffixes.insert("ue", "vĕ"); |
| 67 | suffixes.insert("ve", "vĕ"); |
| 68 | suffixes.insert("st", "st"); |
| 69 | // assimilations |
| 70 | ajAssims(); |
| 71 | // contractions |
| 72 | ajContractions(); |
| 73 | // POS |
| 74 | ajPos(); |
| 75 | // lecture des morphos |
| 76 | _morphos = lignesFichier(_resDir + "morphos.la"); |
| 77 | |
| 78 | lisModeles(); |
| 79 | lisLexique(); |
| 80 | lisTraductions(true, false); |
| 81 | posCf(); |
| 82 | lisIrreguliers(); |
| 83 | lisLexicoSyntaxe(); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * \fn void Lemmat::ajAssims () |
| 88 | * \brief définit les débuts de mots |
| 89 | * non assimilés, et associe à chacun sa |
| 90 | * forme assimilée. |
| 91 | */ |
| 92 | void Lemmat::ajAssims() |
| 93 | { |
| 94 | // peupler la QMap assims |
| 95 | QStringList lignes = lignesFichier(_resDir + "assimilations.la"); |
| 96 | foreach(QString lin, lignes) |
| 97 | { |
| 98 | if (lin.isEmpty()) continue; |
| 99 | QStringList liste = lin.split(':'); |
| 100 | assimsq.insert(liste.at(0), liste.at(1)); |
| 101 | assims.insert(Ch::atone(liste.at(0)), Ch::atone(liste.at(1))); |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * \fn void Lemmat::ajContractions () |
| 107 | * \brief Établit une liste qui donne, pour chaque |
| 108 | * contraction, la forme non contracte qui lui |
| 109 | * correspond. |
| 110 | */ |
| 111 | void Lemmat::ajContractions() |
| 112 | { |
| 113 | // peupler la QMap _contractions |
| 114 | QStringList lignes = lignesFichier(_resDir + "contractions.la"); |
| 115 | foreach(QString lin, lignes) |
| 116 | { |
| 117 | QStringList liste = lin.split(':'); |
| 118 | _contractions.insert(liste.at(0), liste.at(1)); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * \fn void Lemmat::ajDesinence (Desinence *d) |
| 124 | * \brief ajoute la désinence d dans la map des |
| 125 | * désinences. |
| 126 | */ |
| 127 | void Lemmat::ajDesinence(Desinence *d) |
| 128 | { |
| 129 | _desinences.insert(Ch::deramise(d->gr()), d); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * \fn void Lemmat::ajRadicaux (Lemme *l) |
| 134 | * \brief Calcule tous les radicaux du lemme l, |
| 135 | * en se servant des modèles, les ajoute à ce lemme, |
| 136 | * et ensuite à la map * des radicaux de la classe Lemmat. |
| 137 | * |
| 138 | */ |
| 139 | void Lemmat::ajRadicaux(Lemme *l) |
| 140 | { |
| 141 | // ablŭo=ā̆blŭo|lego|ā̆blŭ|ā̆blūt|is, ere, lui, lutum |
| 142 | // 0 1 2 3 4 |
| 143 | // ăvellāna=ăvēllāna,ăbēllāna|uita|||ae, f.|2 |
| 144 | Modele *m = modele(l->grModele()); |
| 145 | // insérer d'abord les radicaux définis dans lemmes.la |
| 146 | foreach (int i, l->clesR()) |
| 147 | { |
| 148 | QList<Radical *> lr = l->radical(i); |
| 149 | foreach (Radical *r, lr) |
| 150 | { |
| 151 | _radicaux.insert(Ch::deramise(r->gr()), r); |
| 152 | } |
| 153 | } |
| 154 | // pour chaque radical du modèle |
| 155 | // test avec ăvellāna=ăvēllāna,ăbēllāna|uita|||ae, f.|2 |
| 156 | foreach (int i, m->clesR()) |
| 157 | { |
| 158 | if (l->clesR().contains(i)) continue; |
| 159 | QStringList gs = l->grq().split(','); |
| 160 | foreach (QString g, gs) |
| 161 | { |
| 162 | Radical *r = NULL; |
| 163 | QString gen = m->genRadical(i); |
| 164 | // si gen == 'K', le radical est la forme canonique |
| 165 | if (gen == "K") |
| 166 | r = new Radical(g, i, l); |
| 167 | else |
| 168 | { |
| 169 | // sinon, appliquer la règle de formation du modèle |
| 170 | int oter = gen.section(',', 0, 0).toInt(); |
| 171 | QString ajouter = gen.section(',', 1, 1); |
| 172 | if (g.endsWith(0x0306)) g.chop(1); |
| 173 | g.chop(oter); |
| 174 | if (ajouter != "0") g.append(ajouter); |
| 175 | r = new Radical(g, i, l); |
| 176 | } |
| 177 | l->ajRadical(i, r); |
| 178 | _radicaux.insert(Ch::deramise(r->gr()), r); |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | void Lemmat::ajPos() |
| 184 | { |
| 185 | mapPos.insert("adj.", 'a'); |
| 186 | mapPos.insert("adv.", 'd'); |
| 187 | mapPos.insert("conj.", 'c'); |
| 188 | mapPos.insert("excl.", 'e'); |
| 189 | mapPos.insert("interj.", 'i'); |
| 190 | mapPos.insert("num.", 'm'); |
| 191 | mapPos.insert("pron.", 'p'); |
| 192 | mapPos.insert("prép.", 'r'); |
| 193 | mapPos.insert("subst.", 'n'); |
| 194 | mapPos.insert(" v.", 'V'); |
| 195 | mapPos.insert("vb.", 'V'); |
| 196 | mapPos.insert("intr.", 'v'); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * \fn QString Lemmat::assim (QString a) |
| 201 | * \brief Cherche si la chaîne a peut subir |
| 202 | * une assimilation, et renvoie |
| 203 | * cette chaîne éventuellement assimilée. |
| 204 | * *version avec quantités* |
| 205 | */ |
| 206 | QString Lemmat::assim(QString a) |
| 207 | { |
| 208 | foreach (QString d, assims.keys()) |
| 209 | if (a.startsWith(d)) |
| 210 | { |
| 211 | a.replace(d, assims.value(d)); |
| 212 | return a; |
| 213 | } |
| 214 | return a; |
| 215 | } |
| 216 | |
| 217 | int Lemmat::aRomano (QString r) |
| 218 | { |
| 219 | if (r.size () == 0) return 0; |
| 220 | // création de la table de conversion : pourrait être créée ailleurs. |
| 221 | QMap<QChar,int> conversion; |
| 222 | conversion['I']=1; |
| 223 | conversion['V']=5; |
| 224 | conversion['X']=10; |
| 225 | conversion['L']=50; |
| 226 | conversion['C']=100; |
| 227 | conversion['D']=500; |
| 228 | conversion['M']=1000; |
| 229 | // calcul du résultat : ajout d'un terme si l'ordre est normal, soustraction sinon. |
| 230 | int res=0; |
| 231 | int conv_s = conversion[r[0]]; |
| 232 | for (int i = 0; i < r.count()-1; i++) |
| 233 | { |
| 234 | int conv_c = conv_s; |
| 235 | conv_s = conversion[r[i+1]]; |
| 236 | if (conv_c < conv_s) |
| 237 | res -= conv_c; |
| 238 | else res += conv_c; |
| 239 | } |
| 240 | res += conv_s; |
| 241 | return res; |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * \fn QString Lemmat::assimq (QString a) |
| 246 | * \brief Cherche si la chaîne a peut subir |
| 247 | * une assimilation, et renvoie |
| 248 | * cette chaîne éventuellement assimilée. |
| 249 | * *version avec quantités* |
| 250 | */ |
| 251 | QString Lemmat::assimq(QString a) |
| 252 | { |
| 253 | foreach (QString d, assimsq.keys()) |
| 254 | if (a.startsWith(d)) |
| 255 | { |
| 256 | a.replace(d, assimsq.value(d)); |
| 257 | return a; |
| 258 | } |
| 259 | return a; |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | QMap<QString, QString> Lemmat::cibles() |
| 264 | { |
| 265 | return _cibles; |
| 266 | } |
| 267 | */ |
| 268 | |
| 269 | /** |
| 270 | * \fn QString Lemmat::desassim (QString a) |
| 271 | * \brief Essaie de remplacer l'assimilation de a |
| 272 | * par sa forme non assimilée, et renvoie |
| 273 | * le résultat. |
| 274 | */ |
| 275 | QString Lemmat::desassim(QString a) |
| 276 | { |
| 277 | foreach (QString d, assims.values()) |
| 278 | { |
| 279 | if (a.startsWith(d)) |
| 280 | { |
| 281 | a.replace(d, assims.key(d)); |
| 282 | return a; |
| 283 | } |
| 284 | } |
| 285 | return a; |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * \fn QString Lemmat::desassimq (QString a) |
| 290 | * \brief Essaie de remplacer l'assimilation de a |
| 291 | * par sa forme non assimilée, et renvoie |
| 292 | * le résultat. |
| 293 | */ |
| 294 | QString Lemmat::desassimq(QString a) |
| 295 | { |
| 296 | foreach (QString d, assimsq.values()) |
| 297 | { |
| 298 | if (a.startsWith(d)) |
| 299 | { |
| 300 | a.replace(d, assimsq.key(d)); |
| 301 | return a; |
| 302 | } |
| 303 | } |
| 304 | return a; |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Fonction de conversion |
| 309 | * des chiffres romains en chiffres arabes. |
| 310 | * |
| 311 | */ |
| 312 | bool Lemmat::estRomain(QString r) |
| 313 | { |
| 314 | return !(r.contains(QRegExp ("[^IVXLCDM]")) |
| 315 | || r.contains("IL") |
| 316 | || r.contains("IVI")); |
| 317 | } |
| 318 | |
| 319 | /** |
| 320 | * \fn MapLem Lemmat::lemmatise (QString f) |
| 321 | * \brief Le cœur du lemmatiseur |
| 322 | * |
| 323 | * renvoie une QMap<Lemme*,QStringlist> contenant |
| 324 | * - la liste de tous les lemmes pouvant donner |
| 325 | * la forme f; |
| 326 | * - pour chacun de ces lemmes la QStringList des morphologies |
| 327 | * correspondant à la forme. |
| 328 | */ |
| 329 | MapLem Lemmat::lemmatise(QString f) |
| 330 | { |
| 331 | MapLem result; |
| 332 | if (f.isEmpty()) return result; |
| 333 | QString f_lower = f.toLower(); |
| 334 | int cnt_v = f_lower.count("v"); |
| 335 | bool V_maj = f[0] == 'V'; |
| 336 | int cnt_ae = f_lower.count("æ"); |
| 337 | int cnt_oe = f_lower.count("œ"); |
| 338 | f = Ch::deramise(f); |
| 339 | // formes irrégulières |
| 340 | QList<Irreg *> lirr = _irregs.values(f); |
| 341 | foreach (Irreg *irr, lirr) |
| 342 | { |
| 343 | foreach (int m, irr->morphos()) |
| 344 | { |
| 345 | SLem sl = {irr->grq(), irr->lemme(), morpho(m), "", irr->lemme()->pos().at(0).toLatin1()}; |
| 346 | result[irr->lemme()].prepend(sl); |
| 347 | } |
| 348 | } |
| 349 | // radical + désinence |
| 350 | for (int i = 0; i <= f.length(); ++i) |
| 351 | { |
| 352 | QString r = f.left(i); |
| 353 | QString d = f.mid(i); |
| 354 | QList<Desinence *> ldes = _desinences.values(d); |
| 355 | if (ldes.empty()) continue; |
| 356 | // Je regarde d'abord si d est une désinence possible, |
| 357 | // car il y a moins de désinences que de radicaux. |
| 358 | // Je fais la recherche sur les radicaux seulement si la désinence existe. |
| 359 | QList<Radical *> lrad = _radicaux.values(r); |
| 360 | // ii noté ī |
| 361 | // 1. Patauium, gén. Pataui : Patau.i -> Patau+i.i |
| 362 | // 2. conubium, ablP conubis : conubi.s -> conubi.i+s |
| 363 | if (d.startsWith('i') && !d.startsWith("ii") && !r.endsWith('i')) |
| 364 | lrad << _radicaux.values(r + "i"); |
| 365 | if (lrad.empty()) continue; |
| 366 | // Il n'y a rien à faire si le radical n'existe pas. |
| 367 | foreach (Radical *rad, lrad) |
| 368 | { |
| 369 | Lemme *l = rad->lemme(); |
| 370 | foreach (Desinence *des, ldes) |
| 371 | { |
| 372 | if (des->modele() == l->modele() && |
| 373 | des->numRad() == rad->numRad() && |
| 374 | !l->estIrregExcl(des->morphoNum())) |
| 375 | { |
| 376 | bool c = ((cnt_v==0) |
| 377 | ||(cnt_v == rad->grq().toLower().count("v") |
| 378 | +des->grq().count("v"))); |
| 379 | if (!c) c = (V_maj && (rad->gr()[0] == 'U') |
| 380 | && (cnt_v - 1 == rad->grq().toLower().count("v"))); |
| 381 | c = c && ((cnt_oe==0)||(cnt_oe == rad->grq().toLower().count("ōe"))); |
| 382 | c = c && ((cnt_ae==0)||(cnt_ae == rad->grq().toLower().count("āe") |
| 383 | +des->grq().count("āe"))); |
| 384 | if (c) |
| 385 | { |
| 386 | QString fq = rad->grq() + des->grq(); |
| 387 | if (!r.endsWith("i") && rad->gr().endsWith("i")) |
| 388 | fq = rad->grq().left(rad->grq().size()-1) + "ī" |
| 389 | + des->grq().right(des->grq().size()-1); |
| 390 | int numM; |
| 391 | if (l->modele()->gr() == "inv") numM = 416; |
| 392 | else numM = des->morphoNum(); |
| 393 | QString morphosl = morpho(numM); |
| 394 | if (l->pos() == "n") morphosl.append(" "+l->genre()); |
| 395 | SLem sl = {fq, l, morphosl, "", l->modele()->pos().toLatin1()}; |
| 396 | result[l].prepend(sl); |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | } |
| 401 | } |
| 402 | if (_extLoaded && !_extension && !result.isEmpty()) |
| 403 | { |
| 404 | // L'extension est chargée mais je ne veux voir |
| 405 | // les solutions qui en viennent que si toutes en viennent. |
| 406 | MapLem res; |
| 407 | foreach (Lemme *l, result.keys()) |
| 408 | { |
| 409 | if (l->origin() == 0) |
| 410 | res[l] = result[l]; |
| 411 | } |
| 412 | |
| 413 | if (!res.isEmpty()) result = res; |
| 414 | } |
| 415 | return result; |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * \fn MapLem Lemmat::lemmatiseM (QString f, bool debPhr) |
| 420 | * \brief Renvoie dans une MapLem les lemmatisations de la |
| 421 | * forme f. le paramètre debPhr à true indique qu'il |
| 422 | * s'agit d'un début de phrase, et la fonction |
| 423 | * peut tenir compte des majuscules pour savoir |
| 424 | * s'il s'agit d'un nom propre. |
| 425 | */ |
| 426 | MapLem Lemmat::lemmatiseM(QString f, bool debPhr) |
| 427 | { |
| 428 | QString res; |
| 429 | QTextStream fl(&res); |
| 430 | // chiffres romains |
| 431 | MapLem mm = lemmatise(f); |
| 432 | if (estRomain(f)) |
| 433 | { |
| 434 | QString lin = QString("%1|inv|||num.|1").arg(f); |
| 435 | Lemme *romain = new Lemme(lin, 0, this); |
| 436 | int nr = aRomano(f); |
| 437 | romain->ajTrad(QString("%1").arg(nr), "fr"); |
| 438 | SLem sl = {f, 0, "inv","",'m'}; |
| 439 | QList<SLem> lsl; |
| 440 | lsl.append(sl); |
| 441 | mm.insert(romain, lsl); |
| 442 | } |
| 443 | if (f.isEmpty()) return mm; |
| 444 | // suffixes |
| 445 | foreach (QString suf, suffixes.keys()) |
| 446 | if (mm.empty() && f.endsWith(suf)) |
| 447 | { |
| 448 | QString sf = f; |
| 449 | sf.chop(suf.length()); |
| 450 | // TODO : aequeque est la seule occurrence |
| 451 | // de -queque dans le corpus classique |
| 452 | mm = lemmatiseM(sf, debPhr); |
| 453 | bool sst = false; |
| 454 | if (mm.isEmpty() && (suf == "st")) |
| 455 | { |
| 456 | sf += "s"; |
| 457 | mm = lemmatiseM(sf, debPhr); |
| 458 | sst = true; |
| 459 | } |
| 460 | foreach (Lemme *l, mm.keys()) |
| 461 | { |
| 462 | QList<SLem> ls = mm.value(l); |
| 463 | for (int i = 0; i < ls.count(); ++i) |
| 464 | if (sst) mm[l][i].sufq = "t"; |
| 465 | else mm[l][i].sufq += suffixes.value(suf); // Pour modoquest |
| 466 | } |
| 467 | } |
| 468 | if (debPhr && f.at(0).isUpper()) |
| 469 | { |
| 470 | QString nf = f.toLower(); |
| 471 | MapLem nmm = lemmatiseM(nf); |
| 472 | foreach (Lemme *nl, nmm.keys()) |
| 473 | mm.insert(nl, nmm.value(nl)); |
| 474 | } |
| 475 | // assimilations |
| 476 | if (mm.empty()) |
| 477 | { |
| 478 | QString fa = assim(f); |
| 479 | if (fa != f) |
| 480 | { |
| 481 | MapLem nmm = lemmatiseM(fa); |
| 482 | // désassimiler les résultats |
| 483 | foreach (Lemme *nl, nmm.keys()) |
| 484 | { |
| 485 | for (int i = 0; i < nmm[nl].count(); ++i) |
| 486 | nmm[nl][i].grq = desassimq(nmm[nl][i].grq); |
| 487 | mm.insert(nl, nmm.value(nl)); |
| 488 | } |
| 489 | } |
| 490 | } |
| 491 | if (mm.empty()) |
| 492 | { |
| 493 | QString fa = desassim(f); |
| 494 | if (fa != f) |
| 495 | { |
| 496 | MapLem nmm = lemmatise(fa); |
| 497 | foreach (Lemme *nl, nmm.keys()) |
| 498 | { |
| 499 | for (int i = 0; i < nmm[nl].count(); ++i) |
| 500 | nmm[nl][i].grq = assimq(nmm[nl][i].grq); |
| 501 | mm.insert(nl, nmm.value(nl)); |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | // contractions |
| 506 | QString fd = f; |
| 507 | foreach (QString cle, _contractions.keys()) |
| 508 | if (fd.endsWith(cle)) |
| 509 | { |
| 510 | fd.chop(cle.length()); |
| 511 | if ((fd.contains("v") || fd.contains("V"))) |
| 512 | fd.append(_contractions.value(cle)); |
| 513 | else |
| 514 | fd.append(Ch::deramise(_contractions.value(cle))); |
| 515 | MapLem nmm = lemmatise(fd); |
| 516 | foreach (Lemme *nl, nmm.keys()) |
| 517 | { |
| 518 | int diff = _contractions.value(cle).size() - cle.size(); |
| 519 | // nombre de lettres que je dois supprimer |
| 520 | for (int i = 0; i < nmm[nl].count(); ++i) |
| 521 | { |
| 522 | int position = f.size() - cle.size() + 1; |
| 523 | // position de la 1ère lettre à supprimer |
| 524 | if (fd.size() != nmm[nl][i].grq.size()) |
| 525 | { |
| 526 | // il y a une (ou des) voyelle(s) commune(s) |
| 527 | QString debut = nmm[nl][i].grq.left(position + 2); |
| 528 | position += debut.count("\u0306"); // Faut-il vérifier que je suis sur le "v". |
| 529 | } |
| 530 | nmm[nl][i].grq = nmm[nl][i].grq.remove(position, diff); |
| 531 | } |
| 532 | mm.insert(nl, nmm.value(nl)); |
| 533 | } |
| 534 | } |
| 535 | // majuscule initiale |
| 536 | if (mm.empty()) |
| 537 | { |
| 538 | f[0] = f.at(0).toUpper(); |
| 539 | MapLem nmm = lemmatise(f); |
| 540 | foreach (Lemme *nl, nmm.keys()) |
| 541 | mm.insert(nl, nmm.value(nl)); |
| 542 | } |
| 543 | return mm; |
| 544 | } |
| 545 | |
| 546 | /** |
| 547 | * \fn Lemme* Lemmat::lemme (QString l) |
| 548 | * \brief cherche dans la liste des lemmes le lemme |
| 549 | * dont la clé est l, et retourne le résultat. |
| 550 | */ |
| 551 | Lemme *Lemmat::lemme(QString l) |
| 552 | { |
| 553 | return _lemmes.value(l); |
| 554 | } |
| 555 | |
| 556 | QStringList Lemmat::lignesFichier(QString nf) |
| 557 | { |
| 558 | QFile f(nf); |
| 559 | f.open(QFile::ReadOnly); |
| 560 | QTextStream flux(&f); |
| 561 | flux.setCodec("UTF-8"); |
| 562 | QStringList retour; |
| 563 | while (!flux.atEnd()) |
| 564 | { |
| 565 | QString lin = flux.readLine(); |
| 566 | if ((!lin.isEmpty()) && ((!lin.startsWith("!")) || lin.startsWith("! --- "))) |
| 567 | retour.append(lin.simplified()); |
| 568 | } |
| 569 | f.close(); |
| 570 | return retour; |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * \fn void Lemmat::lisIrreguliers() |
| 575 | * \brief Chargement des formes irrégulières |
| 576 | * du fichier data/irregs.la |
| 577 | */ |
| 578 | void Lemmat::lisIrreguliers() |
| 579 | { |
| 580 | QStringList lignes = lignesFichier(_resDir + "irregs.la"); |
| 581 | foreach (QString lin, lignes) |
| 582 | { |
| 583 | if (lin.isEmpty()) continue; |
| 584 | Irreg *irr = new Irreg(lin, this); |
| 585 | if (irr != 0 && irr->lemme() != 0) |
| 586 | _irregs.insert(Ch::deramise(irr->gr()), irr); |
| 587 | #ifdef DEBOG |
| 588 | else |
| 589 | std::cerr << "Irréguliers, erreur dans la ligne" << qPrintable(lin); |
| 590 | #endif |
| 591 | } |
| 592 | // ajouter les irréguliers aux lemmes |
| 593 | foreach (Irreg *ir, _irregs) |
| 594 | ir->lemme()->ajIrreg(ir); |
| 595 | } |
| 596 | |
| 597 | /** |
| 598 | * \fn void Lemmat::lisFichierLexique(filepath) |
| 599 | * \brief Lecture des lemmes, synthèse et enregistrement |
| 600 | * de leurs radicaux |
| 601 | */ |
| 602 | void Lemmat::lisFichierLexique(QString filepath) |
| 603 | { |
| 604 | int orig = 0; |
| 605 | if (filepath.endsWith("ext.la")) orig = 1; |
| 606 | QStringList lignes = lignesFichier(filepath); |
| 607 | foreach (QString lin, lignes) |
| 608 | { |
| 609 | // contrôle de format |
| 610 | QStringList eclats = lin.split('|'); |
| 611 | if (eclats.count() < 6) |
| 612 | { |
| 613 | std::cerr << qPrintable ("ligne mal formée:"+lin); |
| 614 | continue; |
| 615 | } |
| 616 | Lemme *l = new Lemme(lin, orig, this); |
| 617 | _lemmes.insert(l->cle(), l); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | void Lemmat::lisLexicoSyntaxe() |
| 622 | { |
| 623 | QStringList lignes = lignesFichier(_resDir+"lexsynt.la"); |
| 624 | while (lignes.count() > 0) |
| 625 | { |
| 626 | QString lin = lignes.takeFirst(); |
| 627 | QStringList eclats = lin.split(':',QString::SkipEmptyParts); |
| 628 | if (eclats.count() > 1) |
| 629 | { |
| 630 | Lemme *l = lemme(eclats.at(0)); |
| 631 | if (l != 0) |
| 632 | { |
| 633 | QStringList lsynt = eclats.at(1).split(',',QString::SkipEmptyParts); |
| 634 | l->setLexSynt(lsynt); |
| 635 | } |
| 636 | } |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | /** |
| 641 | * \fn void Lemmat::lisLexique() |
| 642 | * \brief Lecture du fichier de lemmes de base |
| 643 | */ |
| 644 | void Lemmat::lisLexique() |
| 645 | { |
| 646 | Lemmat::lisFichierLexique(_resDir + "lemmes.la"); |
| 647 | } |
| 648 | |
| 649 | /** |
| 650 | * \fn void Lemmat::lisModeles() |
| 651 | * \brief Lecture des modèles, synthèse et enregistrement |
| 652 | * de leurs désinences |
| 653 | */ |
| 654 | void Lemmat::lisModeles() |
| 655 | { |
| 656 | QStringList lignes = lignesFichier(_resDir + "modeles.la"); |
| 657 | int max = lignes.count()-1; |
| 658 | QStringList sl; |
| 659 | for (int i=0;i<=max;++i) |
| 660 | { |
| 661 | QString l = lignes.at(i); |
| 662 | if (l.startsWith('$')) |
| 663 | { |
| 664 | _variables[l.section('=', 0, 0)] = l.section('=', 1, 1); |
| 665 | continue; |
| 666 | } |
| 667 | QStringList eclats = l.split(":"); |
| 668 | if ((eclats.at(0) == "modele" || i == max) && !sl.empty()) |
| 669 | { |
| 670 | Modele *m = new Modele(sl, this); |
| 671 | _modeles.insert(m->gr(), m); |
| 672 | sl.clear(); |
| 673 | } |
| 674 | sl.append(l); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | /** |
| 679 | * \fn void Lemmat::lisTraductions() |
| 680 | * \brief Lecture des fichiers de traductions |
| 681 | * trouvés dans data/, nommés lemmes, avec |
| 682 | * un suffixe corresponant à la langue cible |
| 683 | * qu'ils fournissent. |
| 684 | */ |
| 685 | void Lemmat::lisTraductions(bool base, bool extension) |
| 686 | { |
| 687 | QDir rep; |
| 688 | if (!base && !extension) return; |
| 689 | if (base && extension) { |
| 690 | rep = QDir(_resDir, "lem*.*"); |
| 691 | } else if (base) { |
| 692 | rep = QDir(_resDir, "lemmes.*"); |
| 693 | } else { |
| 694 | rep = QDir(_resDir, "lem_ext.*"); |
| 695 | } |
| 696 | QStringList ltr = rep.entryList(); |
| 697 | if (base) { |
| 698 | ltr.removeOne("lemmes.la"); // n'est pas un fichier de traductions |
| 699 | } |
| 700 | if (extension) { |
| 701 | ltr.removeOne("lem_ext.la"); // n'est pas un fichier de traductions |
| 702 | } |
| 703 | foreach (QString nfl, ltr) |
| 704 | { |
| 705 | // suffixe |
| 706 | QString suff = QFileInfo(nfl).suffix(); |
| 707 | QStringList lignes = lignesFichier(_resDir + nfl); |
| 708 | // lire le nom de la langue |
| 709 | QString lang = lignes.takeFirst(); |
| 710 | lang = lang.mid(1); |
| 711 | _cibles[suff] = lang; |
| 712 | foreach(QString lin, lignes) |
| 713 | { |
| 714 | Lemme *l = lemme(Ch::deramise(lin.section(':', 0, 0))); |
| 715 | if (l != 0) l->ajTrad(lin.section(':', 1), suff); |
| 716 | #ifdef DEBOG |
| 717 | else |
| 718 | std::cerr << "traduction, erreur dans la ligne" << lin |
| 719 | << "\n clé" << Ch::deramise(lin.section(':', 0, 0)); |
| 720 | #endif |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | |
| 725 | /** |
| 726 | * \fn Modele * Lemmat::modele (QString m) |
| 727 | * \brief Renvoie l'objet de la classe Modele dont le nom est m. |
| 728 | */ |
| 729 | Modele *Lemmat::modele(QString m) { return _modeles[m]; } |
| 730 | /** |
| 731 | * \fn QString Lemmat::morpho (int m) |
| 732 | * \brief Renvoie la chaîne de rang m dans la liste des morphologies |
| 733 | * donnée par le fichier data/morphos.la |
| 734 | */ |
| 735 | QString Lemmat::morpho(int m) |
| 736 | { |
| 737 | if (m < 0 || m >= _morphos.count()+1) return "morpho, erreur d'indice"; |
| 738 | //if (m == _morphos.count() - 1) return "-"; |
| 739 | return _morphos.at(m - 1); |
| 740 | } |
| 741 | |
| 742 | /** |
| 743 | * \fn void Lemmat::posCf() |
| 744 | * \brief récupère les entrées cf. pos des entrées renvoyées |
| 745 | */ |
| 746 | void Lemmat::posCf() |
| 747 | { |
| 748 | foreach (Lemme *l, _lemmes) |
| 749 | { |
| 750 | if (!l->renvoi().isEmpty()) |
| 751 | { |
| 752 | Lemme *lr = lemme(l->renvoi()); |
| 753 | if (lr != 0) |
| 754 | { |
| 755 | l->setPos(lr->pos()); |
| 756 | l->ajTrad(lr->traduction("fr"), "fr"); |
| 757 | } |
| 758 | } |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | QString Lemmat::posString(QChar p) |
| 763 | { |
| 764 | return mapPos.key(p,"n."); |
| 765 | } |
| 766 | |
| 767 | /** |
| 768 | * \fn QString Lemmat::variable (QString v) |
| 769 | * \brief permet de remplacer la métavariable v |
| 770 | * par son contenu. Ces métavariables sont |
| 771 | * utilisées par le fichier modeles.la, pour |
| 772 | * éviter de répéter des suites de désinences. |
| 773 | * Elles sont repérées comme en PHP, par leur |
| 774 | * premier caractère $. |
| 775 | */ |
| 776 | QString Lemmat::variable(QString v) |
| 777 | { |
| 778 | return _variables[v]; |
| 779 | } |