Minor changes
[apertium.git] / apertium-forms-server / interface.py
blob451a31fa9f2f62b8c0fe7871fcf00240ef6fb4b7
1 #!/usr/bin/python2.5
2 # coding=utf-8
3 # -*- encoding: utf-8 -*-
5 import sys, string, codecs, xml, re, Ft;
6 from Ft.Xml.XPath import Evaluate;
8 sys.stdout = codecs.getwriter('utf-8')(sys.stdout);
9 sys.stderr = codecs.getwriter('utf-8')(sys.stderr);
12 # This class implements the HTML interface for the
13 # forms. If you want to change how they look, make
14 # the change here.
19 class Interface: #{
21 def display(self, post_data): #{
23 if post_data['committing'] == 'yes': #{
24 left_entrada = self.show_entrada(post_data, 'left');
25 bidix_entrada = self.show_entrada(post_data, 'bidix');
26 right_entrada = self.show_entrada(post_data, 'right');
28 print '<pre>';
29 print left_entrada.replace('<', '&lt;').replace('>', '&gt;');
30 print "\n";
31 print bidix_entrada.replace('<', '&lt;').replace('>', '&gt;');
32 print "\n";
33 print right_entrada.replace('<', '&lt;').replace('>', '&gt;');
34 print '</pre>';
36 selected_pair = post_data['selected_pair'];
37 pairs = post_data['pairs'];
38 post_data['left_dictionary'].commit(left_entrada);
39 post_data['bidix_dictionary'].commit(bidix_entrada);
40 post_data['right_dictionary'].commit(right_entrada);
42 # pairs[selected_pair].commit(left_entrada, bidix_entrada, right_entrada);
44 print '<a href="http://xixona.dlsi.ua.es:8080/">again!</a>';
46 return;
49 print '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">';
50 print '<head>';
51 print ' <title>Apertium dictionary management</title>';
52 print ' <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> ';
53 print ' <link rel="stylesheet" type="text/css" href="/static/styles/default.css"/>';
54 print ' <script src="/static/js/boxover.js"></script> ';
55 print '</head>';
56 print '<body>';
57 print '<form action="add" method="POST" name="dixform">';
58 print '<a href="http://xixona.dlsi.ua.es:8080/">home</a>';
59 print '<div id="language_bar">';
60 print 'ca · en · es';
61 print '</div>';
62 print '<div>';
63 print ' <div width="100%"> <!-- Header -->';
64 print ' Language pair: <select name="selected_pair" onChange="dixform.submit();">';
65 for pair in post_data['pairs'].keys(): #{
66 if pair == post_data['selected_pair']: #{
67 print ' <option value="' + pair + '" selected>' + pair + '</option>';
68 else: #{
69 print ' <option value="' + pair + '">' + pair + '</option>';
72 print ' </select>';
73 print ' Part-of-speech: <select name="selected_tag" onChange="dixform.submit();">';
74 for tag in post_data['tags'].keys(): #{
75 if tag == post_data['selected_tag']: #{
76 print ' <option value="' + tag + '" selected>' + tag + '</option>';
77 else: #{
78 print ' <option value="' + tag + '">' + tag + '</option>';
81 print ' </select>';
82 print ' </div>';
83 print '</div>';
84 print '<div>';
85 print ' <hr />';
86 print ' <div id="left"> <!-- Left -->';
87 print ' <p>';
88 print ' Lemma:<sup><span class="tooltip" title="header=[Lemma] body=[Type in the lemma, or citation form of the word you wish to add.]">?</span></sup>';
89 print ' <input type="text" name="left_lemma" value="' + post_data['left_lemma'] + '">';
90 print ' </p>';
91 print ' <p>';
92 print ' Paradigm:<sup><span class="tooltip" title="header=[Paradigm] body=[Paradigms define how a word inflects, select the one that fits the lemma you added.]">?</span></sup>';
93 print ' <select name="left_paradigm">';
94 for left_p in post_data['left_paradigms']: #{
95 if post_data['left_display_mode'] == 'list' and left_p not in post_data['left_glosses']: #{
96 continue;
98 if left_p == post_data['left_paradigm']: #{
99 if left_p in post_data['left_glosses']: #{
100 print ' <option value="' + left_p + '" selected>' + post_data['left_glosses'][left_p] + '</option>';
101 else: #{
102 print ' <option value="' + left_p + '" selected>' + left_p + '</option>';
103 else: #{
104 if left_p in post_data['left_glosses']: #{
105 print ' <option value="' + left_p + '">' + post_data['left_glosses'][left_p] + '</option>';
106 else: #{
107 print ' <option value="' + left_p + '">' + left_p + '</option>';
111 print ' </select>';
112 print ' </p>';
114 self.show_preview(post_data, 'left');
116 left_entrada = self.show_entrada(post_data, 'left');
117 print ' <pre>';
118 print left_entrada.replace('<', '&lt;').replace('>', '&gt;');
119 print ' </pre>';
121 print ' </div>';
123 print ' <!-- Bidix side -->';
124 print ' <div id="centre">';
125 if post_data['restriction'] != 'LR' and post_data['restriction'] != 'RL': #{
126 print ' <input type="radio" name="restriction" value="none" checked>←→<br />';
127 else: #{
128 print ' <input type="radio" name="restriction" value="none">←→<br />';
130 print '';
131 if post_data['restriction'] == 'LR': #{
132 print ' <input type="radio" name="restriction" value="LR" checked>→→<br />';
133 else: #{
134 print ' <input type="radio" name="restriction" value="LR">→→<br />';
136 print '';
137 if post_data['restriction'] == 'RL': #{
138 print ' <input type="radio" name="restriction" value="RL" checked>←←<br />';
139 else: #{
140 print ' <input type="radio" name="restriction" value="RL">←←<br />';
142 print '';
143 print ' <input type="submit" name="clear_box" value="Clear">';
144 print ' <input type="submit" name="preview_box" value="Preview">';
145 print ' <input type="submit" name="commit_box" value="Commit">';
146 print '';
148 bidix_entrada = self.show_entrada(post_data, 'bidix');
149 print '<pre>';
150 print bidix_entrada.replace('<', '&lt;').replace('>', '&gt;');
151 print '</pre>';
152 print ' </div>';
155 print ' <div id="right"> <!-- Right -->';
156 print ' <p>';
157 print ' Lemma:<sup><span class="tooltip" title="header=[Lemma] body=[Type in the lemma, or citation form of the word you wish to add.]">?</span></sup>';
158 print ' <input type="text" name="right_lemma" value="' + post_data['right_lemma'] + '">';
159 print ' </p>';
160 print ' <p>';
161 print ' Paradigm:<sup><span class="tooltip" title="header=[Paradigm] body=[Paradigms define how a word inflects, select the one that fits the lemma you added.]">?</span></sup>';
162 print ' <select name="right_paradigm">';
163 for right_p in post_data['right_paradigms']: #{
164 if post_data['right_display_mode'] == 'list' and right_p not in post_data['right_glosses']: #{
165 continue;
167 if right_p == post_data['right_paradigm']: #{
168 if right_p in post_data['right_glosses']: #{
169 print ' <option value="' + right_p + '" selected>' + post_data['right_glosses'][right_p] + '</option>';
170 else: #{
171 print ' <option value="' + right_p + '" selected>' + right_p + '</option>';
172 else: #{
173 if right_p in post_data['right_glosses']: #{
174 print ' <option value="' + right_p + '">' + post_data['right_glosses'][right_p] + '</option>';
175 else: #{
176 print ' <option value="' + right_p + '">' + right_p + '</option>';
180 print ' </select>';
181 print ' </p>';
183 self.show_preview(post_data, 'right');
185 right_entrada = self.show_entrada(post_data, 'right');
186 print ' <pre>';
187 print right_entrada.replace('<', '&lt;').replace('>', '&gt;');
188 print ' </pre>';
190 print ' </div>';
191 print '</div>';
193 print '</body>';
194 print '</html>';
197 def show_preview(self, post_data, _side): #
198 print >> sys.stderr, 'right paradigm: ' , post_data['right_paradigm'];
199 p = _side + '_paradigm';
200 if post_data['previewing'] == 'on': #{
201 paradigm = post_data[_side + '_dictionary'].get_paradigm(post_data[p], post_data['selected_tag']);
202 if type(paradigm) == type(None): #{
203 return;
205 if type(paradigm.get_stems()) != type(None): #{
206 for s in post_data[_side + '_paradigms'][paradigm.name].get_stems(): #{
207 shows = post_data[_side + '_dictionary'].get_tag_by_tag(post_data['selected_tag']).get_list();
209 if len(shows) > 0: #{
210 for show in post_data[_side + '_dictionary'].get_tag_by_tag(post_data['selected_tag']).get_list(): #{
211 if show == s[1]: #{
212 print self.incondicional(post_data[_side + '_lemma'], paradigm.name) + s[0] + '<br />';
213 print '<span id="symbol_list">' + s[1] + '</span>';
214 print '<p />';
218 else: #{
219 print self.incondicional(post_data[_side + '_lemma'], post_data[_side + '_paradigm']) + s[0] + '<br />';
220 print '<span id="symbol_list">' + s[1] + '</span>';
221 print '<p />';
224 else: #{
225 print ' No stems';
230 def show_entrada(self, post_data, _side): #{
231 if post_data['previewing'] == 'on': #{
232 if _side == 'bidix': #{
233 dictionary = post_data[_side + '_dictionary'];
235 if type(dictionary) != None: #{
236 _lemma1 = post_data['left_lemma'];
237 _lemma2 = post_data['right_lemma'];
238 _tag = post_data['selected_tag'];
239 _comment = '';
240 _restriction = post_data['restriction'];
241 _author = 'webform';
243 entrada = dictionary.generate_bidix_entrada(_lemma1, _lemma2, _tag, _restriction, _comment, _author);
245 return entrada;
249 if _side == 'right' or _side == 'left' and post_data[_side + '_paradigm']: #{
250 dictionary = post_data[_side + '_dictionary'];
251 paradigm = post_data[_side + '_dictionary'].get_paradigm(post_data[_side + '_paradigm'], post_data['selected_tag']);
253 if type(paradigm) != type(None): #{
254 _lemma = post_data[_side + '_lemma'];
255 _restriction = post_data['restriction'];
256 _paradigm = paradigm.name;
257 _comment = '';
258 _author = 'webform';
260 entrada = dictionary.generate_monodix_entrada(_lemma, _paradigm, _restriction, _comment, _author);
262 return entrada;
266 return '';
269 def incondicional(self, _lemma, _paradigm): #{
270 if _paradigm.count('/') < 1: #{
271 return _lemma;
274 paradigm = _paradigm.decode('utf-8');
275 bar_pos = paradigm.find('/');
276 und_pos = paradigm.find('_');
277 chr_str = (und_pos - bar_pos) - 1;
278 l = _lemma.decode('utf-8');
279 r = l[0:(len(l) - chr_str)];
281 return r.encode('utf-8');