Can get inflections of verb.
[mverbiste] / verbiste / misc-types.h
1 /*  $Id: misc-types.h,v 1.12 2011/01/26 02:03:28 sarrazip Exp $
2     misc-types.h - Miscellaneous types used by the dictionary class.
3
4     verbiste - French conjugation system
5     Copyright (C) 2003-2010 Pierre Sarrazin <http://sarrazip.com/>
6
7     This program is free software; you can redistribute it and/or
8     modify it under the terms of the GNU General Public License
9     as published by the Free Software Foundation; either version 2
10     of the License, or (at your option) any later version.
11
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20     02111-1307, USA.
21 */
22
23 #ifndef _H_misc_types
24 #define _H_misc_types
25
26 #include <verbiste/c-api.h>
27
28 #include <assert.h>
29 #include <vector>
30 #include <string>
31 #include <map>
32 #include <set>
33
34
35 /**
36     Valid modes.
37 */
38 enum Mode
39 {
40     INVALID_MODE = VERBISTE_INVALID_MODE,
41     INFINITIVE_MODE = VERBISTE_INFINITIVE_MODE,
42     INDICATIVE_MODE = VERBISTE_INDICATIVE_MODE,
43     CONDITIONAL_MODE = VERBISTE_CONDITIONAL_MODE,
44     SUBJUNCTIVE_MODE = VERBISTE_SUBJUNCTIVE_MODE,
45     IMPERATIVE_MODE = VERBISTE_IMPERATIVE_MODE,
46     PARTICIPLE_MODE = VERBISTE_PARTICIPLE_MODE,
47     GERUND_MODE = VERBISTE_GERUND_MODE,
48
49     // Greek modes:
50     PRESENT_INDICATIVE = VERBISTE_PRESENT_INDICATIVE,
51     PRESENT_SUBJUNCTIVE = VERBISTE_PRESENT_SUBJUNCTIVE,
52     PRESENT_IMPERATIVE = VERBISTE_PRESENT_IMPERATIVE,
53     PRESENT_GERUND = VERBISTE_PRESENT_GERUND,
54     PAST_IMPERFECT_INDICATIVE = VERBISTE_PAST_IMPERFECT_INDICATIVE,
55     PAST_PERFECT_INDICATIVE = VERBISTE_PAST_PERFECT_INDICATIVE,
56     PAST_PERFECT_SUBJUNCTIVE = VERBISTE_PAST_PERFECT_SUBJUNCTIVE,
57     PAST_PERFECT_IMPERATIVE = VERBISTE_PAST_PERFECT_IMPERATIVE,
58     PAST_PERFECT_INFINITIVE = VERBISTE_PAST_PERFECT_INFINITIVE
59 };
60
61
62 /**
63     Valid tenses.
64 */
65 enum Tense
66 {
67     INVALID_TENSE = VERBISTE_INVALID_TENSE,
68     PRESENT_TENSE = VERBISTE_PRESENT_TENSE,
69     PAST_TENSE = VERBISTE_PAST_TENSE,
70     IMPERFECT_TENSE = VERBISTE_IMPERFECT_TENSE,
71     FUTURE_TENSE = VERBISTE_FUTURE_TENSE,
72
73     // Greek tenses:
74     ACTIVE_TENSE = VERBISTE_ACTIVE_TENSE,
75     PASSIVE_TENSE = VERBISTE_PASSIVE_TENSE,
76     IMPERATIVE_ACTIVE_TENSE = VERBISTE_IMPERATIVE_ACTIVE_TENSE,
77     IMPERATIVE_PASSIVE_TENSE = VERBISTE_IMPERATIVE_PASSIVE_TENSE,
78     PAST_PERFECT = VERBISTE_PAST_PERFECT
79 };
80
81
82 /**
83     Description of a verb inflection.
84     Gives the mode, tense and person of a conjugated verb.
85     The person is 1, 2 or 3 for je/nous, tu/vous, il/ils, except in
86     infinitive and participle mode, where it is always 0
87     since it does not apply.
88 */
89 class ModeTensePersonNumber
90 {
91 public:
92
93     /** Mode (infinitive, indicative, etc). */
94     Mode mode;
95
96     /** Tense (present, past, etc). */
97     Tense tense;
98
99     /** Person (1, 2 or 3, or 0 in infinitive mode).
100         In participle mode, 1..4 means masc sing, masc plur,
101         fem sing, fem plur.
102     */
103     unsigned char person;
104
105     /** Number (true for plural, false for singular). */
106     bool plural;
107
108     /** Indicates if this termination is correct or an error. */
109     bool correct;
110
111
112     ModeTensePersonNumber(Mode m = INVALID_MODE,
113                     Tense t = INVALID_TENSE,
114                     unsigned char pers = 0,
115                     bool plur = false,
116                     bool isCorrect = true)
117       : mode(m), tense(t), person(pers), plural(plur), correct(isCorrect)
118     {
119         assert(mode <= PARTICIPLE_MODE);
120         assert(tense <= FUTURE_TENSE);
121         assert(person <= 3);
122     }
123
124
125     /** Constructs an object from English mode and tense names.
126         Calls the set() method.
127         @param    modeName      mode name known to method
128                                 FrenchVerbDictionary::convertModeName()
129         @param    tenseName     tense name known to method
130                                 FrenchVerbDictionary::convertTenseName()
131         @param    personNum     person "counter" (1--6, except for imperative
132                                 mode, where it must be 1--3; ignored for
133                                 infinitive and participle modes)
134         @param    isCorrect     indicates if this termination is correct
135                                 or an error
136     */
137     ModeTensePersonNumber(const char *modeName,
138                     const char *tenseName,
139                     int personNum,
140                     bool isCorrect,
141                     bool isItalian)
142       : mode(INVALID_MODE), tense(INVALID_TENSE), person(0), plural(false), correct(false)
143     {
144         set(modeName, tenseName, personNum, isCorrect, isItalian);
145     }
146
147
148     /** Initializes an object from English mode and tense names.
149         Call the set() method.
150         @param    modeName      mode name known to method
151                                 FrenchVerbDictionary::convertModeName()
152         @param    tenseName     tense name known to method
153                                 FrenchVerbDictionary::convertTenseName()
154         @param    personNum     person "counter" (1--6, except for imperative
155                                 mode, where it must be 1--3; ignored for
156                                 infinitive mode); for participle mode, must be
157                                 1..4 (masc sing, masc plur, fem sing, fem plur)
158         @param    isCorrect     indicates if this termination is correct
159                                 or an error
160         @param    isItalian     true for Italian, false for French
161     */
162     void set(const char *modeName, const char *tenseName, int personNum, bool isCorrect, bool isItalian);
163
164
165     /**
166         Dumps the fields of this object into those of the destination object.
167         @param    destination   object whose fields (mode, tense, person,
168                                 number) are assigned with values taken
169                                 from this object
170     */
171     void dump(Verbiste_ModeTensePersonNumber &destination) const;
172 };
173
174
175 struct InflectionSpec
176 {
177     std::string inflection;
178     bool isCorrect;
179
180     InflectionSpec(const std::string &inf, bool c) : inflection(inf), isCorrect(c) {}
181 };
182
183
184 /** List of inflections. */
185 typedef std::vector<InflectionSpec> PersonSpec;
186
187
188 /** List of persons (1, 3 or 6 persons depending on the mode and tense). */
189 typedef std::vector<PersonSpec> TenseSpec;
190
191
192 /**
193     Mode specification.
194     Contains tense specifications indexed by Tense values.
195 */
196 typedef std::map<Tense, TenseSpec> ModeSpec;
197
198
199 /**
200     Conjugation template specification.
201     Contains mode specifications indexed by Mode values.
202 */
203 typedef std::map<Mode, ModeSpec> TemplateSpec;
204
205
206 /**
207     Conjugation system for the known verbs of a language.
208     Contains conjugation templates indexed by template names.
209 */
210 typedef std::map<std::string, TemplateSpec> ConjugationSystem;
211
212
213 /**
214     Table of template names indexed by verb infinitive.
215     If the verb "abaisser" follows the "aim:er" conjugation template,
216     then a VerbTable would contain an entry where the key is "abaisser"
217     and the value is a set containing "aim:er".
218     An infinitive can be associated with more than one template.
219 */
220 typedef std::map< std::string, std::set<std::string> > VerbTable;
221
222
223 /**
224     Table that describes the mode, tense and person of a number of inflections.
225     For example, in the "aim:er" conjugation template, the inflection
226     (termination) "e" is associated with:
227
228     - the 1st person singular of the indicative present;
229
230     - the 3rd person singular of the indicative present;
231
232     - the 1st person singular of the subjunctive present;
233
234     - the 3rd person singular of the subjunctive present;
235
236     - the 2nd person singular of the imperative present.
237
238     Thus, in this table, the key "e" would be be associated with a vector
239     of five objects representing these five mode-tense-person combinations.
240 */
241 typedef std::map<std::string, std::vector<ModeTensePersonNumber> >
242                                             TemplateInflectionTable;
243
244
245 /**
246     Table of template inflection tables, indexed by template names.
247     For example, a key "aim:er" would be associated with a template
248     inflection table that gives all inflections (terminations) that are
249     accepted by the "aim:er" conjugation template (e.g., "e", "es",
250     "ons", etc).
251 */
252 typedef std::map<std::string, TemplateInflectionTable> InflectionTable;
253
254
255 /**
256     Description of a conjugated verb's inflection.
257 */
258 class InflectionDesc
259 {
260 public:
261
262     /** Infinitive form of the conjugated verb (UTF-8). */
263     std::string infinitive;
264
265     /** Conjugated template used by the verb (e.g. "aim:er") (UTF-8). */
266     std::string templateName;
267
268     /** Mode, tense, person and number of the inflection. */
269     ModeTensePersonNumber mtpn;
270
271     /**
272         Constructs an inflection description from optional arguments.
273     */
274     InflectionDesc(const std::string &inf = "",
275                 const std::string &tname = "",
276                 ModeTensePersonNumber m = ModeTensePersonNumber())
277       : infinitive(inf),
278         templateName(tname),
279         mtpn(m)
280     {
281     }
282 };
283
284
285 #endif  /* _H_misc_types */