[go: up one dir, main page]

File: Options.h

package info (click to toggle)
timbl 6.10-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,088 kB
  • sloc: cpp: 17,211; ansic: 425; sh: 70; makefile: 63
file content (502 lines) | stat: -rw-r--r-- 14,967 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
  Copyright (c) 1998 - 2024
  ILK   - Tilburg University
  CLST  - Radboud University
  CLiPS - University of Antwerp

  This file is part of timbl

  timbl is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 3 of the License, or
  (at your option) any later version.

  timbl is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, see <http://www.gnu.org/licenses/>.

  For questions and suggestions, see:
      https://github.com/LanguageMachines/timbl/issues
  or send mail to:
      lamasoftware (at ) science.ru.nl

*/
#ifndef TIMBL_OPTIONS_H
#define TIMBL_OPTIONS_H

#include <vector>
#include <map>
#include <climits>
#include <cstdio>
#include "ticcutils/StringOps.h"

namespace Timbl {

  class OptionClass {
    friend class OptionTableClass;
  public:
    explicit OptionClass( const std::string& n ): Name( n ) {};
    virtual ~OptionClass() {};
    virtual bool set_option( const std::string& ) = 0;
    virtual std::ostream& show_opt( std::ostream & ) const = 0;
    virtual std::ostream& show_full( std::ostream & ) const = 0;
  protected:
    const std::string Name;
  private:
    OptionClass(const OptionClass&);
    OptionClass& operator = (const OptionClass&);
  };

  template <class Type>
  class OptionClassT: public OptionClass {
  public:
    OptionClassT( const std::string& n, Type *tp, Type t ):OptionClass(n),
							   Content(tp) { *Content = t; };
    virtual bool set_option( const std::string& line ) override {
      Type T;
      bool result = TiCC::stringTo<Type>( line, T );
      if ( result ) {
	*Content = T;
      }
      return result;
    };
    virtual std::ostream& show_opt( std::ostream &os ) const override {
      os.width(20);
      os.setf( std::ios::left, std::ios::adjustfield );
      os << Name << " : " << TiCC::toString<Type>(*Content);
      return os;
    };
    virtual std::ostream& show_full( std::ostream &os ) const override {
      return show_opt( os );
    };
    private:
    Type *Content;
    OptionClassT(const OptionClassT&);
    OptionClassT& operator = (const OptionClassT&);
  };

  using BoolOption= OptionClassT<bool>;

  template <>
    inline std::ostream& OptionClassT<bool>::show_opt( std::ostream &os ) const {
      os.width(20);
      os.setf( std::ios::left, std::ios::adjustfield );
      os.setf( std::ios::boolalpha );
      os << Name << " : " << *Content;
      return os;
  }

  template <>
    inline std::ostream& OptionClassT<bool>::show_full( std::ostream &os ) const{
    os.width(20);
    os.setf( std::ios::left, std::ios::adjustfield );
    os.setf( std::ios::boolalpha );
    os << Name << " :  false or true [" << *Content << "]";
    return os;
  }

  using  VerbosityOption = OptionClassT<VerbosityFlags>;

  template <>
    inline std::ostream& OptionClassT<VerbosityFlags>::show_full( std::ostream &os ) const {
    os.width(20);
    os.setf( std::ios::left, std::ios::adjustfield );
    os << Name << " : " << TiCC::toString<VerbosityFlags>(*Content,true);
    return os;
  }

  using InputFormatOption = OptionClassT<InputFormatType>;

  template <>
    inline std::ostream& InputFormatOption::show_full( std::ostream &os ) const {
    os.width(20);
    os.setf( std::ios::left, std::ios::adjustfield );
    os << Name << " : {";
    InputFormatType i = UnknownInputFormat;
    for ( ++i; i < MaxInputFormat-1; ++i ){
      os << TiCC::toString<InputFormatType>(i) << ", ";
    }
    os << TiCC::toString<InputFormatType>(i) << "}, [ "
       << TiCC::toString<InputFormatType>(*Content) << "]";
    return os;
  }


  using MetricOption = OptionClassT<MetricType>;

  template <>
    inline std::ostream& OptionClassT<MetricType>::show_full( std::ostream &os )const {
    os.width(20);
    os.setf( std::ios::left, std::ios::adjustfield );
    os << Name << " : {";
    MetricType i = UnknownMetric;
    for ( ++i; i < MaxMetric-1; ++i ){
      os << TiCC::toString<MetricType>(i) << ", ";
    }
    os << TiCC::toString<MetricType>(i) << "}, [ "
       << TiCC::toString<MetricType>(*Content) << "]";
    return os;
  }

  using  AlgorithmOption = OptionClassT<AlgorithmType>;

  template <>
    inline std::ostream& OptionClassT<AlgorithmType>::show_full( std::ostream &os ) const {
    os.width(20);
    os.setf( std::ios::left, std::ios::adjustfield );
    os << Name << " : {";
    AlgorithmType i = Unknown_a;
    for ( ++i; i < Max_a-1; ++i ){
      os << TiCC::toString<AlgorithmType>(i) << ", ";
    }
    os << TiCC::toString<AlgorithmType>(i) << "}, [ "
       << TiCC::toString<AlgorithmType>(*Content) << "]";
    return os;
  }

  using DecayOption = OptionClassT<DecayType>;

  template <>
    inline std::ostream& DecayOption::show_full( std::ostream &os ) const {
    os.width(20);
    os.setf( std::ios::left, std::ios::adjustfield );
    os << Name << " : {";
    DecayType i = UnknownDecay;
    for ( ++i; i < MaxDecay-1; ++i ){
      os << TiCC::toString<DecayType>(i) << ", ";
    }
    os << TiCC::toString<DecayType>(i) << "}, [ "
       << TiCC::toString<DecayType>(*Content) << "]";
    return os;
  }

  using SmoothOption = OptionClassT<SmoothingType>;

  template <>
    inline std::ostream& SmoothOption::show_full( std::ostream &os ) const {
    os.width(20);
    os.setf( std::ios::left, std::ios::adjustfield );
    os << Name << " : {";
    SmoothingType i = UnknownSmoothing;
    for ( ++i; i < MaxSmoothing-1; ++i ){
      os << TiCC::toString<SmoothingType>(i) << ", ";
    }
    os << TiCC::toString<SmoothingType>(i) << "}, [ "
       << TiCC::toString<SmoothingType>(*Content) << "]";
    return os;
  }

  using WeightOption = OptionClassT<WeightType>;

  template <>
    inline std::ostream& OptionClassT<WeightType>::show_full( std::ostream &os ) const {
    os.width(20);
    os.setf( std::ios::left, std::ios::adjustfield );
    os << Name << " : {";
    WeightType i = Unknown_w;
    for ( ++i; i < Max_w-1; ++i ){
      os << TiCC::toString<WeightType>(i) << ", ";
    }
    os << TiCC::toString<WeightType>(i) << "}, [ "
       << TiCC::toString<WeightType>(*Content) << "]";
    return os;
  }

  using OrdeningOption = OptionClassT<OrdeningType>;

  template <>
    inline std::ostream& OptionClassT<OrdeningType>::show_full( std::ostream &os ) const {
    os.width(20);
    os.setf( std::ios::left, std::ios::adjustfield );
    os << Name << " : {";
    OrdeningType i = UnknownOrdening;
    for ( ++i; i < MaxOrdening-1; ++i ){
      os << TiCC::toString<OrdeningType>(i) << ", ";
    }
    os << TiCC::toString<OrdeningType>(i) << "}, [ "
       << TiCC::toString<OrdeningType>(*Content) << "]";
    return os;
  }

  using  NormalisationOption = OptionClassT<normType>;

  template <>
    inline std::ostream& NormalisationOption::show_full( std::ostream &os ) const {
    os.width(20);
    os.setf( std::ios::left, std::ios::adjustfield );
    os << Name << " : {";
    normType i = unknownNorm;
    for ( ++i; i < maxNorm-1; ++i ){
      os << TiCC::toString<normType>(i) << ", ";
    }
    os << TiCC::toString<normType>(i) << "}, [ "
       << TiCC::toString<normType>(*Content) << "]";
    return os;
  }

  //
  // Array of options types
  //
  template <class Type>
  class OptionArrayClass: public OptionClass {
  public:
    OptionArrayClass( const std::string& n,
		      std::vector<Type>& ta,
		      const size_t size ):
      OptionClass( n ), TA(ta), Size(size ){};
  protected:
    std::vector<Type>& TA;
    size_t Size;
  private:
    OptionArrayClass(const OptionArrayClass&);
    OptionArrayClass& operator = (const OptionArrayClass&);
  };


  class MetricArrayOption: public OptionArrayClass<MetricType> {
  public:
    MetricArrayOption( const std::string& n,
		       std::vector<MetricType>& mp,
		       MetricType& m,
		       size_t s ):
      OptionArrayClass<MetricType>( n, mp, s ), def(m){
      TA.resize(s,m);
    };
    bool set_option( const std::string& line ) override;
    std::ostream& show_opt( std::ostream &os ) const override;
    std::ostream& show_full( std::ostream &os ) const override;
  private:
    const MetricType& def;
  };

  inline bool MetricArrayOption::set_option( const std::string& line ){
    MetricType m = UnknownMetric;
    size_t i=0;
    std::vector<std::string> res;
    bool result = TiCC::split_at( line, res, "=" ) == 2 &&
      TiCC::stringTo<MetricType>( res[1], m ) &&
      TiCC::stringTo<size_t>( res[0], i, 0, Size );
    if ( result ){
      TA[i] = m;
    }
    return result;
  }

  inline std::ostream& MetricArrayOption::show_opt( std::ostream &os ) const {
    os.width(20);
    os.setf( std::ios::left, std::ios::adjustfield );
    os << Name << " : ";
    for ( size_t i=0; i < Size; i++ ){
      if ( TA[i] != def ){
	os << i << ":" << TiCC::toString<MetricType>(TA[i]) << ", ";
      }
    }
    return os;
  }

  inline std::ostream& MetricArrayOption::show_full( std::ostream &os ) const {
    os.width(20);
    os.setf( std::ios::left, std::ios::adjustfield );
    os << Name << " : comma separated metricvalues, [";
    bool first = true;
    for ( size_t i=0; i < Size; i++ ){
      if ( TA[i] != def ){
	if ( !first ){
	  os << ",";
	}
	else {
	  first = false;
	}
	os << i << ":" << TiCC::toString<MetricType>(TA[i]);
      }
    }
    os << "]";
    return os;
  }

  //
  // Limited Type, with min and maxVal
  //
  template <class Type>
    class OptionClassLT: public OptionClass {
    public:
    OptionClassLT( const std::string& n, Type *tp, Type t,
		   Type Min, Type Max ):OptionClass(n),
      Content( tp), minVal( Min ), maxVal( Max )
      { *Content = t; };

    virtual bool set_option( const std::string& line ) override {
      Type T;
      bool result = TiCC::stringTo<Type>( line, T, minVal, maxVal );
      if ( result ) {
	*Content = T;
      }
      return result;
    };
    virtual std::ostream& show_opt( std::ostream &os ) const override {
      os.width(20);
      os.setf( std::ios::showpoint );
      os.setf( std::ios::left, std::ios::adjustfield );
      os << Name << " : " << *Content;
      return os;
    };
    virtual std::ostream& show_full( std::ostream &os ) const override {
      os.width(20);
      os.setf( std::ios::showpoint );
      os.setf( std::ios::left, std::ios::adjustfield );
      os << Name << " :  { "
	 << minVal << " - " << maxVal << "}, [" << *Content << "]";
      return os;
    };
    private:
    Type *Content;
    Type minVal;
    Type maxVal;
    OptionClassLT(const OptionClassLT&);
    OptionClassLT& operator = (const OptionClassLT&);
  };

  using IntegerOption = OptionClassLT<int>;
  using UnsignedOption = OptionClassLT<unsigned int>;
  using SizeOption = OptionClassLT<size_t>;
  using RealOption = OptionClassLT<double>;

  enum SetOptRes { Opt_OK, Opt_Frozen, Opt_Unknown, Opt_Ill_Val};

  struct ci_less
  {
    // case-independent (ci) compare_less binary function
    struct nocase_compare
    {
      bool operator() (const unsigned char& c1, const unsigned char& c2) const {
          return tolower (c1) < tolower (c2);
      }
    };
    bool operator() (const std::string & s1, const std::string & s2) const {
      return std::lexicographical_compare
        (s1.begin(), s1.end(),   // source range
	 s2.begin(), s2.end(),   // dest range
	 nocase_compare());      // comparison
    }
  };

  class OptionTableClass {
  public:
    OptionTableClass():
      table_frozen(false){};
    OptionTableClass( const OptionTableClass& ) = delete; // forbid copies
    OptionTableClass& operator=( const OptionTableClass& ) = delete; // forbid copies
    ~OptionTableClass(){
      for ( const auto& it : global_table ){
	delete it.second;
      }
      for ( const auto& it : runtime_table ){
	delete it.second;
      }
    };
    bool Add( OptionClass *opt ){
      //      std::cerr << "Table add: " << opt->Name << std::endl;
      runtime_table[opt->Name] = opt;
      return true;
    };
    void FreezeTable(void);
    bool TableFrozen(void){ return table_frozen; };
    SetOptRes SetOption( const std::string& );
    void Show_Settings( std::ostream& ) const;
    void Show_Options( std::ostream& ) const;
  private:
    bool table_frozen;
    std::map<std::string,OptionClass *,ci_less> runtime_table;
    std::map<std::string,OptionClass *,ci_less> global_table;
    inline OptionClass *look_up( const std::string&, bool & );
  };

  inline void OptionTableClass::FreezeTable(void){
    global_table = runtime_table;
    runtime_table.clear();
    table_frozen = true;
  }

  inline void OptionTableClass::Show_Settings( std::ostream& os ) const{
    for ( const auto& it: global_table ){
      it.second->show_opt( os ) << std::endl;
    }
    for ( const auto& it: runtime_table ){
      it.second->show_opt( os ) << std::endl;
    }
  }

  inline void OptionTableClass::Show_Options( std::ostream& os ) const {
    for ( const auto& it: global_table ){
      it.second->show_full( os ) << std::endl;
    }
    for ( const auto& it: runtime_table ){
      it.second->show_full( os ) << std::endl;
    }
  }

  inline void split_line( const std::string& line,
			  std::string& name,
			  std::string& value ){
    std::vector<std::string> results;
    size_t i = TiCC::split_at( line, results, ":" );
    switch (i){
    case 2:
      name = TiCC::trim(results[0]);
      // fallthrough
    case 1:
      value = TiCC::trim(results[1]);
    default:
      break;
    }
  }

  inline OptionClass *OptionTableClass::look_up( const std::string& option_name,
						 bool &runtime ){
    //    std::cerr << "lookup: " << option_name << std::endl;
    const auto itr = runtime_table.find( option_name );
    if ( itr != runtime_table.end() ){
      runtime = true;
      //      std::cerr << "FOUND: runtime= " << option_name << std::endl;
      return itr->second;
    }
    else {
      const auto itg = global_table.find( option_name );
      if ( itg != global_table.end() ){
	runtime = table_frozen;
	//	std::cerr << "FOUND global= " << option_name << std::endl;
	return itg->second;
      }
    }
    return NULL;
  }

  inline SetOptRes OptionTableClass::SetOption( const std::string& line ){
    SetOptRes result = Opt_OK;
    bool runtime = false;
    std::string option_name;
    std::string value;
    split_line( line, option_name, value );
    OptionClass *option = look_up( option_name, runtime );
    if ( option ){
      if ( !runtime ){
	result = Opt_Frozen; // may not be changed at this stage
      }
      else if ( !option->set_option( value ) ){
	result = Opt_Ill_Val; // illegal value
      }
    }
    else {
      result = Opt_Unknown; // What the hell ???
    }
    return result;
  }


}

#endif