| 1 | /* semantique.cpp*/ |
| 2 | /* |
| 3 | * This file is part of PRAELECTOR. |
| 4 | * |
| 5 | * PRAELECTOR 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 PRAELECTOR; 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 - 2019 |
| 20 | */ |
| 21 | |
| 22 | /* |
| 23 | TODO |
| 24 | - recherche d'hyperonyme avant l'insertion d'un lien. |
| 25 | https://fr.wikipedia.org/wiki/Hyperonymie |
| 26 | - chercher le même lien chez tous les autres sèmes. |
| 27 | - chercher le même id de liensynt, avec un super hyper ou |
| 28 | hyponyme du même lien. |
| 29 | - il faudrait formuler les questions "peut-on dire <hyper> <op> <hypo> ? |
| 30 | donc : chercher récursivement si l'hyperonyme peut... |
| 31 | |
| 32 | |
| 33 | - voir aussi l'existence chez les autres sèmes de liens ==, et demander |
| 34 | s'il y a hyper- ou hyponymie. |
| 35 | |
| 36 | */ |
| 37 | |
| 38 | #include <iostream> |
| 39 | #include <QFile> |
| 40 | #include <QInputDialog> |
| 41 | #include <QMessageBox> |
| 42 | #include <QTextStream> |
| 43 | |
| 44 | #include <ch.h> |
| 45 | #include <semantique.h> |
| 46 | |
| 47 | #include <QDebug> |
| 48 | |
| 49 | using namespace std; |
| 50 | |
| 51 | |
| 52 | |
| 53 | /*********************/ |
| 54 | /* classe Lien */ |
| 55 | /*********************/ |
| 56 | |
| 57 | Lien::Lien(QString idl, Seme* s) |
| 58 | { |
| 59 | _sup = s; |
| 60 | _id = idl; |
| 61 | } |
| 62 | |
| 63 | bool operator==(Lien& la, Lien& lb) |
| 64 | { |
| 65 | return la.toString() == lb.toString(); |
| 66 | } |
| 67 | |
| 68 | bool Lien::hypo() |
| 69 | { |
| 70 | return _id.at(0) == '@'; |
| 71 | } |
| 72 | |
| 73 | QString Lien::id() |
| 74 | { |
| 75 | return _id; |
| 76 | } |
| 77 | |
| 78 | Seme* Lien::sup() |
| 79 | { |
| 80 | return _sup; |
| 81 | } |
| 82 | |
| 83 | QString Lien::toString() |
| 84 | { |
| 85 | return QString(" %1;%2") |
| 86 | .arg(_id) |
| 87 | .arg(_sup->cle()); |
| 88 | } |
| 89 | |
| 90 | /*********************/ |
| 91 | /* classe Seme */ |
| 92 | /*********************/ |
| 93 | |
| 94 | // Un Seme est l'aspect sémantique d'un Lemme |
| 95 | // Il garde la mémoire des liens syntaxique dont il est le sub |
| 96 | // et celle des relations ontologiques transitives dont il est |
| 97 | // aussi le sub : est_un, est_dans, |
| 98 | // TODO On peut imaginer aussi les relations symétriques |
| 99 | |
| 100 | Seme::Seme(QString c, QObject* parent) : QObject(parent) |
| 101 | { |
| 102 | _rete = qobject_cast<Semantique*>(parent); |
| 103 | _cle = c; |
| 104 | } |
| 105 | |
| 106 | bool operator==(Seme& sa, Seme& sb) |
| 107 | { |
| 108 | return sa.cle() == sb.cle(); |
| 109 | } |
| 110 | |
| 111 | bool Seme::aLien(Lien* l) |
| 112 | { |
| 113 | for (int i=0;i<_lSynt.count();++i) |
| 114 | { |
| 115 | Lien* li = _lSynt.at(i); |
| 116 | if (*li == *l) return true; |
| 117 | } |
| 118 | // un hyperonyme a peut-être le lien |
| 119 | for (int i=0;i<_lOnto.count();++i) |
| 120 | { |
| 121 | Lien* li = _lOnto.at(i); |
| 122 | if (_rete->seme(li->sup()->cle())->aLien(l)) |
| 123 | return true; |
| 124 | } |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | QString Seme::cle() |
| 129 | { |
| 130 | return _cle; |
| 131 | } |
| 132 | |
| 133 | void Seme::ajLin(QString op, Seme* s) |
| 134 | { |
| 135 | Lien *nl = new Lien(op, s); |
| 136 | if (op.at(0) == '@') |
| 137 | _lOnto.append(nl); |
| 138 | else _lSynt.append(nl); |
| 139 | } |
| 140 | |
| 141 | bool Seme::hyponyme(Seme* s) |
| 142 | { |
| 143 | for (int i=0;i<_lOnto.count();++i) |
| 144 | { |
| 145 | Lien*l = _lOnto.at(i); |
| 146 | if (l->sup() == s || l->sup()->hyponyme(s)) |
| 147 | return true; |
| 148 | } |
| 149 | return false; |
| 150 | } |
| 151 | |
| 152 | int Seme::nbOnto() |
| 153 | { |
| 154 | return _lOnto.count(); |
| 155 | } |
| 156 | |
| 157 | Lien* Seme::onto(int i) |
| 158 | { |
| 159 | if (i >= _lOnto.count()) |
| 160 | { |
| 161 | cerr << "seme::onto, depassement"; |
| 162 | return 0; |
| 163 | } |
| 164 | return _lOnto.at(i); |
| 165 | } |
| 166 | |
| 167 | void Seme::rmLien(Lien* l) |
| 168 | { |
| 169 | if (l->hypo()) |
| 170 | { |
| 171 | int i = 0; |
| 172 | while (i < _lOnto.count()) |
| 173 | { |
| 174 | Lien* li = _lOnto.at(i); |
| 175 | if (*li == *l) _lOnto.removeAt(i); |
| 176 | else ++i; |
| 177 | } |
| 178 | } |
| 179 | else |
| 180 | { |
| 181 | int i = 0; |
| 182 | while (i < _lSynt.count()) |
| 183 | { |
| 184 | Lien* li = _lSynt.at(i); |
| 185 | if (*li == *l) _lSynt.removeAt(i); |
| 186 | else ++i; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | QString Seme::toString() |
| 192 | { |
| 193 | QStringList ret; |
| 194 | ret.append(_cle); |
| 195 | for (int i=0;i<_lSynt.count();++i) |
| 196 | { |
| 197 | ret.append(_lSynt.at(i)->toString()); |
| 198 | } |
| 199 | for (int i=0;i<_lOnto.count();++i) |
| 200 | { |
| 201 | ret.append(_lOnto.at(i)->toString()); |
| 202 | } |
| 203 | return ret.join("\n"); |
| 204 | } |
| 205 | |
| 206 | /***************************/ |
| 207 | /* classe Semantique */ |
| 208 | /***************************/ |
| 209 | |
| 210 | Semantique::Semantique() |
| 211 | { |
| 212 | QFile fp (Ch::chemin("/data/semantique.la")); |
| 213 | if (fp.open (QIODevice::ReadOnly|QIODevice::Text)) |
| 214 | { |
| 215 | QTextStream fluxD (&fp); |
| 216 | Seme* sem = 0; |
| 217 | while (!fluxD.atEnd ()) |
| 218 | { |
| 219 | QString ligne = fluxD.readLine(); |
| 220 | if (ligne.simplified().isEmpty()) continue; |
| 221 | if (ligne.at(0) == '!') continue; |
| 222 | if (ligne.at(0) == ' ' && sem != 0) |
| 223 | { |
| 224 | QStringList eclats = ligne.simplified().split(';'); |
| 225 | if (eclats.count() < 2) |
| 226 | { |
| 227 | cerr << qPrintable(ligne)<<" ligne mal formée\n"; |
| 228 | } |
| 229 | else sem->ajLin(eclats.at(0), seme(eclats.at(1))); |
| 230 | } |
| 231 | else sem = seme(ligne); |
| 232 | } |
| 233 | fp.close (); |
| 234 | } |
| 235 | //else cerr << "fichier semantique.fr introuvable"; |
| 236 | } |
| 237 | |
| 238 | void Semantique::ajLien(QString l) |
| 239 | { |
| 240 | QStringList eclats = l.split(';'); |
| 241 | if (eclats.count() < 3) |
| 242 | { |
| 243 | cerr << "ligne " << qPrintable(l) << " mal formée\n"; |
| 244 | return; |
| 245 | } |
| 246 | QString op = eclats.at(1); |
| 247 | // premier lemme |
| 248 | Seme* seme1 = 0; |
| 249 | if (_semes.contains(eclats.at(0))) |
| 250 | seme1 = _semes.value(eclats.at(0)); |
| 251 | else |
| 252 | { |
| 253 | seme1 = new Seme(eclats.at(0), this); |
| 254 | _semes.insert(seme1->cle(), seme1); |
| 255 | } |
| 256 | // peupler le seme |
| 257 | Seme* seme2 = 0; |
| 258 | if (_semes.contains(eclats.at(2))) |
| 259 | seme2 = _semes.value(eclats.at(2)); |
| 260 | else |
| 261 | { |
| 262 | seme2 = new Seme(eclats.at(2), this); |
| 263 | _semes.insert(seme2->cle(), seme2); |
| 264 | } |
| 265 | // si l'option d'enrichissement sémantique est acté |
| 266 | // créer un lien nl |
| 267 | Lien* nl = new Lien(op, seme2); |
| 268 | if (seme1->aLien(nl)) |
| 269 | { |
| 270 | // si le sème a déjà ce lien, abandonner |
| 271 | // TODO éventuellement, augmenter le poids du lien avant de quitter. |
| 272 | delete nl; |
| 273 | return; |
| 274 | } |
| 275 | // chercher d'autes sèmes ayant déjà le lien id;seme2 |
| 276 | // TODO à rendre récursif : en cas de réponse positive, |
| 277 | // essayer de remonter le lien aux hyperonymes |
| 278 | for (int i=0;i<_semes.count();++i) |
| 279 | { |
| 280 | Seme* s = seme(i); |
| 281 | if (s->aLien(nl)) |
| 282 | { |
| 283 | QString q("Peut-on dire que %1 est toujours un %2<br>" |
| 284 | "et que %2 peut être %3 de %4 ?"); |
| 285 | int r = QMessageBox::question(0,"",q |
| 286 | .arg(seme1->cle()) |
| 287 | .arg(s->cle()) |
| 288 | .arg(op) |
| 289 | .arg(seme2->cle()), |
| 290 | QMessageBox::Yes | QMessageBox::No); |
| 291 | if (r == QMessageBox::Yes) |
| 292 | { |
| 293 | // leme1 a s comme hyperonyme, et s supporte le lien. |
| 294 | // déclarer l'hyponymie |
| 295 | seme1->ajLin("@", seme2); |
| 296 | // supprimer le lien |
| 297 | seme1->rmLien(nl); |
| 298 | // ajouter le lien à s |
| 299 | ajLien(QString("%1;%2;%3") |
| 300 | .arg(s->cle()) |
| 301 | .arg(op) |
| 302 | .arg(seme2->cle())); |
| 303 | sauve(); |
| 304 | return; |
| 305 | } |
| 306 | r = QMessageBox::question(0,"",q |
| 307 | .arg(s->cle()) |
| 308 | .arg(seme1->cle()) |
| 309 | .arg(op) |
| 310 | .arg(seme2->cle()), |
| 311 | QMessageBox::Yes | QMessageBox::No); |
| 312 | if (r == QMessageBox::Yes) |
| 313 | { |
| 314 | // s a comme hyperonyme seme1 |
| 315 | // déclarer l'hyponymie |
| 316 | s->ajLin("@", seme1); |
| 317 | // supprimer le lien |
| 318 | s->rmLien(nl); |
| 319 | // ajouter le lien à seme1 |
| 320 | ajLien(QString("%1;%2;%3") |
| 321 | .arg(seme1->cle()) |
| 322 | .arg(op) |
| 323 | .arg(seme2->cle())); |
| 324 | sauve(); |
| 325 | return; |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | // aucune -nymie détectée, ajout simple du lien |
| 330 | seme1->ajLin(op, seme2); |
| 331 | sauve(); |
| 332 | } |
| 333 | |
| 334 | int Semantique::nbSemes() |
| 335 | { |
| 336 | return _semes.count(); |
| 337 | } |
| 338 | |
| 339 | QList<Seme*> Semantique::ontLien(Lien* l) |
| 340 | { |
| 341 | QList<Seme*> ret; |
| 342 | for (int i=0;i<_semes.count();++i) |
| 343 | { |
| 344 | Seme* s = seme(i); |
| 345 | if (s->aLien(l)) ret.append(s); |
| 346 | } |
| 347 | return ret; |
| 348 | } |
| 349 | |
| 350 | void Semantique::sauve() |
| 351 | { |
| 352 | QFile fp (Ch::chemin("/data/semantique.la")); |
| 353 | if (fp.open (QIODevice::WriteOnly|QIODevice::Text)) |
| 354 | { |
| 355 | QTextStream fluxD (&fp); |
| 356 | for (int i=0;i<_semes.values().count();++i) |
| 357 | { |
| 358 | fluxD << "\n"<<_semes.values().at(i)->toString(); |
| 359 | } |
| 360 | fluxD << "\n"; |
| 361 | } |
| 362 | fp.close(); |
| 363 | } |
| 364 | |
| 365 | Seme* Semantique::seme(QString s) |
| 366 | { |
| 367 | if (_semes.contains(s)) |
| 368 | return _semes.value(s); |
| 369 | else |
| 370 | { |
| 371 | Seme* seme = new Seme(s, this); |
| 372 | _semes.insert(seme->cle(), seme); |
| 373 | return seme; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | Seme* Semantique::seme(int i) |
| 378 | { |
| 379 | if (i >= _semes.count()) |
| 380 | { |
| 381 | cerr << "erreur, i > nb. de semes"; |
| 382 | return 0; |
| 383 | } |
| 384 | return _semes.values().at(i); |
| 385 | } |