Few more things
[apertium.git] / lttoolbox / lttoolbox / Expander.H
blob7dac745788e55d3d51790418f9504c17b7e4d867
1 /*
2  * Copyright (C) 2005 Universitat d'Alacant / Universidad de Alicante
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * 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, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17  * 02111-1307, USA.
18  */
19 #ifndef _EXPANDER_
20 #define _EXPANDER_
22 #include <lttoolbox/Ltstr.H>
24 #include <list>
25 #include <map>
26 #include <string>
27 #include <libxml/xmlreader.h>
29 using namespace std;
31 typedef list<pair<string, string> > EntList;
33 /**
34  * A compiler of dictionaries to letter transducers
35  */
36 class Expander
38 private:
39   /**
40    * The libxml2's XML reader
41    */
42   xmlTextReaderPtr reader;
43   
44   /**
45    * The paradigm being compiled
46    */
47   string current_paradigm;
48   
49   /**
50    * The direction of the compilation, 'lr' (left-to-right) or 'rl'
51    * (right-to-left)
52    */
53   string direction;
54   
55   /**
56    * Paradigms
57    */
58   map<string, EntList, Ltstr> paradigm;
60   map<string, EntList, Ltstr> paradigm_lr;
62   map<string, EntList, Ltstr> paradigm_rl;
64   /**
65    * Method to parse an XML Node
66    */
67   void procNode(FILE *output);
69   /**
70    * Parse the &lt;pardef&gt; element
71    */
72   void procParDef();
73   
74   /**
75    * Parse the &lt;e&gt; element
76    */
77   void procEntry(FILE *output);
79   /**
80    * Parse the &lt;re&gt; element
81    * @return the string representing the regular expression
82    */
83   string procRegExp();
85   /**
86    * Gets an attribute value with their name and the current context
87    * @param name the name of the attribute
88    * @return the value of the attribute
89    */
90   string attrib(string const &name);
92   /**
93    * Parse the &lt;p&lt; element
94    * @return a pair of string, left part and right part of a transduction
95    */
96   pair<string, string> procTransduction();
98   /**
99    * Parse the &lt;i&lt; element
100    * @return a string from the dictionary's entry
101    */
102   string procIdentity();
104   /**
105    * Parse the &lt;par&gt; element
106    * @return the name of the paradigm
107    */
108   string procPar();
110   /**
111    * Skip all document #text nodes before "elem"
112    * @param name the name of the node
113    * @param elem the name of the expected node
114    */
115   void skip(string &name, string const &elem);
117   /**
118    * Skip all blank #text nodes before "name"
119    * @param name the name of the node
120    */
121   void skipBlanks(string &name);
122   
123   
124   void readString(string &result, string const &name);
125   
126   /**
127    * Force an element to be empty, and check for it
128    * @param name the element 
129    */
130   void requireEmptyError(string const &name);
132   /**
133    * Force an attribute to be specified, amd check for it
134    * @param value the value of the attribute
135    * @param attrname the name of the attribute
136    * @param elemname the parent of the attribute
137    */
138   void requireAttribute(string const &value, string const &attrname,
139                         string const &elemname);
141   /**
142    * True if all the elements in the current node are blanks
143    * @return true if all are blanks
144    */
145   bool allBlanks();
147   /**
148    * Append a list of endings to a list of current transductions.
149    * @param result the current partial transductions, and after calling
150    *               this method, the result of concatenations.
151    * @param endings the endings to be appended.
152    */
153   static void append(list<pair<string, string> > &result, 
154                      list<pair<string, string> > const &endings);
156   /**
157    * Append a list of endings to a list of current transductions.
158    * @param result the current partial transductions, and after calling
159    *               this method, the result of concatenations.
160    * @param endings the endings to be appended.
161    */
162   static void append(list<pair<string, string> > &result, 
163                      string const &endings);
165   /**
166    * Append a list of endings to a list of current transductions.
167    * @param result the current partial transductions, and after calling
168    *               this method, the result of concatenations.
169    * @param endings the endings to be appended.
170    */
171   static void append(list<pair<string, string> > &result, 
172                      pair<string, string> const &endings);
174 public:
175   /**
176    * Constructor
177    */
178   Expander();
180   /**
181    * Destructor
182    */
183   ~Expander();
185   /**
186    * Compile dictionary to letter transducers
187    */
188   void expand(string const &fichero, FILE *output);
192 #endif