Debian Codegraph @

src/mot.cpp

raw symbols SCIP
1 /* mot.cpp */
2 /* Copyright (C) 2015 Yves Ouvrard
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <mot.h>
18 #include <mainwindow.h>
19
20 #include <iostream>
21 #include <QDebug>
22 #include <QTextCodec>
23 #include <QTextStream>
24
25 /**
26 * \fn Mot::Mot(QString g)
27 * \brief Créateur de la classe Mot.
28 * d : position de la première lettre ;
29 * f : position de la dernière lettre ;
30 * r : rang dans la phrase (le premier mot a le rang 0) ;
31 * parent : la phrase.
32 */
33 // signetMot
34 Mot::Mot(QString g, int d, int f, int r, QObject *parent) : QObject(parent)
35 {
36 if (parent != 0)
37 _phrase = qobject_cast<Phrase*>(parent);
38 _gr = g;
39 _dm = d;
40 _fm = f;
41 _que = -1;
42 _rang = r;
43 _morphos.clear();
44 _reqLancees = false;
45 }
46
47 Mot::~Mot()
48 {
49 _morphos.clear();
50 while (_flechis.count() > 0)
51 {
52 MotFlechi* mf = _flechis.takeFirst();
53 delete mf;
54 }
55 }
56
57 void Mot::ajFlechi(MotFlechi* mf)
58 {
59 if (!_flechis.contains(mf))
60 _flechis.append(mf);
61 }
62
63 void Mot::annuleFlechi(int f)
64 {
65 MotFlechi* mf = _flechis.at(f);
66 for (int ir=0;ir<mf->nbReqSup();++ir)
67 mf->reqSup(ir)->annuleRequis(mf->morpho()+", fléchi rejeté");
68 for (int ir=0;ir<mf->nbReqSub();++ir)
69 mf->reqSub(ir)->annuleRequis(mf->morpho()+", fléchi rejeté");
70 mf->setRejete(true);
71 }
72
73 void Mot::annuleFlechi(MotFlechi* mf)
74 {
75 mf->setRejete(true);
76 }
77
78 void Mot::annuleLemme(Lemme* lem)
79 {
80 for (int i=0;i<_flechis.count();++i)
81 {
82 MotFlechi* mf = _flechis.at(i);
83 if (mf->lemme() == lem)
84 {
85 mf->setRejete(true);
86 }
87 }
88 }
89
90 void Mot::choixFlechi(MotFlechi* mf)
91 {
92 for (int i=0;i<_flechis.count();++i)
93 {
94 MotFlechi* f = _flechis.at(i);
95 if (f != mf)
96 {
97 f->setRejete(true);
98 }
99 else f->setValide(true);
100 }
101 }
102
103 void Mot::choixLemme(Lemme* l)
104 {
105 for (int i=0;i<_flechis.count();++i)
106 {
107 MotFlechi* mf = _flechis.at(i);
108 if (mf->lemme() != l)
109 {
110 mf->setRejete(true);
111 }
112 }
113 }
114
115 void Mot::choixSub(Requete* req)
116 {
117 // annuler toutes les autres super, sauf antécédent
118 MotFlechi* mf = req->sub();
119 if (mf==0)
120 {
121 return;
122 }
123 for (int i=0;i<mf->nbReqSup();++i)
124 {
125 Requete* r = mf->reqSup(i);
126 if (r->close() && r != req && r->id() != "antecedent")
127 r->annuleRequis("Lien concurrent choisi");
128 }
129 }
130
131 void Mot::choixSuper(Requete* req)
132 {
133 // annuler toutes les requêtes homonymes, sauf si req est multi
134 // TODO : tenir compte de l'antécédent ?
135 if (!req->multi())
136 {
137 // annuler toutes les requêtes homonymes, sauf si req est multi
138 MotFlechi* mf = req->super();
139 for (int i=0;i<mf->nbReqSub();++i)
140 {
141 Requete* r = mf->reqSub(i);
142 if (r->close() && r->aff() == req->aff())
143 r->annuleRequis("Lien concurrent choisi");
144 }
145 }
146 }
147
148 int Mot::debut()
149 {
150 return _dm;
151 }
152
153 bool Mot::estAu(QString t)
154 {
155 for (int i=0;i<_morphos.values().count();++i)
156 {
157 QList<SLem> lsl = _morphos.values().at(i);
158 for (int iv=0;iv<lsl.count();++iv)
159 if (lsl.at(iv).morpho.contains(t))
160 return true;
161 }
162 return false;
163
164 }
165
166 Lemme* Mot::estLemDe(QStringList lr)
167 {
168 if (lr.empty()) return 0;
169 for (int i=0;i<_morphos.keys().count();++i)
170 {
171 Lemme*l = _morphos.keys().at(i);
172 if (lr.contains(l->gr()))
173 return l;
174 }
175 return 0;
176 }
177
178 bool Mot::estLie()
179 {
180 for (int i=0;i<_phrase->nbRequetes();++i)
181 {
182 Requete* req = _phrase->requete(i);
183 if (req != 0 && req->close() && ((req->super()->mot() == this) || (req->sub()->mot() == this)))
184 return true;
185 }
186 return false;
187 }
188
189 Lemme* Mot::estPosDe(QString pr)
190 {
191 if (pr.isEmpty()) return 0;
192 for (int i=0;i<_morphos.keys().count();++i)
193 {
194 Lemme* l = _morphos.keys().at(i);
195 QString pm = l->pos();
196 for (int ip=0;ip<pm.size();++ip)
197 if (pr.contains(pm.at(ip)))
198 return l;
199 }
200 return 0;
201 }
202
203 bool Mot::estPrep()
204 {
205 for (int i=0;i<_flechis.count();++i)
206 {
207 Lemme* l = _flechis.at(i)->lemme();
208 if (l->synt("prepabl") || l->synt("prepacc"))
209 return true;
210 }
211 return false;
212 }
213
214 bool Mot::estRelatif()
215 {
216 for (int i=0;i<_flechis.count();++i)
217 {
218 MotFlechi* mf = _flechis.at(i);
219 if (mf->lemme()->cle() != "qui2") continue;
220 if (mf->nbCloses() > 0) return true;
221 }
222 return false;
223 }
224
225 bool Mot::estSommet()
226 {
227 for (int i=0;i<_flechis.count();++i)
228 {
229 MotFlechi* mf = _flechis.at(i);
230 // si le fléchi a un sub relatif, le mot n'est pas sommet.
231 if (mf->estSub() || _phrase->estVbRelative(mf)) return false;
232 }
233 return true;
234 }
235
236 bool Mot::estSubParAff(QString aff)
237 {
238 for (int i=0;i<_flechis.count();++i)
239 {
240 MotFlechi* ms = _flechis.at(i);
241 if (ms->estSubParAff(aff)) return true;
242 }
243 return false;
244 }
245
246 bool Mot::estSubParId(QString id)
247 {
248 for (int i=0;i<_flechis.count();++i)
249 {
250 MotFlechi* ms = _flechis.at(i);
251 if (ms->estSubParId(id)) return true;
252 }
253 return false;
254 }
255
256 bool Mot::estSuperParAff(QString aff)
257 {
258 for (int i=0;i<_flechis.count();++i)
259 {
260 MotFlechi* ms = _flechis.at(i);
261 if (ms->estSuperParAff(aff)) return true;
262 }
263 return false;
264 }
265
266 bool Mot::estSuperParId(QString id)
267 {
268 for (int i=0;i<_flechis.count();++i)
269 {
270 MotFlechi* ms = _flechis.at(i);
271 if (ms->estSuperParId(id)) return true;
272 }
273 return false;
274 }
275
276 int Mot::fin()
277 {
278 return _fm;
279 }
280
281 MotFlechi* Mot::flechi(int i)
282 {
283 return _flechis.at(i);
284 }
285
286 QList<MotFlechi*> Mot::flValides(Requete* rtest)
287 {
288 QList<MotFlechi*> lmf;
289 if (rtest != 0 && rtest->super()->mot() == this)
290 lmf.append(rtest->super());
291 for (int i=0;i<_flechis.count();++i)
292 {
293 MotFlechi* mf = _flechis.at(i);
294 if (mf->nbReqSupValides(rtest) > 0)
295 {
296 lmf.append(mf);
297 }
298 }
299 return lmf;
300 }
301
302 QString Mot::gr()
303 {
304 return _gr;
305 }
306
307 QString Mot::html()
308 {
309 if (_flechis.empty())
310 return "mot non reconnu <a href=\"m.a."
311 + QString::number(_rang)
312 + "\">ajouter une entrée</a>";
313 QStringList ret;
314 for (int i=0;i<_flechis.count();++i)
315 {
316 QString lin;
317 QTextStream fl(&lin);
318 MotFlechi* mf = _flechis.at(i);
319 if (mf->rejete()) continue;
320 fl << "<span style=\"color:green;font-style:italic\"> "
321 << mf->morphoHtml() << "</span>";
322 // déterminants pour les noms, sujets pour les verbes
323 switch(mf->pos())
324 {
325 case 'n': fl << " <a href=\"m.d."<<i<<"\">det.</a> "; break;
326 case 'w':
327 case 'v': if (mf->morpho().contains('3'))
328 fl << " <a href=\"m.s."<<i<<"\">suj.</a> ";
329 break;
330 default:break;
331 }
332 fl << "tr. <span style=\"color:darkred;font-style:italic\">"<<mf->trdet()<<"</span> ";
333 if (mf->nbTr() > 1)
334 {
335 fl << "<a href=\"m.t."<<i<<"\">traductions</a> ";
336 }
337
338
339 fl << "<a href=\"m.e."<<i<<"\">&eacute;diter</a>&nbsp;"
340 << "<a href=\"m.c."<<i<<"\">choisir</a>&nbsp;rejeter&nbsp;"
341 << "<a href=\"m.r.m."<<i<<"\">le lemme</a>&nbsp;"
342 << "<a href=\"m.r.f."<<i<<"\">la forme</a>&nbsp;";
343 //<< "<a href=\"m.+."<<i<<"\">+sub</a>";
344 ret.append(lin);
345 }
346 if (lemmeUnique())
347 {
348 int numfl = 0;
349 for (int i=0;i<_flechis.count();++i)
350 {
351 if (_flechis.at(i)->valide())
352 numfl = i;
353 }
354 MotFlechi* mf = _flechis.at(numfl);
355 if (mf->nbTr() > 1)
356 {
357 for (int j=0;j<mf->nbTr();++j)
358 if (j != mf->itr())
359 {
360 QString lin;
361 QTextStream fl(&lin);
362 fl << "<a href=\"m.i."<<numfl<<"."<<j<<"\">"+mf->trfl(j)+"</a>";
363 ret << lin;
364 }
365 }
366 QString lin = "<a href=\"m.e."+QString::number(numfl)+"\">&eacute;diter</a>";
367 ret << lin;
368 }
369 return ret.join("<br/>\n");
370 }
371
372 int Mot::indexOf(MotFlechi* mf)
373 {
374 for (int i=0;i<_flechis.count();++i)
375 if (_flechis.at(i) == mf)
376 return i;
377 return -1;
378 }
379
380 void Mot::lance()
381 {
382 if (_reqLancees) return;
383 for (int i=0;i<_flechis.count();++i)
384 {
385 MotFlechi* mf = _flechis.at(i);
386 if (!mf->rejete()) mf->lance();
387 }
388 _reqLancees = true;
389 }
390
391 bool Mot::lemmeUnique()
392 {
393 // permet, en cas de retour true,
394 // d'afficher les choix de traduction
395 // du seul lemme [valide]
396 for (int i=0;i<_flechis.count();++i)
397 {
398 MotFlechi* mf = _flechis.at(i);
399 if (mf->valide()) return true;
400 }
401 return _morphos.keys().count() == 1;
402 }
403
404 QList<Requete*> Mot::lReqSupCloses()
405 {
406 QList<Requete*> liste;
407 for (int i=0;i<_phrase->nbRequetes();++i)
408 {
409 Requete* req = _phrase->requete(i);
410 if (req->close() && req->super()->mot() == this)
411 liste.append(req);
412 }
413 return liste;
414 }
415
416 bool Mot::monomorphe()
417 {
418 return _morphos.values().count() == 1;
419 }
420
421 MapLem Mot::morphos()
422 {
423 return _morphos;
424 }
425
426 int Mot::nbFlechis()
427 {
428 return _flechis.count();
429 }
430
431 int Mot::nbSupers()
432 {
433 int ret = 0;
434 for (int i=0;i<_flechis.count();++i)
435 {
436 MotFlechi* mf = _flechis.at(i);
437 for (int ir=0;ir<mf->nbReqSup();++ir)
438 if (mf->reqSup(ir)->close()) ++ret;
439 }
440 return ret;
441 }
442
443 bool Mot::nomAdj()
444 {
445 bool n = false;
446 bool a = false;
447 for (int i=0;i<_morphos.keys().count();++i)
448 {
449 Lemme* l = _morphos.keys().at(i);
450 a = a || l->pos().contains('a');
451 n = n || l->pos().contains('n');
452 if (a && n) return true;
453 }
454 return false;
455 }
456
457 Phrase* Mot::phrase()
458 {
459 return _phrase;
460 }
461
462 QString Mot::pos()
463 {
464 QString ret;
465 for (int i=0;i<_morphos.keys().count();++i)
466 ret.append(_morphos.keys().at(i)->pos());
467 return ret;
468 }
469
470 MotFlechi* Mot::princeps()
471 {
472 int max = 0;
473 MotFlechi* maxm = 0;
474 for (int i=0;i<_flechis.count();++i)
475 {
476 MotFlechi* mf = _flechis.at(i);
477 int n = mf->nbCloses();
478 if (n > max)
479 {
480 max = n;
481 maxm = mf;
482 }
483 }
484 if (max > 3) return maxm;
485 return 0;
486 }
487
488 bool Mot::que()
489 {
490 if (_que > 0) return true;
491 if (_gr.endsWith("que"))
492 {
493 _que = 1;
494 foreach (Lemme *l, _morphos.keys())
495 if (l->gr().endsWith("que"))
496 _que = 0;
497 }
498 else _que = 0;
499 return _que > 0;
500 }
501
502 int Mot::rang()
503 {
504 return _rang;
505 }
506
507 /**
508 * void Mot::reinit()
509 * repêche tous les fléchis rejetées
510 */
511 void Mot::reinit()
512 {
513 for (int i=0;i<_flechis.count();++i)
514 {
515 _flechis.at(i)->setRejete(false);
516 }
517 }
518
519 bool Mot::reqLancees()
520 {
521 return _reqLancees;
522 }
523
524 Requete* Mot::reqSub(QString id)
525 {
526 for (int i=0;i<_flechis.count();++i)
527 {
528 MotFlechi* mf = _flechis.at(i);
529 for (int j=0;j<mf->nbReqSub();++j)
530 {
531 Requete* req = mf->reqSub(j);
532 if (req->close() && req->id() == id) return req;
533 }
534 }
535 return 0;
536 }
537
538 QList<Requete*> Mot::reqSuper()
539 {
540 QList<Requete*> ret;
541 for (int i=0;i<_flechis.count();++i)
542 {
543 MotFlechi* mf = _flechis.at(i);
544 for (int j=0;j<mf->nbReqSup();++j)
545 {
546 Requete* req = mf->reqSup(j);
547 if (req->close()) ret.append(req);
548 }
549 }
550 return ret;
551 }
552
553 void Mot::rmFlechi(MotFlechi* mf)
554 {
555 if (mf == 0 || !_flechis.contains(mf)) return;
556 mf->setRejete(true);
557 }
558
559 void Mot::setFlechis(MapLem m)
560 {
561 _morphos = m;
562 // calcul de tous les fléchis de mc
563 for (int il=0;il<m.keys().count();++il)
564 {
565 Lemme *l = m.keys().at(il);
566 QString pl = l->pos();
567 for (int im=0;im<m[l].count();++im)
568 {
569 // pour les lemmes à plusieurs pos
570 for (int ip=0;ip<pl.count();++ip)
571 {
572 QString morpho = m[l].at(im).morpho;
573 char p = pl.at(ip).toLatin1();
574 MotFlechi* mf = new MotFlechi(l, p, morpho, this);
575 ajFlechi(mf);
576 // équivalents des fléchis
577 QString eqiv = mf->eqiv();
578 if (!eqiv.isEmpty()) for (int ie=0;ie<eqiv.length();++ie)
579 {
580 MotFlechi* nmf = new MotFlechi(l, eqiv.at(ie).toLatin1(), morpho, this, mf->posO());
581 ajFlechi(nmf);
582 }
583 }
584 }
585 }
586 // trier les fléchis
587 qSort(_flechis.begin(), _flechis.end(), sortF);
588 }
589
590 //MotFlechi::MotFlechi(Lemme* l, char p, QString m, Mot* parent)
591
592 void Mot::setReqLancees(bool r)
593 {
594 _reqLancees = r;
595 }
596
597 void Mot::setTr(QString t)
598 {
599 _tr = t;
600 }
601
602 bool Mot::sortF(MotFlechi* mfa, MotFlechi* mfb)
603 {
604 if (mfa->lemme() != mfb->lemme())
605 {
606 return mfa->lemme()->freq() > mfb->lemme()->freq();
607 }
608 QStringList k = QStringList()
609 << "nominatif"<<"accusatif"<<"ablatif"
610 <<"datif"<<"génitif"<<"vocatif";
611 QString ma = mfa->morpho();
612 QString mb = mfb->morpho();
613 for (int i=0;i<k.count();++i)
614 {
615 if (ma.contains(k.at(i)))
616 return true;
617 else if (mb.contains(k.at(i)))
618 return false;
619 }
620 return true;
621 }
622
623 MotFlechi* Mot::super()
624 {
625 for (int i=0;i<_flechis.count();++i)
626 {
627 MotFlechi* mf = _flechis.at(i);
628 for (int ir=0;ir<mf->nbReqSub();++ir)
629 {
630 Requete* r = mf->reqSub(ir);
631 if (r->close()) // && r->id() != "antecedent")
632 return r->super();
633 }
634 }
635 return 0;
636 }
637
638 QString Mot::trGroupe(Requete* rtest)
639 {
640 QList<MotFlechi*> lmf = flValides(rtest);
641 if (!lmf.isEmpty())
642 {
643 QStringList lret;
644 if (lmf.count() > 1) lret.append( _gr+" : Erreur, plusieurs fléchis valides");
645 for (int i=0;i<lmf.count();++i)
646 {
647 lret.append(lmf.at(i)->trGroupe());
648 }
649 return lret.join("\n");
650 }
651 return trs();
652 }
653
654 QString Mot::trs()
655 {
656 QStringList ret;
657 for (int i=0;i<_flechis.count();++i)
658 {
659 MotFlechi* mf = _flechis.at(i);
660 if (!mf->rejete()) ret.append(mf->trGroupe());
661 }
662 ret.removeDuplicates();
663 return ret.join(" / ");
664 }