Debian Codegraph @

src/ch.cpp

raw symbols SCIP
1 /* ch.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 ch.cpp
24 * \brief constantes et utilitaires de traitement
25 * des chaînes de caractères
26 *
27 */
28
29 #include <iostream>
30 #include <QApplication>
31 #include <QFile>
32 #include <QRegExp>
33
34 #include "ch.h"
35
36 #include <QDebug>
37
38 /**
39 * \fn Ch::allonge(QString *f)
40 * \brief modifie f pour que sa dernière voyelle
41 * devienne longue.
42 */
43 void Ch::allonge(QString *f)
44 {
45 if (f->isEmpty()) return;
46 int taille = f->size();
47 // Je sais que le morceau à attacher commence par une consonne.
48 if (consonnes.contains(f->at(taille - 1)) &&
49 !QString("\u0101e \u0101u \u0113u \u014de")
50 .contains(f->mid(taille - 3, 2).toLower()))
51 {
52 f->replace(QRegExp("[a\u0103]([" + consonnes + "])$"), "\u0101\\1");
53 f->replace(QRegExp("[e\u0115]([" + consonnes + "])$"), "\u0113\\1");
54 f->replace(QRegExp("[i\u012d]([" + consonnes + "])$"), "\u012b\\1");
55 f->replace(QRegExp("[o\u014F]([" + consonnes + "])$"), "\u014d\\1");
56 f->replace(QRegExp("[u\u016d]([" + consonnes + "])$"), "\u016b\\1");
57 f->replace(QRegExp("[y\u0233]([" + consonnes + "])$"), "\u045e\\1");
58 f->replace(QRegExp("[A\u0102]([" + consonnes + "])$"), "\u0100\\1");
59 f->replace(QRegExp("[E\u0114]([" + consonnes + "])$"), "\u0112\\1");
60 f->replace(QRegExp("[I\u012c]([" + consonnes + "])$"), "\u012a\\1");
61 f->replace(QRegExp("[O\u014e]([" + consonnes + "])$"), "\u014c\\1");
62 f->replace(QRegExp("[U\u016c]([" + consonnes + "])$"), "\u016a\\1");
63 f->replace(QRegExp("[Y\u0232]([" + consonnes + "])$"), "\u040e\\1");
64 }
65 }
66
67 /**
68 * \fn Ch:atone(QString a, bool bdc)
69 * \brief supprime tous les diacritiques de la chaîne a
70 * si bdc est à true, les diacritiques des majuscules
71 * sont également supprimés.
72 */
73 QString Ch::atone(QString a, bool bdc)
74 {
75 // Supprimer le combining breve à la fin du mot
76 // if (a.endsWith(0x0306)) a.chop(1);
77 // minuscules
78 a.replace(0x0101, 'a');
79 a.replace(0x0103, 'a'); // ā ă
80 a.replace(0x0113, 'e');
81 a.replace(0x0115, 'e'); // ē ĕ
82 a.replace(0x012b, 'i');
83 a.replace(0x012d, 'i'); // ī ĭ
84 a.replace(0x014d, 'o');
85 a.replace(0x014f, 'o'); // ō ŏ
86 a.replace(0x016b, 'u');
87 a.replace(0x016d, 'u'); // ū ŭ
88 a.replace(0x0233, 'y');
89 a.replace(0x045e, 'y'); // ȳ ў
90 if (!bdc)
91 {
92 // majuscule
93 a.replace(0x0100, 'A');
94 a.replace(0x0102, 'A'); // Ā Ă
95 a.replace(0x0112, 'E');
96 a.replace(0x0114, 'E'); // Ē Ĕ
97 a.replace(0x012a, 'I');
98 a.replace(0x012c, 'I'); // Ī Ĭ
99 a.replace(0x014c, 'O');
100 a.replace(0x014e, 'O'); // Ō Ŏ
101 a.replace(0x016a, 'U');
102 a.replace(0x016c, 'U'); // Ū Ŭ
103 a.replace(0x0232, 'Y');
104 a.replace(0x040e, 'Y'); // Ȳ Ў
105 }
106 // combining breve
107 a.remove(0x0306); //ō̆ etc.
108 return a;
109 }
110
111 QString Ch::chemin(QString f)
112 {
113 QStringList _chemins = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation);
114 if (f.at(0) != '/') f.prepend('/');
115 QString ret;
116 for (int i=_chemins.count()-1;i>-1;--i)
117 {
118 ret = _chemins.at(i);
119 ret.append("/"+f);
120 if (QFile::exists(ret))
121 return ret;
122 }
123 ret = qApp->applicationDirPath()+f;
124 return ret;
125 }
126
127 /**
128 * \fn Ch::deQuant(QString *c)
129 * \brief utilisée en cas d'élision.
130 * supprime la quantité de la voyelle finale de la chaine c
131 * lorsque cette voyelle est en fin de mot ou suivie d'un "m".
132 */
133 void Ch::deQuant(QString *c)
134 {
135 if (c->endsWith("\u0306"))
136 c->chop(1); // Supprimer le combining breve à la fin du mot
137 c->replace(QRegExp("[\u0101\u0103](m?)$"), "a\\1"); // ā ă
138 c->replace(QRegExp("[\u0113\u0115](m?)$"), "e\\1");
139 c->replace(QRegExp("[\u012b\u012d](m?)$"), "i\\1");
140 c->replace(QRegExp("[\u014d\u014f](m?)$"), "o\\1");
141 c->replace(QRegExp("[\u016b\u016d](m?)$"), "u\\1");
142 c->replace(QRegExp("[\u0232\u0233](m?)$"), "y\\1");
143 }
144
145 /**
146 * \fn QString Ch::deramise(QString r)
147 * \brief retourne une graphie non-ramiste
148 * de r, càd dont tous les j deviennent i,
149 * et tous les v deviennent u. Les V majuscules
150 * initiaux sont ignorés.
151 */
152 QString Ch::deramise(QString r)
153 {
154 r.replace('J', 'I');
155 r.replace('j', 'i');
156 r.replace('v', 'u');
157 r.replace("æ", "ae");
158 r.replace("Æ", "Ae");
159 r.replace("œ", "oe");
160 r.replace("Œ", "Oe");
161 r.replace(0x1ee5, 'u'); // ụ le u muet de suavis, suadeo, etc...
162 if (!r.startsWith('V')) r.replace ('V', 'U');
163 return r;
164 }
165
166 /**
167 * \fn Ch::elide(QString *mp)
168 * \brief met entre crochets la dernière syllabe de mp.
169 */
170 void Ch::elide(QString *mp)
171 {
172 int taille = mp->size();
173 if ((taille > 1) && ((mp->endsWith('m') || mp->endsWith("\u0101e")) ||
174 mp->endsWith("\u0306")) &&
175 voyelles.contains(mp->at(taille - 2)))
176 {
177 deQuant(mp);
178 mp->insert(taille - 2, '[');
179 mp->append(']');
180 }
181 else if (voyelles.contains(mp->at(taille - 1)) && *mp != "\u014d")
182 {
183 deQuant(mp);
184 mp->insert(taille - 1, '[');
185 mp->append(']');
186 }
187 }
188
189 QString Ch::jviu(QString ch)
190 {
191 ch.replace('J', 'I');
192 ch.replace('j', 'i');
193 ch.replace('v', 'u');
194 ch.replace('V', 'U');
195 return ch;
196 }
197
198 QString Ch::ote_diacritiques (QString k)
199 {
200 // minuscules
201 k.replace (0x0101, 'a'); k.replace (0x0103, 'a'); // ā ă
202 k.replace (0x0113, 'e'); k.replace (0x0115, 'e'); // ē ĕ
203 k.replace (0x012b, 'i'); k.replace (0x012d, 'i'); // ī ĭ
204 k.replace (0x014d, 'o'); k.replace (0x014f, 'o'); // ō ŏ
205 k.replace (0x016b, 'u'); k.replace (0x016d, 'u'); // ū ŭ
206 k.replace (0x0233, 'y'); k.replace (0x045e, 'y'); // ȳ ў
207 // majuscule
208 k.replace (0x0100, 'A'); k.replace (0x0102, 'A'); // Ā Ă
209 k.replace (0x0112, 'E'); k.replace (0x0114, 'E'); // Ē Ĕ
210 k.replace (0x012a, 'I'); k.replace (0x012c, 'I'); // Ī Ĭ
211 k.replace (0x014c, 'O'); k.replace (0x014e, 'O'); // Ō Ŏ
212 k.replace (0x016a, 'U'); k.replace (0x016c, 'U'); // Ū Ŭ
213 k.replace (0x0232, 'Y'); k.replace (0x040e, 'Y'); // Ȳ Ў
214 // combining breve
215 k.remove (0x0306);
216 return k;
217 }