| 1 | /* OTMultiEditor.cpp |
| 2 | * |
| 3 | * Copyright (C) 2005-2022,2024 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 2 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 2005/07/04 created |
| 22 | * pb 2007/10/01 constraint plasticity |
| 23 | * pb 2011/03/01 multiple update rules |
| 24 | */ |
| 25 | |
| 26 | #include "OTMultiEditor.h" |
| 27 | #include "EditorM.h" |
| 28 | #include "machine.h" |
| 29 | |
| 30 | Thing_implement (OTMultiEditor, HyperPage, 0); |
| 31 | |
| 32 | static void menu_cb_evaluate (OTMultiEditor me, EDITOR_ARGS) { |
| 33 | EDITOR_FORM (U"Evaluate", nullptr) |
| 34 | REAL (evaluationNoise, U"Evaluation noise", U"2.0") |
| 35 | EDITOR_OK |
| 36 | EDITOR_DO |
| 37 | Editor_save (me, U"Evaluate"); |
| 38 | OTMulti_newDisharmonies (my otMulti(), evaluationNoise); |
| 39 | Graphics_updateWs (my graphics.get()); |
| 40 | Editor_broadcastDataChanged (me); |
| 41 | EDITOR_END |
| 42 | } |
| 43 | |
| 44 | static void menu_cb_evaluate_noise_2_0 (OTMultiEditor me, EDITOR_ARGS) { |
| 45 | Editor_save (me, U"Evaluate (noise 2.0)"); |
| 46 | OTMulti_newDisharmonies (my otMulti(), 2.0); |
| 47 | Graphics_updateWs (my graphics.get()); |
| 48 | Editor_broadcastDataChanged (me); |
| 49 | } |
| 50 | |
| 51 | static void menu_cb_evaluate_tinyNoise (OTMultiEditor me, EDITOR_ARGS) { |
| 52 | Editor_save (me, U"Evaluate (tiny noise)"); |
| 53 | OTMulti_newDisharmonies (my otMulti(), 1e-9); |
| 54 | Graphics_updateWs (my graphics.get()); |
| 55 | Editor_broadcastDataChanged (me); |
| 56 | } |
| 57 | |
| 58 | static void menu_cb_editRanking (OTMultiEditor me, EDITOR_ARGS) { |
| 59 | EDITOR_FORM (U"Edit ranking", nullptr) |
| 60 | MUTABLE_COMMENT (constraintLabel, U"") |
| 61 | REAL (rankingValue, U"Ranking value", U"100.0") |
| 62 | REAL (disharmony, U"Disharmony", U"100.0") |
| 63 | EDITOR_OK |
| 64 | if (my selectedConstraint < 1 || my selectedConstraint > my otMulti() -> numberOfConstraints) |
| 65 | Melder_throw (U"Select a constraint first."); |
| 66 | OTConstraint constraint = & my otMulti() -> constraints [my otMulti() -> index [my selectedConstraint]]; |
| 67 | SET_STRING (constraintLabel, constraint -> name.get()) |
| 68 | SET_REAL (rankingValue, constraint -> ranking) |
| 69 | SET_REAL (disharmony, constraint -> disharmony) |
| 70 | EDITOR_DO |
| 71 | OTConstraint constraint = & my otMulti() -> constraints [my otMulti() -> index [my selectedConstraint]]; |
| 72 | Editor_save (me, U"Edit ranking"); |
| 73 | constraint -> ranking = rankingValue; |
| 74 | constraint -> disharmony = disharmony; |
| 75 | OTMulti_sort (my otMulti()); |
| 76 | Graphics_updateWs (my graphics.get()); |
| 77 | Editor_broadcastDataChanged (me); |
| 78 | EDITOR_END |
| 79 | } |
| 80 | |
| 81 | static void menu_cb_learnOne (OTMultiEditor me, EDITOR_ARGS) { |
| 82 | EDITOR_FORM (U"Learn one", U"OTGrammar: Learn one...") |
| 83 | OPTIONMENU_ENUM (kOTGrammar_rerankingStrategy, updateRule, |
| 84 | U"Update rule", kOTGrammar_rerankingStrategy::SYMMETRIC_ALL) |
| 85 | OPTIONMENU (direction, U"Direction", 3) |
| 86 | OPTION (U"forward") |
| 87 | OPTION (U"backward") |
| 88 | OPTION (U"bidirectionally") |
| 89 | REAL (plasticity, U"Plasticity", U"0.1") |
| 90 | REAL (relativePlasticitySpreading, U"Rel. plasticity spreading", U"0.1") |
| 91 | EDITOR_OK |
| 92 | EDITOR_DO |
| 93 | Editor_save (me, U"Learn one"); |
| 94 | my form1 = GuiText_getString (my form1Text); |
| 95 | my form2 = GuiText_getString (my form2Text); |
| 96 | OTMulti_learnOne (my otMulti(), my form1.get(), my form2.get(), |
| 97 | updateRule, direction, |
| 98 | plasticity, relativePlasticitySpreading); |
| 99 | Graphics_updateWs (my graphics.get()); |
| 100 | Editor_broadcastDataChanged (me); |
| 101 | EDITOR_END |
| 102 | } |
| 103 | |
| 104 | static void menu_cb_removeConstraint (OTMultiEditor me, EDITOR_ARGS) { |
| 105 | if (my selectedConstraint < 1 || my selectedConstraint > my otMulti() -> numberOfConstraints) |
| 106 | Melder_throw (U"Select a constraint first."); |
| 107 | OTConstraint constraint = & my otMulti() -> constraints [my otMulti() -> index [my selectedConstraint]]; |
| 108 | Editor_save (me, U"Remove constraint"); |
| 109 | OTMulti_removeConstraint (my otMulti(), constraint -> name.get()); |
| 110 | Graphics_updateWs (my graphics.get()); |
| 111 | Editor_broadcastDataChanged (me); |
| 112 | } |
| 113 | |
| 114 | static void menu_cb_resetAllRankings (OTMultiEditor me, EDITOR_ARGS) { |
| 115 | EDITOR_FORM (U"Reset all rankings", nullptr) |
| 116 | REAL (ranking, U"Ranking", U"100.0") |
| 117 | EDITOR_OK |
| 118 | EDITOR_DO |
| 119 | Editor_save (me, U"Reset all rankings"); |
| 120 | OTMulti_reset (my otMulti(), ranking); |
| 121 | Graphics_updateWs (my graphics.get()); |
| 122 | Editor_broadcastDataChanged (me); |
| 123 | EDITOR_END |
| 124 | } |
| 125 | |
| 126 | static void menu_cb_OTLearningTutorial (OTMultiEditor, EDITOR_ARGS) { |
| 127 | Melder_help (U"OT learning"); |
| 128 | } |
| 129 | |
| 130 | static void do_limit (OTMultiEditor me) { |
| 131 | my form1 = GuiText_getString (my form1Text); |
| 132 | my form2 = GuiText_getString (my form2Text); |
| 133 | Graphics_updateWs (my graphics.get()); |
| 134 | } |
| 135 | |
| 136 | static void gui_button_cb_limit (OTMultiEditor me, GuiButtonEvent /* event */) { |
| 137 | do_limit (me); |
| 138 | } |
| 139 | |
| 140 | void structOTMultiEditor :: v_createChildren () { |
| 141 | OTMultiEditor_Parent :: v_createChildren (); |
| 142 | #if defined (macintosh) |
| 143 | #define STRING_SPACING 8 |
| 144 | #else |
| 145 | #define STRING_SPACING 2 |
| 146 | #endif |
| 147 | int height = Machine_getTextHeight (), y = Machine_getMenuBarBottom () + 4; |
| 148 | GuiButton_createShown (our windowForm, 4, 124, y, y + height, |
| 149 | U"Partial forms:", gui_button_cb_limit, this, GuiButton_DEFAULT); |
| 150 | form1Text = GuiText_createShown (our windowForm, |
| 151 | 124 + STRING_SPACING, 274 + STRING_SPACING, y, y + Gui_TEXTFIELD_HEIGHT, 0); |
| 152 | form2Text = GuiText_createShown (our windowForm, |
| 153 | 274 + 2 * STRING_SPACING, 424 + 2 * STRING_SPACING, y, y + Gui_TEXTFIELD_HEIGHT, 0); |
| 154 | } |
| 155 | |
| 156 | void structOTMultiEditor :: v_createMenus () { |
| 157 | OTMultiEditor_Parent :: v_createMenus (); |
| 158 | Editor_addCommand (this, U"Edit", U"-- edit ot --", 0, nullptr); |
| 159 | Editor_addCommand (this, U"Edit", U"Evaluate...", 0, menu_cb_evaluate); |
| 160 | Editor_addCommand (this, U"Edit", U"Evaluate (noise 2.0)", '2', menu_cb_evaluate_noise_2_0); |
| 161 | Editor_addCommand (this, U"Edit", U"Evaluate (tiny noise)", '9', menu_cb_evaluate_tinyNoise); |
| 162 | Editor_addCommand (this, U"Edit", U"Edit ranking...", 'E', menu_cb_editRanking); |
| 163 | Editor_addCommand (this, U"Edit", U"Reset all rankings...", 'R', menu_cb_resetAllRankings); |
| 164 | Editor_addCommand (this, U"Edit", U"Learn one...", '1', menu_cb_learnOne); |
| 165 | Editor_addCommand (this, U"Edit", U"-- remove --", 0, nullptr); |
| 166 | Editor_addCommand (this, U"Edit", U"Remove constraint", 0, menu_cb_removeConstraint); |
| 167 | } |
| 168 | |
| 169 | void structOTMultiEditor :: v_createMenuItems_help (EditorMenu menu) { |
| 170 | OTMultiEditor_Parent :: v_createMenuItems_help (menu); |
| 171 | EditorMenu_addCommand (menu, U"OT learning tutorial", 0, menu_cb_OTLearningTutorial); |
| 172 | } |
| 173 | |
| 174 | static OTMulti drawTableau_grammar; |
| 175 | static conststring32 drawTableau_form1, drawTableau_form2; |
| 176 | static bool drawTableau_constraintsAreDrawnVertically; |
| 177 | static void drawTableau (Graphics g) { |
| 178 | OTMulti_drawTableau (drawTableau_grammar, g, drawTableau_form1, drawTableau_form2, drawTableau_constraintsAreDrawnVertically, true); |
| 179 | } |
| 180 | |
| 181 | void structOTMultiEditor :: v_draw () { |
| 182 | static MelderString buffer; |
| 183 | const double rowHeight = 0.25; |
| 184 | longdouble tableauHeight = 2 * rowHeight; |
| 185 | HyperPage_listItem (this, U"\t\t %%ranking value\t %disharmony\t %plasticity"); |
| 186 | for (integer icons = 1; icons <= our otMulti() -> numberOfConstraints; icons ++) { |
| 187 | const OTConstraint constraint = & our otMulti() -> constraints [our otMulti() -> index [icons]]; |
| 188 | MelderString_copy (& buffer, U"\t", ( icons == selectedConstraint ? U"♠︎ " : U" " ), U"@@", icons, |
| 189 | U"|", constraint -> name.get(), U"@\t ", Melder_fixed (constraint -> ranking, 3), |
| 190 | U"\t ", Melder_fixed (constraint -> disharmony, 3), |
| 191 | U"\t ", Melder_fixed (constraint -> plasticity, 6) |
| 192 | ); |
| 193 | HyperPage_listItem (this, buffer.string); |
| 194 | } |
| 195 | Graphics_setAtSignIsLink (graphics.get(), false); |
| 196 | drawTableau_grammar = our otMulti(); |
| 197 | for (integer icand = 1; icand <= our otMulti() -> numberOfCandidates; icand ++) |
| 198 | if (OTMulti_candidateMatches (our otMulti(), icand, our form1.get(), our form2.get())) |
| 199 | tableauHeight += rowHeight; |
| 200 | drawTableau_form1 = our form1.get(); // BUG: dangle |
| 201 | drawTableau_form2 = our form2.get(); |
| 202 | drawTableau_constraintsAreDrawnVertically = our d_constraintsAreDrawnVertically; |
| 203 | HyperPage_picture (this, 20, tableauHeight, drawTableau); |
| 204 | Graphics_setAtSignIsLink (graphics.get(), true); |
| 205 | } |
| 206 | |
| 207 | int structOTMultiEditor :: v_goToPage (conststring32 title) { |
| 208 | if (! title) |
| 209 | return 1; |
| 210 | selectedConstraint = Melder_atoi (title); |
| 211 | return 1; |
| 212 | } |
| 213 | |
| 214 | autoOTMultiEditor OTMultiEditor_create (conststring32 title, OTMulti otMulti) { |
| 215 | try { |
| 216 | autoOTMultiEditor me = Thing_new (OTMultiEditor); |
| 217 | my form1 = Melder_dup (U""); |
| 218 | my form2 = Melder_dup (U""); |
| 219 | HyperPage_init (me.get(), title, otMulti); |
| 220 | return me; |
| 221 | } catch (MelderError) { |
| 222 | Melder_throw (U"OTMulti window not created."); |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | /* End of file OTMultiEditor.cpp */ |