opennn_strings.cpp
1// OpenNN: Open Neural Networks Library
2// www.opennn.net
3//
4// O P E N N N S T R I N G S
5//
6// Artificial Intelligence Techniques, SL
7// artelnics@artelnics.com
8
9#include "opennn_strings.h"
10
11namespace OpenNN
12{
13
17
18Index count_tokens(string& str, const char& separator)
19{
20 trim(str);
21
22 Index tokens_count = 0;
23
24 // Skip delimiters at beginning.
25
26 string::size_type last_pos = str.find_first_not_of(separator, 0);
27
28 // Find first "non-delimiter".
29
30 string::size_type pos = str.find_first_of(separator, last_pos);
31
32 while(string::npos != pos || string::npos != last_pos)
33 {
34 // Found a token, add it to the vector
35
36 tokens_count++;
37
38 // Skip delimiters. Note the "not_of"
39
40 last_pos = str.find_first_not_of(separator, pos);
41
42 // Find next "non-delimiter"
43
44 pos = str.find_first_of(separator, last_pos);
45 }
46
47 return tokens_count;
48}
49
50
51Index count_tokens(const string& s, const char& c)
52{
53 return static_cast<Index>(count(s.begin(), s.end(), c) + 1);
54}
55
56
60
61Tensor<string, 1> get_tokens(const string& str, const char& separator)
62{
63 // const string new_string = get_trimmed(str);
64
65 const Index tokens_number = count_tokens(str, separator);
66
67 Tensor<string, 1> tokens(tokens_number);
68
69 // Skip delimiters at beginning.
70
71 string::size_type lastPos = str.find_first_not_of(separator, 0);
72
73 // Find first "non-delimiter"
74
75 Index index = 0;
76 Index old_pos;
77
78 string::size_type pos = str.find_first_of(separator, lastPos);
79
80 while(string::npos != pos || string::npos != lastPos)
81 {
82 if((lastPos-old_pos != 1) && index!= 0)
83 {
84 tokens[index] = "";
85 index++;
86 old_pos = old_pos+1;
87 continue;
88 }
89 else
90 {
91 // Found a token, add it to the vector
92
93 tokens[index] = str.substr(lastPos, pos - lastPos);
94 }
95
96 old_pos = pos;
97
98 // Skip delimiters. Note the "not_of"
99
100 lastPos = str.find_first_not_of(separator, pos);
101
102 // Find next "non-delimiter"
103
104 pos = str.find_first_of(separator, lastPos);
105
106 index++;
107 }
108
109 return tokens;
110}
111
112
116
117void fill_tokens(const string& str, const char& separator, Tensor<string, 1>& tokens)
118{
119 tokens.setConstant("");
120
121 // Skip delimiters at beginning.
122
123 string::size_type last_position = str.find_first_not_of(separator, 0);
124
125 string::size_type position = str.find_first_of(separator, last_position);
126
127 // Find first "non-delimiter"
128
129 Index index = 0;
130
131 Index old_pos;
132
133
134 while(string::npos != position || string::npos != last_position)
135 {
136 // Found a token, add it to the vector
137
138 if((last_position-old_pos != 1) && index!= 0)
139 {
140 tokens[index] = "";
141 index++;
142 old_pos = old_pos+1;
143 continue;
144 }
145 else
146 {
147 // Found a token, add it to the vector
148
149 tokens[index] = str.substr(last_position, position - last_position);
150 }
151
152 old_pos = position;
153
154 // Skip delimiters. Note the "not_of"
155
156 last_position = str.find_first_not_of(separator, position);
157
158 // Find next "non-delimiter"
159
160 position = str.find_first_of(separator, last_position);
161
162 index++;
163 }
164}
165
166
168
169Tensor<type, 1> to_type_vector(const string& str, const char& separator)
170{
171 const Tensor<string, 1> tokens = get_tokens(str, separator);
172
173 const Index tokens_size = tokens.dimension(0);
174
175 Tensor<type, 1> type_vector(tokens_size);
176
177 for(Index i = 0; i < tokens_size; i++)
178 {
179 try
180 {
181 stringstream buffer;
182
183 buffer << tokens[i];
184
185 type_vector(i) = type(stof(buffer.str()));
186 }
187 catch(const logic_error&)
188 {
189 type_vector(i) = static_cast<type>(nan(""));
190 }
191 }
192
193 return type_vector;
194}
195
196
199
200bool is_numeric_string(const string& str)
201{
202 string::size_type index;
203
204 istringstream iss(str.data());
205
206 float dTestSink;
207
208 iss >> dTestSink;
209
210 // was any input successfully consumed/converted?
211
212 if(!iss) return false;
213
214 // was all the input successfully consumed/converted?
215
216 try
217 {
218 stod(str, &index);
219
220 if(index == str.size() || (str.find("%") != string::npos && index+1 == str.size()))
221 {
222 return true;
223 }
224 else
225 {
226 return false;
227 }
228 }
229 catch(const exception&)
230 {
231 return false;
232 }
233}
234
235
239bool is_constant_string(const Tensor<string, 1>& str)
240{
241 const string str0 = str[0];
242 string str1;
243
244 for(int i = 1; i < str.size(); i++)
245 {
246 str1 = str[i];
247 if(str1.compare(str0) != 0)
248 return false;
249 }
250 return true;
251}
252
255
256bool is_constant_numeric(const Tensor<type, 1>& str)
257{
258 const type a0 = str[0];
259
260 for(int i = 1; i < str.size(); i++)
261 {
262 if(abs(str[i]-a0) > type(1e-3) || isnan(str[i]) || isnan(a0)) return false;
263 }
264
265 return true;
266}
267
268
271
272bool is_date_time_string(const string& str)
273{
274 if(is_numeric_string(str))return false;
275
276 const string format_1 = "(201[0-9]|202[0-9]|19[0-9][0-9])+[-|/|.](0[1-9]|1[0-2])+[-|/|.](0[1-9]|1[0-9]|2[0-9]|3[0-1])+[,| ||-]([0-1][0-9]|2[0-3])+[:]([0-5][0-9])+[:]([0-5][0-9])";
277 const string format_2 = "(201[0-9]|202[0-9]|19[0-9][0-9])+[-|/|.](0[1-9]|1[0-2])+[-|/|.](0[1-9]|1[0-9]|2[0-9]|3[0-1])+[,| ||-]([0-1][0-9]|2[0-3])+[:]([0-5][0-9])";
278 const string format_3 = "(201[0-9]|202[0-9]|19[0-9][0-9])+[-|/|.](0[1-9]|1[0-2])+[-|/|.](0[1-9]|1[0-9]|2[0-9]|3[0-1])";
279 const string format_4 = "(0[1-9]|1[0-9]|2[0-9]|3[0-1])+[-|\\s|/|.](0[1-9]|1[0-2])+[-|\\s|/|.](200[0-9]|201[0-9]|202[0-9]|19[0-9][0-9])+[,| ||-]([0-1][0-9]|2[0-3]|[0-9])+[:]([0-5][0-9])+[:]([0-5][0-9])";
280 const string format_5 = "(0[1-9]|1[0-9]|2[0-9]|3[0-1])+[-|\\s|/|.](0[1-9]|1[0-2])+[-|\\s|/|.](200[0-9]|201[0-9]|202[0-9]|19[0-9][0-9])+[,| ||-]([0-1][0-9]|2[0-3]|[0-9])+[:]([0-5][0-9])";
281 const string format_6 = "(0[1-9]|1[0-9]|2[0-9]|3[0-1])+[-|\\s|/|.](0[1-9]|1[0-2])+[-|\\s|/|.](200[0-9]|201[0-9]|202[0-9]|19[0-9][0-9])";
282 const string format_7 = "(201[0-9]|202[0-9]|19[0-9][0-9])+[-|/|.]([Jj]an(?:uary)?|[Ff]eb(?:ruary)?|[Mm]ar(?:ch)?|[Aa]pr(?:il)?|[Mm]ay|[Jj]un(?:e)?|[Jj]ul(?:y)|[Aa]ug(?:gust)?|[Ss]ep(?:tember)?|[Oo]ct(?:ober)?|[Nn]ov(?:ember)?|[Dd]ec(?:ember)?)+[-|/|.](0[1-9]|1[0-9]|2[0-9]|3[0-1])+[,| ||-]([0-1][0-9]|2[0-3])+[:]([0-5][0-9])+[:]([0-5][0-9])";
283 const string format_8 = "(201[0-9]|202[0-9]|19[0-9][0-9])+[-|/|.]([Jj]an(?:uary)?|[Ff]eb(?:ruary)?|[Mm]ar(?:ch)?|[Aa]pr(?:il)?|[Mm]ay|[Jj]un(?:e)?|[Jj]ul(?:y)|[Aa]ug(?:gust)?|[Ss]ep(?:tember)?|[Oo]ct(?:ober)?|[Nn]ov(?:ember)?|[Dd]ec(?:ember)?)+[-|/|.](0[1-9]|1[0-9]|2[0-9]|3[0-1])+[,| ||-]([0-1][0-9]|2[0-3])+[:]([0-5][0-9])";
284 const string format_9 = "(201[0-9]|202[0-9]|19[0-9][0-9])+[-|/|.]([Jj]an(?:uary)?|[Ff]eb(?:ruary)?|[Mm]ar(?:ch)?|[Aa]pr(?:il)?|[Mm]ay|[Jj]un(?:e)?|[Jj]ul(?:y)|[Aa]ug(?:gust)?|[Ss]ep(?:tember)?|[Oo]ct(?:ober)?|[Nn]ov(?:ember)?|[Dd]ec(?:ember)?)+[-|/|.](0[1-9]|1[0-9]|2[0-9]|3[0-1])";
285 const string format_10 = "([Jj]an(?:uary)?|[Ff]eb(?:ruary)?|[Mm]ar(?:ch)?|[Aa]pr(?:il)?|[Mm]ay|[Jj]un(?:e)?|[Jj]ul(?:y)|[Aa]ug(?:gust)?|[Ss]ep(?:tember)?|[Oo]ct(?:ober)?|[Nn]ov(?:ember)?|[Dd]ec(?:ember)?)+ (0[1-9]|1[0-9]|2[0-9]|3[0-1])+[| ][,|.| ](201[0-9]|202[0-9]|19[0-9][0-9])";
286 const string format_11 = "(20[0-9][0-9]|19[0-9][0-9])+[-|/|.](0[1-9]|1[0-2])";
287 const string format_12 = "([0-2][0-9])+[:]([0-5][0-9])+[:]([0-5][0-9])";
288 const string format_13 = "([1-9]|0[1-9]|1[0-2])+[-|/|.](0[1-9]|1[0-9]|2[0-9]|3[0-1])+[-|/|.](201[0-9]|202[0-9]|19[0-9][0-9])+[,| ||-]([0-1][0-9]|2[0-3])+[:]([0-5][0-9])+[:]([0-5][0-9])+[,| ||-][AP]M";
289
290 const regex regular_expression(format_1 + "|" + format_2 + "|" + format_3 + "|" + format_4 + "|" + format_5 + "|" + format_6 + "|" + format_7 + "|" + format_8
291 + "|" + format_9 + "|" + format_10 + "|" + format_11 +"|" + format_12 + "|" + format_13);
292
293 if(regex_match(str, regular_expression))
294 {
295 return true;
296 }
297 else
298 {
299 return false;
300 }
301}
302
303
307
308time_t date_to_timestamp(const string& date, const Index& gmt)
309{
310 struct tm time_structure;
311
312 smatch month;
313
314 const regex months("([Jj]an(?:uary)?)|([Ff]eb(?:ruary)?)|([Mm]ar(?:ch)?)|([Aa]pr(?:il)?)|([Mm]ay)|([Jj]un(?:e)?)|([Jj]ul(?:y)?)"
315 "|([Aa]ug(?:gust)?)|([Ss]ep(?:tember)?)|([Oo]ct(?:ober)?)|([Nn]ov(?:ember)?)|([Dd]ec(?:ember)?)");
316
317 smatch matchs;
318
319 const string format_1 = "(201[0-9]|202[0-9]|200[0-9]|19[0-9][0-9])+[-|/|.](0[1-9]|1[0-2])+[-|/|.](0[1-9]|1[0-9]|2[0-9]|3[0-1])+[,| ||-]([0-1][0-9]|2[0-3])+[:]([0-5][0-9])+[:]([0-5][0-9])";
320 const string format_2 = "(201[0-9]|202[0-9]|200[0-9]|19[0-9][0-9])+[-|/|.](0[1-9]|1[0-2])+[-|/|.](0[1-9]|1[0-9]|2[0-9]|3[0-1])+[,| ||-]([0-1][0-9]|2[0-3])+[:]([0-5][0-9])";
321 const string format_3 = "(201[0-9]|202[0-9]|200[0-9]|19[0-9][0-9])+[-|/|.](0[1-9]|1[0-2])+[-|/|.](0[1-9]|1[0-9]|2[0-9]|3[0-1])";
322 const string format_4 = "(0[1-9]|1[0-9]|2[0-9]|3[0-1])+[-|\\s|/|.](0[1-9]|1[0-2])+[-|\\s|/|.](200[0-9]|201[0-9]|202[0-9]|19[0-9][0-9])+[,| ||-]([0-1][0-9]|2[0-3]|[0-9])+[:]([0-5][0-9])+[:]([0-5][0-9])";
323 const string format_5 = "(0[1-9]|1[0-9]|2[0-9]|3[0-1])+[-|\\s|/|.](0[1-9]|1[0-2])+[-|\\s|/|.](200[0-9]|201[0-9]|202[0-9]|19[0-9][0-9])+[,| ||-]([0-1][0-9]|2[0-3]|[0-9])+[:]([0-5][0-9])";
324 const string format_6 = "(0[1-9]|1[0-9]|2[0-9]|3[0-1])+[-|\\s|/|.](0[1-9]|1[0-2])+[-|\\s|/|.](200[0-9]|201[0-9]|202[0-9]|19[0-9][0-9])";
325 const string format_7 = "(201[0-9]|202[0-9]|200[0-9]|19[0-9][0-9])+[-|/|.]([Jj]an(?:uary)?|[Ff]eb(?:ruary)?|[Mm]ar(?:ch)?|[Aa]pr(?:il)?|[Mm]ay|[Jj]un(?:e)?|[Jj]ul(?:y)|[Aa]ug(?:gust)?|[Ss]ep(?:tember)?|[Oo]ct(?:ober)?|[Nn]ov(?:ember)?|[Dd]ec(?:ember)?)+[-|/|.](0[1-9]|1[0-9]|2[0-9]|3[0-1])+[,| ||-]([0-1][0-9]|2[0-3])+[:]([0-5][0-9])+[:]([0-5][0-9])";
326 const string format_8 = "(201[0-9]|202[0-9]|200[0-9]|19[0-9][0-9])+[-|/|.]([Jj]an(?:uary)?|[Ff]eb(?:ruary)?|[Mm]ar(?:ch)?|[Aa]pr(?:il)?|[Mm]ay|[Jj]un(?:e)?|[Jj]ul(?:y)|[Aa]ug(?:gust)?|[Ss]ep(?:tember)?|[Oo]ct(?:ober)?|[Nn]ov(?:ember)?|[Dd]ec(?:ember)?)+[-|/|.](0[1-9]|1[0-9]|2[0-9]|3[0-1])+[,| ||-]([0-1][0-9]|2[0-3])+[:]([0-5][0-9])";
327 const string format_9 = "(201[0-9]|202[0-9]|200[0-9]|19[0-9][0-9])+[-|/|.]([Jj]an(?:uary)?|[Ff]eb(?:ruary)?|[Mm]ar(?:ch)?|[Aa]pr(?:il)?|[Mm]ay|[Jj]un(?:e)?|[Jj]ul(?:y)|[Aa]ug(?:gust)?|[Ss]ep(?:tember)?|[Oo]ct(?:ober)?|[Nn]ov(?:ember)?|[Dd]ec(?:ember)?)+[-|/|.](0[1-9]|1[0-9]|2[0-9]|3[0-1])";
328 const string format_10 = "([Jj]an(?:uary)?|[Ff]eb(?:ruary)?|[Mm]ar(?:ch)?|[Aa]pr(?:il)?|[Mm]ay|[Jj]un(?:e)?|[Jj]ul(?:y)|[Aa]ug(?:gust)?|[Ss]ep(?:tember)?|[Oo]ct(?:ober)?|[Nn]ov(?:ember)?|[Dd]ec(?:ember)?)+ (0[1-9]|1[0-9]|2[0-9]|3[0-1])+[| ][,|.| ](201[0-9]|202[0-9]|19[0-9][0-9])";
329 const string format_11 = "(20[0-9][0-9]|19[0-9][0-9])+[-|/|.](0[1-9]|1[0-2])";
330 const string format_12 = "([0-2][0-9])+[:]([0-5][0-9])+[:]([0-5][0-9])";
331 const string format_13 = "([1-9]|0[1-9]|1[0-2])+[-|/|.](0[1-9]|1[0-9]|2[0-9]|3[0-1])+[-|/|.](201[0-9]|202[0-9]|19[0-9][0-9])+[,| ||-]([0-1][0-9]|2[0-3])+[:]([0-5][0-9])+[:]([0-5][0-9])+[,| ||-][AP]M";
332 const string format_14 = "(201[0-9]|202[0-9]|200[0-9]|19[0-9][0-9])";
333
334 const regex regular_expression(format_1 + "|" + format_2 + "|" + format_3 + "|" + format_4 + "|" + format_5 + "|" + format_6 + "|" + format_7 + "|" + format_8
335 + "|" + format_9 + "|" + format_10 + "|" + format_11 +"|" + format_12 + "|" + format_13 + "|" + format_14);
336
337 regex_search(date, matchs, regular_expression);
338
339 if(matchs[1] != "") // yyyy/mm/dd hh:mm:ss
340 {
341 if(stoi(matchs[1].str()) < 1970)
342 {
343 ostringstream buffer;
344
345 buffer << "OpenNN Exception: DataSet Class.\n"
346 << "time_t date_to_timestamp(const string&) method.\n"
347 << "Cannot convert dates below 1970.\n";
348
349 throw logic_error(buffer.str());
350 }
351 else
352 {
353
354 time_structure.tm_year = stoi(matchs[1].str())-1900;
355 time_structure.tm_mon = stoi(matchs[2].str())-1;
356 time_structure.tm_mday = stoi(matchs[3].str());
357 time_structure.tm_hour = stoi(matchs[4].str()) - static_cast<int>(gmt);
358 time_structure.tm_min = stoi(matchs[5].str());
359 time_structure.tm_sec = stoi(matchs[6].str());
360 }
361 }
362 else if(matchs[7] != "") // yyyy/mm/dd hh:mm
363 {
364 if(stoi(matchs[7].str()) < 1970)
365 {
366 ostringstream buffer;
367
368 buffer << "OpenNN Exception: DataSet Class.\n"
369 << "time_t date_to_timestamp(const string&) method.\n"
370 << "Cannot convert dates below 1970.\n";
371
372 throw logic_error(buffer.str());
373 }
374 else
375 {
376 time_structure.tm_year = stoi(matchs[7].str())-1900;
377 time_structure.tm_mon = stoi(matchs[8].str())-1;
378 time_structure.tm_mday = stoi(matchs[9].str());
379 time_structure.tm_hour = stoi(matchs[10].str()) - static_cast<int>(gmt);
380 time_structure.tm_min = stoi(matchs[11].str());
381 time_structure.tm_sec = 0;
382 }
383 }
384 else if(matchs[12] != "") // yyyy/mm/dd
385 {
386 if(stoi(matchs[12].str()) < 1970)
387 {
388 ostringstream buffer;
389
390 buffer << "OpenNN Exception: DataSet Class.\n"
391 << "time_t date_to_timestamp(const string&) method.\n"
392 << "Cannot convert dates below 1970.\n";
393
394 throw logic_error(buffer.str());
395 }
396 else
397 {
398 time_structure.tm_year = stoi(matchs[12].str())-1900;
399 time_structure.tm_mon = stoi(matchs[13].str())-1;
400 time_structure.tm_mday = stoi(matchs[14].str());
401 time_structure.tm_hour = 0;
402 time_structure.tm_min = 0;
403 time_structure.tm_sec = 0;
404 }
405 }
406 else if(matchs[15] != "") // dd/mm/yyyy hh:mm:ss
407 {
408 if(stoi(matchs[17].str()) < 1970)
409 {
410 ostringstream buffer;
411
412 buffer << "OpenNN Exception: DataSet Class.\n"
413 << "time_t date_to_timestamp(const string&) method.\n"
414 << "Cannot convert dates below 1970.\n";
415
416 throw logic_error(buffer.str());
417 }
418 else
419 {
420 time_structure.tm_year = stoi(matchs[17].str())-1900;
421 time_structure.tm_mon = stoi(matchs[16].str())-1;
422 time_structure.tm_mday = stoi(matchs[15].str());
423 time_structure.tm_hour = stoi(matchs[18].str()) - static_cast<int>(gmt);
424 time_structure.tm_min = stoi(matchs[19].str());
425 time_structure.tm_sec = stoi(matchs[20].str());
426 }
427 }
428 else if(matchs[21] != "") // dd/mm/yyyy hh:mm
429 {
430 if(stoi(matchs[23].str()) < 1970)
431 {
432 ostringstream buffer;
433
434 buffer << "OpenNN Exception: DataSet Class.\n"
435 << "time_t date_to_timestamp(const string&) method.\n"
436 << "Cannot convert dates below 1970.\n";
437
438 throw logic_error(buffer.str());
439 }
440 else
441 {
442 time_structure.tm_year = stoi(matchs[23].str())-1900;
443 time_structure.tm_mon = stoi(matchs[22].str())-1;
444 time_structure.tm_mday = stoi(matchs[21].str());
445 time_structure.tm_hour = stoi(matchs[24].str()) - static_cast<int>(gmt);
446 time_structure.tm_min = stoi(matchs[25].str());
447 time_structure.tm_sec = 0;
448 }
449 }
450 else if(matchs[26] != "") // dd/mm/yyyy
451 {
452 if(stoi(matchs[28].str()) < 1970)
453 {
454 ostringstream buffer;
455
456 buffer << "OpenNN Exception: DataSet Class.\n"
457 << "time_t date_to_timestamp(const string&) method.\n"
458 << "Cannot convert dates below 1970.\n";
459
460 throw logic_error(buffer.str());
461 }
462 else
463 {
464 time_structure.tm_year = stoi(matchs[28].str())-1900;
465 time_structure.tm_mon = stoi(matchs[27].str())-1;
466 time_structure.tm_mday = stoi(matchs[26].str());
467 time_structure.tm_hour = 0;
468 time_structure.tm_min = 0;
469 time_structure.tm_sec = 0;
470 }
471 }
472 else if(matchs[29] != "") // yyyy/mmm|mmmm/dd hh:mm:ss
473 {
474 if(stoi(matchs[29].str()) < 1970)
475 {
476 ostringstream buffer;
477
478 buffer << "OpenNN Exception: DataSet Class.\n"
479 << "time_t date_to_timestamp(const string&) method.\n"
480 << "Cannot convert dates below 1970.\n";
481
482 throw logic_error(buffer.str());
483 }
484 else
485 {
486 regex_search(date, month, months);
487
488 Index month_number = 0;
489
490 if(!month.empty())
491 {
492 for(Index i = 1; i < 13; i++)
493 {
494 if(month[static_cast<size_t>(i)] != "") month_number = i;
495 }
496 }
497
498 time_structure.tm_year = stoi(matchs[29].str())-1900;
499 time_structure.tm_mon = static_cast<int>(month_number) - 1;
500 time_structure.tm_mday = stoi(matchs[31].str());
501 time_structure.tm_hour = stoi(matchs[32].str()) - static_cast<int>(gmt);
502 time_structure.tm_min = stoi(matchs[33].str());
503 time_structure.tm_sec = stoi(matchs[34].str());
504 }
505 }
506 else if(matchs[35] != "") // yyyy/mmm|mmmm/dd hh:mm
507 {
508 if(stoi(matchs[35].str()) < 1970)
509 {
510 ostringstream buffer;
511
512 buffer << "OpenNN Exception: DataSet Class.\n"
513 << "time_t date_to_timestamp(const string&) method.\n"
514 << "Cannot convert dates below 1970.\n";
515
516 throw logic_error(buffer.str());
517 }
518 else
519 {
520 regex_search(date, month, months);
521
522 Index month_number = 0;
523 if(!month.empty())
524 {
525 for(Index i =1 ; i<13 ; i++)
526 {
527 if(month[static_cast<size_t>(i)] != "") month_number = i;
528 }
529 }
530
531 time_structure.tm_year = stoi(matchs[35].str())-1900;
532 time_structure.tm_mon = static_cast<int>(month_number) - 1;
533 time_structure.tm_mday = stoi(matchs[37].str());
534 time_structure.tm_hour = stoi(matchs[38].str())- static_cast<int>(gmt);
535 time_structure.tm_min = stoi(matchs[39].str());
536 time_structure.tm_sec = 0;
537 }
538 }
539 else if(matchs[40] != "") // yyyy/mmm|mmmm/dd
540 {
541 if(stoi(matchs[40].str()) < 1970)
542 {
543 ostringstream buffer;
544
545 buffer << "OpenNN Exception: DataSet Class.\n"
546 << "time_t date_to_timestamp(const string&) method.\n"
547 << "Cannot convert dates below 1970.\n";
548
549 throw logic_error(buffer.str());
550 }
551 else
552 {
553 regex_search(date, month, months);
554
555 Index month_number = 0;
556 if(!month.empty())
557 {
558 for(Index i =1 ; i<13 ; i++)
559 {
560 if(month[static_cast<size_t>(i)] != "") month_number = i;
561 }
562 }
563
564 time_structure.tm_year = stoi(matchs[40].str())-1900;
565 time_structure.tm_mon = static_cast<int>(month_number)-1;
566 time_structure.tm_mday = stoi(matchs[42].str())- static_cast<int>(gmt);
567 time_structure.tm_hour = 0;
568 time_structure.tm_min = 0;
569 time_structure.tm_sec = 0;
570 }
571 }
572 else if(matchs[43] != "") // mmm dd, yyyy
573 {
574 if(stoi(matchs[45].str()) < 1970)
575 {
576 ostringstream buffer;
577
578 buffer << "OpenNN Exception: DataSet Class.\n"
579 << "time_t date_to_timestamp(const string&) method.\n"
580 << "Cannot convert dates below 1970.\n";
581
582 throw logic_error(buffer.str());
583 }
584 else
585 {
586 regex_search(date,month,months);
587
588 Index month_number = 0;
589
590 if(!month.empty())
591 {
592 for(Index i =1 ; i<13 ; i++)
593 {
594 if(month[static_cast<size_t>(i)] != "") month_number = i;
595 }
596 }
597
598 time_structure.tm_year = stoi(matchs[45].str())-1900;
599 time_structure.tm_mon = static_cast<int>(month_number)-1;
600 time_structure.tm_mday = stoi(matchs[44].str());
601 time_structure.tm_hour = 0;
602 time_structure.tm_min = 0;
603 time_structure.tm_sec = 0;
604 }
605 }
606 else if(matchs[46] != "") // yyyy/ mm
607 {
608 if(stoi(matchs[46].str()) < 1970)
609 {
610 ostringstream buffer;
611
612 buffer << "OpenNN Exception: DataSet Class.\n"
613 << "time_t date_to_timestamp(const string&) method.\n"
614 << "Cannot convert dates below 1970.\n";
615
616 throw logic_error(buffer.str());
617 }
618 else
619 {
620 time_structure.tm_year = stoi(matchs[46].str())-1900;
621 time_structure.tm_mon = stoi(matchs[47].str())-1;
622 time_structure.tm_mday = 1;
623 time_structure.tm_hour = 0;
624 time_structure.tm_min = 0;
625 time_structure.tm_sec = 0;
626 }
627 }
628 else if(matchs[48] != "") // hh:mm:ss
629 {
630 time_structure.tm_year = 70;
631 time_structure.tm_mon = 0;
632 time_structure.tm_mday = 1;
633 time_structure.tm_hour = stoi(matchs[48].str());
634 time_structure.tm_min = stoi(matchs[49].str());
635 time_structure.tm_sec = stoi(matchs[50].str());
636 }
637 else if(matchs[51] != "") // mm/dd/yyyy hh:mm:ss [AP]M
638 {
639 time_structure.tm_year = stoi(matchs[53].str())-1900;
640 time_structure.tm_mon = stoi(matchs[51].str());
641 time_structure.tm_mday = stoi(matchs[52].str());
642 time_structure.tm_min = stoi(matchs[55].str());
643 time_structure.tm_sec = stoi(matchs[56].str());
644 if(matchs[57].str()=="PM"){
645 time_structure.tm_hour = stoi(matchs[54].str())+12;
646 }
647 else{
648 time_structure.tm_hour = stoi(matchs[54].str());
649 }
650 }
651 else if(matchs[57] != "") // yyyy
652 {
653 time_structure.tm_year = stoi(matchs[57].str())-1900;
654 time_structure.tm_mon = 0;
655 time_structure.tm_mday = 1;
656 time_structure.tm_hour = 0;
657 time_structure.tm_min = 0;
658 time_structure.tm_sec = 0;
659
660 return mktime(&time_structure);
661 }
662 else if(is_numeric_string(date)){
663 }
664 else
665 {
666 ostringstream buffer;
667
668 buffer << "OpenNN Exception: DataSet Class.\n"
669 << "time_t date_to_timestamp(const string&) method.\n"
670 << "Date format (" << date << ") is not implemented.\n";
671
672 throw logic_error(buffer.str());
673 }
674
675 if(is_numeric_string(date))
676 {
677 time_t time_t_date = stoi(date);
678 return time_t_date;
679 }
680 else
681 {
682 return mktime(&time_structure);
683 }
684}
685
686
690
691bool contains_substring(const string& str, const string& sub_str)
692{
693 if(str.find(sub_str) != string::npos)
694 {
695 return true;
696 }
697 return false;
698}
699
700
704
705void trim(string& str)
706{
707 // Prefixing spaces
708
709 str.erase(0, str.find_first_not_of(' '));
710 str.erase(0, str.find_first_not_of('\t'));
711
712 // Surfixing spaces
713
714 str.erase(str.find_last_not_of(' ') + 1);
715 str.erase(str.find_last_not_of('\t') + 1);
716}
717
718
719void erase(string& s, const char& c)
720{
721 s.erase(remove(s.begin(), s.end(), c), s.end());
722}
723
724
728
729string get_trimmed(const string& str)
730{
731 string output(str);
732
733 //prefixing spaces
734
735 output.erase(0, output.find_first_not_of(' '));
736
737 //surfixing spaces
738
739 output.erase(output.find_last_not_of(' ') + 1);
740
741 return output;
742}
743
744
748
749string prepend(const string& pre, const string& str)
750{
751 ostringstream buffer;
752
753 buffer << pre << str;
754
755 return buffer.str();
756}
757
758
761
762bool is_numeric_string_vector(const Tensor<string, 1>& v)
763{
764 for(Index i = 0; i < v.size(); i++)
765 {
766 if(!is_numeric_string(v[i])) return false;
767 }
768
769 return true;
770}
771
772
773bool has_numbers(const Tensor<string, 1>& v)
774{
775 for(Index i = 0; i < v.size(); i++)
776 {
777 if(is_numeric_string(v[i])) return true;
778 }
779
780 return false;
781}
782
783
784bool has_strings(const Tensor<string, 1>& v)
785{
786 for(Index i = 0; i < v.size(); i++)
787 {
788 if(!is_numeric_string(v[i])) return true;
789 }
790
791 return false;
792}
793
796
797bool is_not_numeric(const Tensor<string, 1>& v)
798{
799 for(Index i = 0; i < v.size(); i++)
800 {
801 if(is_numeric_string(v[i])) return false;
802 }
803
804 return true;
805}
806
807
810
811bool is_mixed(const Tensor<string, 1>& v)
812{
813 unsigned count_numeric = 0;
814 unsigned count_not_numeric = 0;
815
816 for(Index i = 0; i < v.size(); i++)
817 {
818 if(is_numeric_string(v[i]))
819 {
820 count_numeric++;
821 }
822 else
823 {
824 count_not_numeric++;
825 }
826 }
827
828 if(count_numeric > 0 && count_not_numeric > 0)
829 {
830 return true;
831 }
832 else
833 {
834 return false;
835 }
836}
837
838
842
843void replace_substring(Tensor<string, 1>& vector, const string& find_what, const string& replace_with)
844{
845 const Index size = vector.dimension(0);
846
847 for(Index i = 0; i < size; i++)
848 {
849 Index position = 0;
850
851 while((position = vector(i).find(find_what, position)) != string::npos)
852 {
853 vector(i).replace(position, find_what.length(), replace_with);
854
855 position += replace_with.length();
856 }
857 }
858}
859
860
861void replace(string& source, const string& find_what, const string& replace_with)
862{
863 Index position = 0;
864
865 while((position = source.find(find_what, position)) != string::npos)
866 {
867 source.replace(position, find_what.length(), replace_with);
868
869 position += replace_with.length();
870 }
871}
872}
873
874// OpenNN: Open Neural Networks Library.
875// Copyright(C) 2005-2021 Artificial Intelligence Techniques, SL.
876//
877// This library is free software; you can redistribute it and/or
878// modify it under the terms of the GNU Lesser General Public
879// License as published by the Free Software Foundation; either
880// version 2.1 of the License, or any later version.
881//
882// This library is distributed in the hope that it will be useful,
883// but WITHOUT ANY WARRANTY; without even the implied warranty of
884// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
885// Lesser General Public License for more details.
886
887// You should have received a copy of the GNU Lesser General Public
888// License along with this library; if not, write to the Free Software
889// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
HALF_CONSTEXPR half abs(half arg)
Definition: half.hpp:2735
HALF_CONSTEXPR bool isnan(half arg)
Definition: half.hpp:4385