File: syntaxe.cpp

package info (click to toggle)
collatinus 10.0-3
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 6,620 kB
  • sloc: cpp: 4,169; makefile: 18; sh: 2
file content (785 lines) | stat: -rw-r--r-- 18,669 bytes parent folder | download
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
/*
 *
 *  This file is part of COLLATINVS.
 *                                                                            
 *  COLLATINVS is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *                                                                            
 *  COLLATINVS 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 General Public License for more details.
 *                                                                            
 *  You should have received a copy of the GNU General Public License
 *  along with COLLATINVS; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#include "frequences.h"
#include "syntaxe.h"
#include <map>
#include <list>
//#include <QDebug>

using namespace std;

QStringList k = QStringList () << "n" << "v" << "ac" << "g" << "d" << "ab";
QStringList g = QStringList () << "m" << "f" << "nt";
QStringList n = QStringList () << "s" << "p";
QStringList d = QStringList () << "" << "comp" << "sup";
QStringList p = QStringList () << "1" << "2" << "3";
QStringList t = QStringList () << "pr" << "fut" << "impf" << "pf" << "fa" << "pqp";
QStringList m = QStringList () << "ind" << "subj" << "imper" << "inf" << "part" << "ger" << "adjv";
// in libcollatinus: "","ind.","coniunct.","imper.","infin.","part.","gerund.","adiect. verb."
QStringList v = QStringList () << "act" << "pass";

//***************
// CLASSE MORPHOS 
//***************

Morphos::Morphos (QString mo)
{
    // éclater en sous-chaînes
    if (!mo.isEmpty ()) mo = mo.simplified ();
    if (mo.isEmpty ())
    {
        graphie = "";
        return;
    }

    elements = mo.split (QRegExp ("\\W+"));
    QStringList eclats = elements;
    QStringList gr;
    QString el;
    foreach (el, eclats)
       if (k.contains (el)) 
          {
              gr << el;
              eclats.replace(eclats.indexOf (el), "");
          }
    foreach (el, eclats)
       if (g.contains (el))
          {
              gr << el;
              eclats.replace(eclats.indexOf (el), "");
          }
    foreach (el, eclats)
       if (n.contains (el))
          {
              gr << el;
              eclats.replace(eclats.indexOf (el), "");
          }
    foreach (el, eclats)
       if (d.contains (el)) 
          {
              gr << el;
              eclats.replace(eclats.indexOf (el), "");
          }
    foreach (el, eclats)
       if (p.contains (el)) 
          {
              gr << el;
              eclats.replace(eclats.indexOf (el), "");
          }
    foreach (el, eclats)
       if (t.contains (el)) 
          {
              gr << el;
              eclats.replace(eclats.indexOf (el), "");
          }
    foreach (el, eclats)
       if (m.contains (el))
          {
              gr << el;
              eclats.replace(eclats.indexOf (el), "");
          }
    foreach (el, eclats)
       if (v.contains (el)) 
          {
              gr << el;
              eclats.replace(eclats.indexOf (el), "");
          }
    graphie = gr.join (" ").simplified ();
}

void Morphos::setCanon (QString c)
{
    canon = c;
}

QString Morphos::getCanon ()
{
    return canon;
}

QString Morphos::getGraphie ()
{
    return graphie;
}

void Morphos::setItem (int i)
{
    item = i;
}

int Morphos::getItem ()
{
    return item;
}

/**
 * Morphos::accepte (Morphos * mb)
 * Vérifie que la morpho d'expression peut utiliser 
 * la morpho de phrase mb 
 */
bool Morphos::accepte (Morphos * mb)
{
    QString mbK = mb->getCanon ();
    if (!canon.isEmpty ())
    {
        if (canon.contains (','))
        {
            QStringList eclatsC = canon.split (',');
            if (!eclatsC.contains (mbK)) return false;
        }
        else
        {
            if (canon != mbK) return false;
        }

    }
    // qDebug () << "mbK:" << mbK << ":" << canon << ":";
    if (graphie.isEmpty ()) return true;
    QStringList eclatsExpr = graphie.split (" ");
    QStringList eclatsP = mb->getGraphie ().split (" ");
    foreach (QString ee, eclatsExpr)
    {
        // qDebug () << "ee :" << ee << ":";
        if (eclatsP.indexOf (ee) < 0)
        {
            // qDebug () << mb->getGraphie () << " refuse " << ee;
            return false;
        }
    }
    // qDebug () << "accepte"; 
    return true;
}

QString Morphos::cas ()
{
    foreach (QString el, elements)    
        if (k.contains (el)) return el;
    return "";
}

QString Morphos::genre ()
{
    foreach (QString el, elements)    
        if (g.contains (el)) return el;
    return "";
}

QString Morphos::nombre ()
{
    foreach (QString el, elements)    
        if (n.contains (el)) return el;
    return "";
}

QString Morphos::humain ()
{
    return QString ("%1.%2").arg (canon).arg (graphie);
}

// **********
// CLASSE MOT
// **********

Mot::Mot (QString g)
{
    // ramismes
    g.replace ("J","I");
    g.replace ("j","i");
    g.replace ("v","u");
    graphie = g;
    lemmatise ();
    adopte = false;
}

Mot::~Mot ()
{
    foreach (Morphos * morph, morphos)
       delete morph;
    morphos.clear ();
}

void Mot::lemmatise ()
{
    ListeAnalyses analyses = lanalyses (graphie.toStdWString ());
    ListeAnalyses adde;
    if (graphie.at (0).isUpper ())
    {
        adde = lanalyses (graphie.toLower ().toStdWString ());
        uniLanalyses (analyses, adde);
    }
	// ôter les éventuels suffxes
	QString sineSuff = graphie;
	sineSuff.remove (QRegExp ("(que|ue|ve|ne)$")); 
	if (sineSuff != graphie)
	{
		adde = lanalyses (sineSuff.toStdWString ());
		uniLanalyses (analyses, adde);
	}
	// enregistrer la lemmatisation
    QString mo;
    QTextStream flux (&mo);
    Morphos * morph;
	std::wstring ancienL, nouvoL; 
    // convertir le retour des analyses en instances de Morphos
	if (analyses.empty ())
		QTextStream (&lemme) << "\n" << graphie << " ?";
    for (std::multimap<std::wstring, AnalyseMorpho>::iterator it = analyses.begin ();
         it != analyses.end (); it++)
    {
        // cas
        if (it->second.casus () > 0) flux << k[it->second.casus ()-1] << " ";
        // genre
        if (it->second.genus () > 0) flux << g[it->second.genus ()-1] << " ";
        // nombre
        if (it->second.numerus () > 0) flux << n[it->second.numerus ()-1] << " ";
        // degré
        if (it->second.gradus () > 0) flux << d[it->second.gradus ()-1] << " ";
        // personne
        if (it->second.persona () > 0) flux << p[it->second.persona ()-1] << " ";
        // temps
        if (it->second.tempus () > 0) flux << t[it->second.tempus ()-1] << " ";
        // mode
        if (it->second.modus () > 0) flux << m[it->second.modus ()-1] << " ";
        // voix
        if (it->second.uox () > 0) flux << v[it->second.uox ()-1] << " ";
        
        mo = mo.simplified ();
        morph = new Morphos (mo); 
        morph->setCanon (QString::fromStdWString (it->second.lemma ()));
        morphos << morph;
        mo.clear ();
		nouvoL = entree (it->second.lemma ())->doc ();
		if (nouvoL != ancienL)
		    QTextStream (&lemme) << "\n" << QString::fromStdWString (nouvoL);
		ancienL = nouvoL;
    }
}

QString Mot::getGraphie ()
{
    return graphie;
}

int Mot::count_morphos ()
{
    return morphos.count ();
}

QString Mot::getCanon (int i)
{
    if (i <  morphos.size ())
        return morphos[i]->getCanon ();
    return "";
}

Morphos * Mot::getMorpho (int i)
{
    return morphos[i];
}

QString Mot::humain (int i)
{
    return morphos[i]->humain ();
}

QString Mot::humain ()
{
    QString retour = graphie ;
    QTextStream flux (&retour);
    foreach (Morphos * morph, morphos)
       flux << "\n    - " << morph->humain ();
    return retour;
}

QString Mot::get_lemme ()
{
	return lemme;
}

void Mot::setAdopte (bool a)
{
    adopte = a;
}

bool Mot::estAdopte ()
{
    return adopte;
}

bool Mot::casCommun (Mot * m)
{
    foreach (Morphos * maMorpho, morphos)
        for (int i = 0;i<m->count_morphos();i++)
            if (maMorpho->cas () == m->getMorpho (i)->cas ()) return true;
    return false;
}

bool Mot::nombreCommun (Mot * m)
{
    foreach (Morphos * maMorpho, morphos)
        for (int i = 0;i<m->count_morphos();i++)
            if (maMorpho->nombre () == m->getMorpho (i)->nombre ()) return true;
    return false;
}

// *****************
// CLASSE EXPRESSION
// *****************

Expression::Expression (QString n)
{
    QStringList eclats = n.split ('|');
    nom = eclats[0];
    doc = eclats[1];
    noyo = eclats[2].toInt ();
    int ie = 3;
    while (ie < eclats.size ())
    {
        QStringList liste = eclats[ie].split ('.');
        Morphos * m = new Morphos (liste[1]);
        m->setCanon (liste[0]); 
        if (!liste[2].isEmpty ()) 
        {
            de = ie;
            en = liste[2];
            avec = liste[3].toInt ();
        }
        else
        {
            avec = -1;
        }
        morphos.append (m);
        ie++;
    }
}

QString Expression::getNom ()
{
    return nom;
}

QString Expression::getDoc ()
{
    return doc;
}

QString Expression::getEn ()
{
    return en;
}

int Expression::countMorphos ()
{
    return morphos.size ();
}

Morphos * Expression::getMorpho (int im)
{
    if (im < morphos.size ())
        return morphos [im];
    return NULL;
}

QString Expression::humain ()
{
    QString retour;
    QTextStream (&retour) << nom << " : " << doc;
    return retour;
}
        
QString Expression::lemme_noyau ()
{
    return morphos[noyo]->getCanon ();
}

int Expression::getPosInP ()
{
    return posInP;
}

void Expression::setPosInP (int p)
{
    posInP = p;
}

bool Expression::accordVoulu ()
{
    return avec >= 0;
}

/*
 ************************
  classe Liste_expr
};
 ************************
*/

Liste_expr::Liste_expr (QString f)
{
    QFile file(f);
    if (!file.open(QFile::ReadOnly | QFile::Text)) 
    {
        //QMessageBox::warning(this, "Collatinus", "Capsam legere nequeo expressions.fr"); 
        return;
    }

    QTextStream in(&file);
    QString lin;
    lin = in.readLine ();
    //while (!file.atEnd ())
    while (!lin.isNull ())
    {
        Expression * E = new Expression (lin);
        // injection
        expressions.insert (E->lemme_noyau (), E); 
        lin = in.readLine ();
    }
}

Liste_expr::~Liste_expr ()
{

}

QList<Expression *> Liste_expr::expr_lemme (QString l)
{
    // qDebug () << "liste_expr (" << l << ")";
    return expressions.values (l);    
}

Liste_expr * LExpressions;

// *************
// CLASSE PHRASE
// *************

Phrase::Phrase (QString t)
{
    graphie = t.simplified ();
    Mot * nmot;
    QStringList l = graphie.split (QRegExp ("\\W+"));
    QString eclat;
    int j = 0;
    for (int i=0; i < l.count (); i++)
    {
        eclat = l[i];
        if (eclat.isEmpty ())
           continue;
        nmot = new Mot (eclat); 
        nmot->no = j;
        mots << nmot; 
        j++;
    }
    cherche_expressions ();
}

Phrase::~Phrase ()
{
    foreach (Mot * mot, mots)
        delete mot;
    mots.clear ();
}

bool Phrase::aiLeCanon (QString c)
{
    if (c.isEmpty ()) return true;
    if (c.contains (','))
    {
        QStringList lc = c.split (','); 
        foreach (QString cc, lc)
            foreach (Mot * mot, mots)
            {
                for (int i=0; i < mot->count_morphos (); i++)
                    if (mot->getCanon (i) == cc) 
                        return true;
            }
    }
    else
    {
        foreach (Mot * mot, mots)
            for (int i=0; i < mot->count_morphos (); i++)
                if (mot->getCanon (i) == c) return true;
    }
    return false;
}

// sert pour Expression::expressions ()
// évite les doublons dans la recherche d'expressions
bool contient (QList<Expression *> liste, Expression * expr)
{
    foreach (Expression * e, liste)
        if (e->getNom () == expr->getNom ()) return true;
    return false;
}

/**
 * Renvoie le premier mot de la phrase non adopté,
 * satisfaisant à la demande du membre d'expression m.
 * en profite pour marquer adopté le mot renvoyé.
 */
Mot * Phrase::motExpr (Morphos * m)
{
    foreach (Mot * imot, mots)
    {
        for (int i = 0;i<imot->count_morphos ();i++)
        {
            // qDebug () << imot->getMorpho (i)->humain ();
            if ((!imot->estAdopte ()) && m->accepte (imot->getMorpho (i)))
            {
                imot->setAdopte (true);
                return imot;
            }
        }
    }
    return NULL;
}

bool Phrase::accord (int de, QString en, int avec)
{
    Mot * motDe = mot_no (de);
    Mot * motAvec = mot_no (avec);
    if (en.contains ('n') && !motDe->nombreCommun (motAvec)) return false;
    if (en.contains ('c') && !motDe->casCommun (motAvec)) return false; 
    return true; 
}

void Phrase::cherche_expressions ()
{
    // pour chaque mot de la phrase
    foreach (Mot * mot, mots)
    {
       // pour chaque canon du mot 
       for (int i=0;i<mot->count_morphos ();i++)
       {
            // s'il n'est pas noyo potentiel, mot suivant
            QList<Expression *> le = LExpressions->expr_lemme (mot->getCanon (i));
            // qDebug () << mot->getCanon (i) << " " << le.isEmpty ();
            if (le.isEmpty ()) continue;
            // ici, possibilité de renvoyer toute expression utilisant le mot
            // pour chaque expression potentielle
            foreach (Expression * expr, le)
            {
                // qDebug () << expr->humain ();
                initAdoptes ();
                // pour chaque mot requis de l'expression
                bool esuiv = false;
                for (int im = 0; im < expr->countMorphos (); im++)
                {
                    // mot absent de la phrase, expression suivante
                    if (!aiLeCanon (expr->getMorpho (im)->getCanon ()))
                    {
                        esuiv = true;        
                        continue;
                    }
                }
                if (esuiv) continue;
                // ici, possibilité de renvoyer les expressions sans vérif de morpho
                for (int im = 0; im < expr->countMorphos (); im++)
                {
                    // si aucun des mots repérés n'a la bonne morpho, expr. suivante
                    // version 2012 : bogue ici.
                    Mot * mota = motExpr (expr->getMorpho (im));
                    if (mota == NULL) 
                    {
                        // qDebug () << "jaipa";
                        esuiv = true;
                        continue;
                    }
                    if (expr->accordVoulu ()) 
                    {
                        de = mot->no;
                        avec = mota->no;
                        en = expr->getEn ();
                    }
                }
                if (esuiv) continue;
                //vérifier l'accord
                if (avec >= 0 && !accord (de, en, avec)) continue;
                // ajouter le n° du mot + l'expression à la phrase
                if (!contient (liste_expressions, expr)) 
                {
                    expr->setPosInP (mot->no);
                    liste_expressions << expr;
                }
            }
        }
    }
}

QList<Expression *> Phrase::expressions ()
{
    return liste_expressions;
}

QString Phrase::analyse ()
{
   QString retour;
   QTextStream flux (&retour);
   QList<Expression *> liste = expressions ();
   foreach (Expression * e, liste)
      flux << e->getNom () << " : " << e->getDoc () << "\n"; 
   return retour;
}

Mot * Phrase::mot_no (int n)
{
	if (n < mots.size ()) return mots[n];
	return NULL;
}

Expression * Phrase::expression_no (int n)
{
	QString retour;
	foreach (Expression * expr, expressions ())
		if (expr->getPosInP () == n)
			return expr;
	return NULL;
}

QStringList * Phrase::analyse_et_lemmes (int minRaritas)
{
	QStringList * retour = new QStringList ();
	QString doc;
	Expression * e;
	//QTextStream ts (&retour);
	for (int i = 0; i < mots.count (); i++)
	{
		 //ts << "\n" <<  mots[i]->get_lemme ();	
         QString lemma (mots[i]->getCanon (0));
         //qDebug () << lemma << " " << frequentia (lemma);
         if (minRaritas <= frequentia (lemma))
         {
		     e = expression_no (i); 
		     if (e != NULL) 
                 retour->append  (mots[i]->get_lemme () + "\n   " + QString (0x00B6) + " " + e->getNom () + " : " + e->getDoc ());
             else retour->append (mots[i]->get_lemme ());
         }
	}
	return retour;
}

void Phrase::initAdoptes ()
{
    foreach (Mot * m, mots)
        m->setAdopte (false);
    de = -1;
    en = "";
    avec = -1;
}

// ************
// CLASSE TEXTE
// ************

Texte::Texte ()
{
}

Texte::~Texte ()
{
    foreach (Phrase * phr, phrases)
       delete phr;
    phrases.clear ();
}

void Texte::ajoute_phrase (Phrase * p)
{
    phrases << p;
}

void Texte::vide_phrases ()
{
    foreach (Phrase * phr, phrases)
        delete phr;
    phrases.clear ();
}

bool Texte::phrase_at_pos (int p)
{
    return phrase_pos (p) != NULL;
}

Phrase * Texte::phrase_pos (int p)
{
    foreach (Phrase * phr, phrases)
    if (p >= phr->debut && p <= phr->fin)
       return phr;
    return NULL;
}


/*
 ************************
  Fonctions hors classe
 ************************
*/

void lis_expr (QString ch)
{
    // QCoreApplication::dirPath ();
    //LExpressions = new Liste_expr ("expressions.fr");
    LExpressions = new Liste_expr (ch);
}

Texte * texte;

void cree_texte ()
{
    texte = new Texte ();
}

void vide_phrases ()
{
    if (texte == NULL) return;
    texte->vide_phrases ();
}

bool phrase_at_pos (int p)
{
    return texte->phrase_at_pos (p);    
}

void cree_phrase (QString p, int d, int f)
{
    Phrase * phr = new Phrase (p);
    phr->debut = d;
    phr->fin = f;
    texte->ajoute_phrase (phr);
}

// appel d'analyse syntaxique
QString analyse_syntaxique (int p, int motNo, QString &canon)
{
    QString retour;
    Phrase * phr = texte->phrase_pos (p);
    if (phr == NULL) return "";
    canon = phr->mot_no(motNo)->getCanon (0);
    foreach (Expression * expr, phr->expressions ())
    {
        if (expr->getPosInP () == motNo)
        {
            // retour précédé du signe unicode '|P' ou 00B6 (182) 
            QTextStream (&retour) << "\n   " << QString (0x00B6) << " " << expr->getNom () << " : " << expr->getDoc ();
        }

    }
    return retour;
}