Debian Codegraph @

gram/OTGrammarEditor.cpp

raw symbols SCIP
1 /* OTGrammarEditor.cpp
2 *
3 * Copyright (C) 1997-2005,2007-2020,2022,2024,2026 Paul Boersma
4 *
5 * This code 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 3 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /*
20 * a selection of changes:
21 * pb 2003/05/27 learnOne and learnOneFromPartialOutput
22 * pb 2008/05/31 ExponentialMaximumEntropy
23 */
24
25 #include "OTGrammarEditor.h"
26 #include "EditorM.h"
27
28 Thing_implement (OTGrammarEditor, HyperPage, 0);
29
30 static void menu_cb_evaluate (OTGrammarEditor me, EDITOR_ARGS) {
31 EDITOR_FORM (U"Evaluate", nullptr)
32 REAL (noise, U"Noise", U"2.0")
33 EDITOR_OK
34 EDITOR_DO
35 Editor_save (me, U"Evaluate");
36 OTGrammar_newDisharmonies (my otGrammar(), noise);
37 Graphics_updateWs (my graphics.get());
38 Editor_broadcastDataChanged (me);
39 EDITOR_END
40 }
41
42 static void menu_cb_evaluate_noise_2_0 (OTGrammarEditor me, EDITOR_ARGS) {
43 Editor_save (me, U"Evaluate (noise 2.0)");
44 OTGrammar_newDisharmonies (my otGrammar(), 2.0);
45 Graphics_updateWs (my graphics.get());
46 Editor_broadcastDataChanged (me);
47 }
48
49 static void menu_cb_evaluate_tinyNoise (OTGrammarEditor me, EDITOR_ARGS) {
50 Editor_save (me, U"Evaluate (tiny noise)");
51 OTGrammar_newDisharmonies (my otGrammar(), 1e-9);
52 Graphics_updateWs (my graphics.get());
53 Editor_broadcastDataChanged (me);
54 }
55
56 static void menu_cb_evaluate_zeroNoise (OTGrammarEditor me, EDITOR_ARGS) {
57 Editor_save (me, U"Evaluate (zero noise)");
58 OTGrammar_newDisharmonies (my otGrammar(), 0.0);
59 Graphics_updateWs (my graphics.get());
60 Editor_broadcastDataChanged (me);
61 }
62
63 static void menu_cb_editConstraint (OTGrammarEditor me, EDITOR_ARGS) {
64 EDITOR_FORM (U"Edit constraint", nullptr)
65 MUTABLE_COMMENT (constraintLabel, U"");
66 REAL (rankingValue, U"Ranking value", U"100.0");
67 REAL (disharmony, U"Disharmony", U"100.0");
68 REAL (plasticity, U"Plasticity", U"1.0");
69 EDITOR_OK
70 if (my selected < 1 || my selected > my otGrammar() -> numberOfConstraints)
71 Melder_throw (U"Select a constraint first.");
72 OTGrammarConstraint constraint = & my otGrammar() -> constraints [my otGrammar() -> index [my selected]];
73 SET_STRING (constraintLabel, constraint -> name.get())
74 SET_REAL (rankingValue, constraint -> ranking)
75 SET_REAL (disharmony, constraint -> disharmony)
76 SET_REAL (plasticity, constraint -> plasticity)
77 EDITOR_DO
78 OTGrammarConstraint constraint = & my otGrammar() -> constraints [my otGrammar() -> index [my selected]];
79 Editor_save (me, U"Edit constraint");
80 constraint -> ranking = rankingValue;
81 constraint -> disharmony = disharmony;
82 constraint -> plasticity = plasticity;
83 OTGrammar_sort (my otGrammar());
84 Graphics_updateWs (my graphics.get());
85 Editor_broadcastDataChanged (me);
86 EDITOR_END
87 }
88
89 static void menu_cb_learnOne (OTGrammarEditor me, EDITOR_ARGS) {
90 EDITOR_FORM (U"Learn one", U"OTGrammar: Learn one...")
91 COMMENT (U"Underlying form:")
92 SENTENCE (inputString, U"Input string", U"")
93 COMMENT (U"Adult surface form:")
94 SENTENCE (outputString, U"Output string", U"")
95 REAL (evaluationNoise, U"Evaluation noise", U"2.0")
96 OPTIONMENU_ENUM (kOTGrammar_rerankingStrategy, updateRule,
97 U"Update rule", kOTGrammar_rerankingStrategy::SYMMETRIC_ALL)
98 REAL (plasticity, U"Plasticity", U"0.1")
99 REAL (relativePlasticitySpreading, U"Rel. plasticity spreading", U"0.1")
100 BOOLEAN (honourLocalRankings, U"Honour local rankings", true)
101 EDITOR_OK
102 EDITOR_DO
103 Editor_save (me, U"Learn one");
104 OTGrammar_learnOne (my otGrammar(), inputString, outputString,
105 evaluationNoise, updateRule, honourLocalRankings,
106 plasticity, relativePlasticitySpreading, true, true, nullptr);
107 OTGrammar_sort (my otGrammar());
108 Graphics_updateWs (my graphics.get());
109 Editor_broadcastDataChanged (me);
110 EDITOR_END
111 }
112
113 static void menu_cb_learnOneFromPartialOutput (OTGrammarEditor me, EDITOR_ARGS) {
114 EDITOR_FORM (U"Learn one from partial adult output", nullptr)
115 COMMENT (U"Partial adult surface form (e.g. overt form):")
116 SENTENCE (partialOutput, U"Partial output", U"")
117 REAL (evaluationNoise, U"Evaluation noise", U"2.0")
118 OPTIONMENU_ENUM (kOTGrammar_rerankingStrategy, updateRule,
119 U"Update rule", kOTGrammar_rerankingStrategy::SYMMETRIC_ALL)
120 REAL (plasticity, U"Plasticity", U"0.1")
121 REAL (relativePlasticitySpreading, U"Rel. plasticity spreading", U"0.1")
122 BOOLEAN (honourLocalRankings, U"Honour local rankings", true)
123 NATURAL (numberOfChews, U"Number of chews", U"1")
124 EDITOR_OK
125 EDITOR_DO
126 Editor_save (me, U"Learn one from partial output");
127 OTGrammar_learnOneFromPartialOutput (my otGrammar(), partialOutput,
128 evaluationNoise, updateRule, honourLocalRankings,
129 plasticity, relativePlasticitySpreading, numberOfChews, true);
130 OTGrammar_sort (my otGrammar());
131 Graphics_updateWs (my graphics.get());
132 Editor_broadcastDataChanged (me);
133 EDITOR_END
134 }
135
136 static void menu_cb_removeConstraint (OTGrammarEditor me, EDITOR_ARGS) {
137 OTGrammarConstraint constraint;
138 if (my selected < 1 || my selected > my otGrammar() -> numberOfConstraints)
139 Melder_throw (U"Select a constraint first.");
140 constraint = & my otGrammar() -> constraints [my otGrammar() -> index [my selected]];
141 Editor_save (me, U"Remove constraint");
142 OTGrammar_removeConstraint (my otGrammar(), constraint -> name.get());
143 Graphics_updateWs (my graphics.get());
144 Editor_broadcastDataChanged (me);
145 }
146
147 static void menu_cb_resetAllRankings (OTGrammarEditor me, EDITOR_ARGS) {
148 EDITOR_FORM (U"Reset all rankings", nullptr)
149 REAL (ranking, U"Ranking", U"100.0")
150 EDITOR_OK
151 EDITOR_DO
152 Editor_save (me, U"Reset all rankings");
153 OTGrammar_reset (my otGrammar(), ranking);
154 Graphics_updateWs (my graphics.get());
155 Editor_broadcastDataChanged (me);
156 EDITOR_END
157 }
158
159 static void menu_cb_OTGrammarEditor_help (OTGrammarEditor, EDITOR_ARGS) { Melder_help (U"OTGrammarEditor"); }
160 static void menu_cb_OTGrammar_help (OTGrammarEditor, EDITOR_ARGS) { Melder_help (U"OTGrammar"); }
161 static void menu_cb_OTLearningTutorial (OTGrammarEditor, EDITOR_ARGS) { Melder_help (U"OT learning"); }
162
163 void structOTGrammarEditor :: v_createMenus () {
164 OTGrammarEditor_Parent :: v_createMenus ();
165 Editor_addCommand (this, U"Edit", U"-- edit ot --", 0, nullptr);
166 Editor_addCommand (this, U"Edit", U"Evaluate...", 0, menu_cb_evaluate);
167 Editor_addCommand (this, U"Edit", U"Evaluate (noise 2.0)", '2', menu_cb_evaluate_noise_2_0);
168 Editor_addCommand (this, U"Edit", U"Evaluate (zero noise)", '0', menu_cb_evaluate_zeroNoise);
169 Editor_addCommand (this, U"Edit", U"Evaluate (tiny noise)", '9', menu_cb_evaluate_tinyNoise);
170 Editor_addCommand (this, U"Edit", U"Edit constraint...", 'E', menu_cb_editConstraint);
171 Editor_addCommand (this, U"Edit", U"Reset all rankings...", 'R', menu_cb_resetAllRankings);
172 Editor_addCommand (this, U"Edit", U"Learn one...", 0, menu_cb_learnOne);
173 Editor_addCommand (this, U"Edit", U"Learn one from partial output...", '1', menu_cb_learnOneFromPartialOutput);
174 Editor_addCommand (this, U"Edit", U"-- remove ot --", 0, nullptr);
175 Editor_addCommand (this, U"Edit", U"Remove constraint", 0, menu_cb_removeConstraint);
176 }
177
178 void structOTGrammarEditor :: v_createMenuItems_help (EditorMenu menu) {
179 OTGrammarEditor_Parent :: v_createMenuItems_help (menu);
180 EditorMenu_addCommand (menu, U"OTGrammarEditor help", '?', menu_cb_OTGrammarEditor_help);
181 EditorMenu_addCommand (menu, U"OTGrammar help", 0, menu_cb_OTGrammar_help);
182 EditorMenu_addCommand (menu, U"OT learning tutorial", 0, menu_cb_OTLearningTutorial);
183 }
184
185 static OTGrammar drawTableau_ot;
186 static conststring32 drawTableau_input;
187 static bool drawTableau_constraintsAreDrawnVertically;
188 static void drawTableau (Graphics g) {
189 OTGrammar_drawTableau (drawTableau_ot, g, drawTableau_constraintsAreDrawnVertically, drawTableau_input);
190 }
191
192 void structOTGrammarEditor :: v_draw () {
193 static char32 text [1000];
194 if (our otGrammar() -> decisionStrategy == kOTGrammar_decisionStrategy::EXPONENTIAL_HG ||
195 our otGrammar() -> decisionStrategy == kOTGrammar_decisionStrategy::EXPONENTIAL_MAXIMUM_ENTROPY)
196 {
197 HyperPage_listItem (this, U"\t\t %%ranking value\t %disharmony\t %plasticity\t %%e^^disharmony");
198 } else {
199 HyperPage_listItem (this, U"\t\t %%ranking value\t %disharmony\t %plasticity");
200 }
201 for (integer icons = 1; icons <= our otGrammar() -> numberOfConstraints; icons ++) {
202 OTGrammarConstraint constraint = & our otGrammar() -> constraints [our otGrammar() -> index [icons]];
203 if (our otGrammar() -> decisionStrategy == kOTGrammar_decisionStrategy::EXPONENTIAL_HG ||
204 our otGrammar() -> decisionStrategy == kOTGrammar_decisionStrategy::EXPONENTIAL_MAXIMUM_ENTROPY)
205 {
206 Melder_sprint (text,1000,
207 U"\t", icons == selected ? U"♠︎ " : U" ",
208 U"@@", icons,
209 U"|", constraint -> name.get(),
210 U"@\t ", Melder_fixed (constraint -> ranking, 3),
211 U"\t ", Melder_fixed (constraint -> disharmony, 3),
212 U"\t ", Melder_fixed (constraint -> plasticity, 6),
213 U"\t ", Melder_graphicalHalf (exp (constraint -> disharmony)));
214 } else {
215 Melder_sprint (text,1000,
216 U"\t", icons == selected ? U"♠︎ " : U" ",
217 U"@@", icons,
218 U"|", constraint -> name.get(),
219 U"@\t ", Melder_fixed (constraint -> ranking, 3),
220 U"\t ", Melder_fixed (constraint -> disharmony, 3),
221 U"\t ", Melder_fixed (constraint -> plasticity, 6));
222 }
223 HyperPage_listItem (this, text);
224 }
225 Graphics_setAtSignIsLink (graphics.get(), false);
226 for (integer itab = 1; itab <= our otGrammar() -> numberOfTableaus; itab ++) {
227 OTGrammarTableau tableau = & our otGrammar() -> tableaus [itab];
228 double rowHeight = 0.25;
229 double tableauHeight = rowHeight * (tableau -> numberOfCandidates + 2);
230 drawTableau_ot = our otGrammar();
231 drawTableau_input = tableau -> input.get();
232 drawTableau_constraintsAreDrawnVertically = d_constraintsAreDrawnVertically;
233 HyperPage_picture (this, 20, tableauHeight, drawTableau);
234 }
235 Graphics_setAtSignIsLink (graphics.get(), true);
236 }
237
238 int structOTGrammarEditor :: v_goToPage (conststring32 title) {
239 if (! title)
240 return 1;
241 selected = Melder_atoi (title);
242 return 1;
243 }
244
245 autoOTGrammarEditor OTGrammarEditor_create (conststring32 title, OTGrammar otGrammar) {
246 try {
247 autoOTGrammarEditor me = Thing_new (OTGrammarEditor);
248 HyperPage_init (me.get(), title, otGrammar);
249 return me;
250 } catch (MelderError) {
251 Melder_throw (U"OTGrammar window not created.");
252 }
253 }
254
255 /* End of file OTGrammarEditor.cpp */