Debian Codegraph @

dwtools/NavigationContext.cpp

raw
1 /* NavigationContext.cpp
2 *
3 * Copyright (C) 2020-2021 David Weenink
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. See the GNU
13 * 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 #include "Distributions_and_Strings.h"
20 #include "NavigationContext.h"
21 #include "NUM2.h"
22 #include "Strings_extensions.h"
23
24
25 #include "enums_getText.h"
26 #include "NavigationContext_enums.h"
27 #include "enums_getValue.h"
28 #include "NavigationContext_enums.h"
29
30 #include "oo_DESTROY.h"
31 #include "NavigationContext_def.h"
32 #include "oo_COPY.h"
33 #include "NavigationContext_def.h"
34 #include "oo_EQUAL.h"
35 #include "NavigationContext_def.h"
36 #include "oo_CAN_WRITE_AS_ENCODING.h"
37 #include "NavigationContext_def.h"
38 #include "oo_WRITE_TEXT.h"
39 #include "NavigationContext_def.h"
40 #include "oo_READ_TEXT.h"
41 #include "NavigationContext_def.h"
42 #include "oo_WRITE_BINARY.h"
43 #include "NavigationContext_def.h"
44 #include "oo_READ_BINARY.h"
45 #include "NavigationContext_def.h"
46 #include "oo_DESCRIPTION.h"
47 #include "NavigationContext_def.h"
48
49
50 Thing_implement (NavigationContext, Daata, 0);
51
52 static bool STRVEChasMatch (constSTRVEC const& labels, conststring32 label, kMelder_string criterion, kMatchBoolean matchBoolean) {
53 bool match = true;
54 if (matchBoolean == kMatchBoolean::OR_) {
55 for (integer istring = 1; istring <= labels.size; istring ++)
56 if (Melder_stringMatchesCriterion (label, criterion, labels [istring], true))
57 return true;
58 match = false;
59 } else { // matchBoolean == kMatchBoolean::AND_
60 for (integer istring = 1; istring <= labels.size; istring ++)
61 if (! (match = match && Melder_stringMatchesCriterion (label, criterion, labels [istring], true)))
62 return false;
63 match = true;
64 }
65 return match;
66 }
67
68 void structNavigationContext :: v1_info () {
69 // skipping parent classes
70 if (topicLabels && topicLabels -> strings.size > 0) {
71 MelderInfo_writeLine (U"\tTopic criterion: ", kMelder_string_getText (topicCriterion));
72 MelderInfo_writeLine (U"\tTopic match boolean: ", kMatchBoolean_getText (topicMatchBoolean));
73 MelderInfo_writeLine (U"\tNumber of Topic labels: ", topicLabels -> strings.size);
74 } else {
75 MelderInfo_writeLine (U"\tNo Topic labels defined");
76 }
77 if (beforeLabels && beforeLabels -> strings.size > 0) {
78 MelderInfo_writeLine (U"\tBefore criterion: ", kMelder_string_getText (beforeCriterion));
79 MelderInfo_writeLine (U"\tBefore match boolean: ", kMatchBoolean_getText (beforeMatchBoolean));
80 MelderInfo_writeLine (U"\tNumber of Before labels: ", beforeLabels -> strings.size);
81 } else {
82 MelderInfo_writeLine (U"\tNo Before labels defined");
83 }
84 if (afterLabels && afterLabels -> strings.size > 0) {
85 MelderInfo_writeLine (U"\tAfter criterion: ", kMelder_string_getText (afterCriterion));
86 MelderInfo_writeLine (U"\tAfter match boolean: ", kMatchBoolean_getText (afterMatchBoolean));
87 MelderInfo_writeLine (U"\tNumber of After labels: ", afterLabels -> strings.size);
88 } else {
89 MelderInfo_writeLine (U"\tNo After labels defined");
90 }
91 MelderInfo_writeLine (U"\tBefore and After use: ", kContext_combination_getText (combinationCriterion));
92 MelderInfo_writeLine (U"\tExclude topic match: ", ( excludeTopicMatch ? U"yes" : U"no" ));
93 }
94
95 autoNavigationContext NavigationContext_createTopicOnly (
96 constSTRVEC const& topicLabels, kMelder_string topicCriterion, kMatchBoolean topicMatchBoolean)
97 {
98 try {
99 autoNavigationContext me = NavigationContext_create (
100 topicLabels, topicCriterion, topicMatchBoolean,
101 { }, kMelder_string::EQUAL_TO, kMatchBoolean::OR_,
102 { }, kMelder_string::EQUAL_TO, kMatchBoolean::OR_,
103 kContext_combination::NO_BEFORE_AND_NO_AFTER, false
104 );
105 return me;
106 } catch (MelderError) {
107 Melder_throw (U"NavigationContext could not be created for Topic.");
108 }
109 }
110
111 autoNavigationContext NavigationContext_createBeforeAndTopic (
112 constSTRVEC const& topicLabels, kMelder_string topicCriterion, kMatchBoolean topicMatchBoolean,
113 constSTRVEC const& beforeLabels, kMelder_string beforeCriterion, kMatchBoolean beforeMatchBoolean)
114 {
115 try {
116 autoNavigationContext me = NavigationContext_create (
117 topicLabels, topicCriterion, topicMatchBoolean,
118 beforeLabels, beforeCriterion, kMatchBoolean::OR_,
119 { }, kMelder_string::EQUAL_TO, kMatchBoolean::OR_,
120 kContext_combination::BEFORE, false
121 );
122 return me;
123 } catch (MelderError) {
124 Melder_throw (U"NavigationContext could not be created for Topic.");
125 }
126 }
127
128 static autoStrings Strings_createAsUniqueTokens (constSTRVEC strings, conststring32 where) {
129 autoStrings me = Strings_createFromSTRVEC (strings);
130 autoStrings thee = Data_copy (me.get());
131 Strings_sort (thee.get());
132 for (integer istring = 2; istring <= thy numberOfStrings; istring ++)
133 Melder_require (! Melder_equ (thy strings [istring].get(), thy strings [istring - 1].get()),
134 U"The ", where, U" labels should be unique, however \"", thy strings [istring].get(), U"\" occurs more than once.");
135 return me;
136 }
137
138 autoNavigationContext NavigationContext_create (
139 constSTRVEC const& topicLabels, kMelder_string topicCriterion, kMatchBoolean topicMatchBoolean,
140 constSTRVEC const& beforeLabels, kMelder_string beforeCriterion, kMatchBoolean beforeMatchBoolean,
141 constSTRVEC const& afterLabels, kMelder_string afterCriterion, kMatchBoolean afterMatchBoolean,
142 kContext_combination combinationCriterion, bool excludeTopicMatch)
143 {
144 try {
145 Melder_require (! (excludeTopicMatch && combinationCriterion == kContext_combination::NO_BEFORE_AND_NO_AFTER),
146 U"You should not exclude Before & After & Topic from matching. One of the three should be included.");
147 autoNavigationContext me = Thing_new (NavigationContext);
148 my topicLabels = Strings_createAsUniqueTokens (topicLabels, U"Topic");
149 my topicCriterion = topicCriterion;
150 my topicMatchBoolean = topicMatchBoolean;
151 my beforeLabels = Strings_createAsUniqueTokens (beforeLabels, U"Before");
152 my beforeCriterion = beforeCriterion;
153 my beforeMatchBoolean = beforeMatchBoolean;
154 my afterLabels = Strings_createAsUniqueTokens (afterLabels, U"After");
155 my afterCriterion = afterCriterion;
156 my afterMatchBoolean = afterMatchBoolean;
157 my combinationCriterion = combinationCriterion;
158 if (combinationCriterion == kContext_combination::BEFORE && beforeLabels.size == 0)
159 Strings_insert (my beforeLabels.get(), 1, U"");
160 else if (combinationCriterion == kContext_combination::AFTER && afterLabels.size == 0)
161 Strings_insert (my afterLabels.get(), 1, U"");
162 else if (combinationCriterion == kContext_combination::BEFORE_AND_AFTER || combinationCriterion == kContext_combination::BEFORE_OR_AFTER_NOT_BOTH ||
163 combinationCriterion == kContext_combination::BEFORE_OR_AFTER_OR_BOTH) {
164 if (beforeLabels.size == 0)
165 Strings_insert (my beforeLabels.get(), 1, U"");
166 if (afterLabels.size == 0)
167 Strings_insert (my afterLabels.get(), 1, U"");
168 }
169 my excludeTopicMatch = excludeTopicMatch;
170 return me;
171 } catch (MelderError) {
172 Melder_throw (U"NavigationContext could not be created.");
173 }
174 }
175
176 static void Strings_checkIfUniqueLabels (Strings me) {
177 autoDistributions distributions = Strings_to_Distributions (me);
178 Melder_require (distributions -> numberOfRows != my numberOfStrings,
179 U"There should be no duplicate labels.");
180 }
181
182 void NavigationContext_replaceTopicLabels (NavigationContext me, Strings labels) {
183 try {
184 Strings_checkIfUniqueLabels (labels);
185 my topicLabels = Data_copy (labels);
186 } catch (MelderError) {
187 Melder_throw (me, U": cannot replace Topic labels from ", labels, U".");
188 }
189 }
190
191 void NavigationContext_replaceBeforeLabels (NavigationContext me, Strings labels) {
192 try {
193 Strings_checkIfUniqueLabels (labels);
194 my beforeLabels = Data_copy (labels);
195 } catch (MelderError) {
196 Melder_throw (me, U": cannot replace Before labels from ", labels, U".");
197 }
198 }
199
200 void NavigationContext_replaceAfterLabels (NavigationContext me, Strings labels) {
201 try {
202 Strings_checkIfUniqueLabels (labels);
203 my afterLabels = Data_copy (labels);
204 } catch (MelderError) {
205 Melder_throw (me, U": cannot replace After labels from ", labels, U".");
206 }
207 }
208
209 void NavigationContext_modifyTopicCriterion (NavigationContext me, kMelder_string criterion, kMatchBoolean matchBoolean) {
210 Melder_require (my topicLabels && my topicLabels -> numberOfStrings > 0,
211 U"There are no Topic labels.");
212 my topicCriterion = criterion;
213 my topicMatchBoolean = matchBoolean;
214 }
215
216 void NavigationContext_modifyBeforeCriterion (NavigationContext me, kMelder_string criterion, kMatchBoolean matchBoolean) {
217 Melder_require (my beforeLabels && my beforeLabels -> numberOfStrings > 0,
218 U"There are no Before labels.");
219 my beforeCriterion = criterion;
220 my beforeMatchBoolean = matchBoolean;
221 }
222
223 void NavigationContext_modifyAfterCriterion (NavigationContext me, kMelder_string criterion, kMatchBoolean matchBoolean) {
224 Melder_require (my afterLabels && my afterLabels -> numberOfStrings > 0,
225 U"There are no After labels.");
226 my afterCriterion = criterion;
227 my afterMatchBoolean = matchBoolean;
228 }
229
230 void NavigationContext_modifyUseCriterion (NavigationContext me, kContext_combination combinationCriterion, bool excludeTopicMatch) {
231 const bool hasBefore = ( my beforeLabels && my beforeLabels -> strings.size > 0 );
232 const bool hasAfter = ( my afterLabels && my afterLabels -> strings.size > 0 );
233 if (combinationCriterion == kContext_combination::BEFORE)
234 Melder_require (hasBefore,
235 U"For the ", kContext_combination_getText (combinationCriterion), U" criterion the NavigationContext should have Before labels.");
236 if (combinationCriterion == kContext_combination::AFTER)
237 Melder_require (hasAfter,
238 U"For the ", kContext_combination_getText (combinationCriterion), U" criterion the NavigationContext should have After labels.");
239 if (combinationCriterion == kContext_combination::BEFORE_AND_AFTER || combinationCriterion == kContext_combination::BEFORE_OR_AFTER_NOT_BOTH ||
240 combinationCriterion == kContext_combination::BEFORE_OR_AFTER_OR_BOTH)
241 Melder_require (hasBefore && hasAfter,
242 U"For the ", kContext_combination_getText (combinationCriterion), U" criterion the NavigationContext should have Before and After labels.");
243 if (excludeTopicMatch)
244 Melder_require (hasBefore || hasAfter,
245 U"For the ", kContext_combination_getText (combinationCriterion), U" criterion the NavigationContext should have Before or After labels.");
246 my excludeTopicMatch = excludeTopicMatch;
247 my combinationCriterion = combinationCriterion;
248 }
249
250 bool NavigationContext_isTopicLabel (NavigationContext me, conststring32 label) {
251 return ( my topicLabels && STRVEChasMatch (my topicLabels -> strings.get(), label, my topicCriterion, my topicMatchBoolean) );
252 }
253
254 bool NavigationContext_isBeforeLabel (NavigationContext me, conststring32 label) {
255 return ( my beforeLabels && STRVEChasMatch (my beforeLabels -> strings.get(), label, my beforeCriterion, my beforeMatchBoolean) );
256 }
257
258 bool NavigationContext_isAfterLabel (NavigationContext me, conststring32 label) {
259 return ( my afterLabels && STRVEChasMatch (my afterLabels -> strings.get(), label, my afterCriterion, my afterMatchBoolean) );
260 }
261
262 /* End of file NavigationContext.cpp */