Class: Wapiti::Options

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/wapiti/options.rb,
ext/wapiti/native.c

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



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
# File 'ext/wapiti/native.c', line 77

static VALUE initialize_options(int argc, VALUE *argv, VALUE self) {
  opt_t* options = get_options(self);
  *options = opt_defaults;

  if (options->maxiter == 0) {
    options->maxiter = INT_MAX;
  }

  // Copy default algorithm and type name to the heap
  // so that all options strings are on the heap.
  options->algo = to_heap(options->algo);
  options->type = to_heap(options->type);

  if (argc > 1) {
    rb_raise(cArgumentError,
      "wrong number of arguments (%d for 0..1)", argc);
  }

  // set defaults
  if (argc) {
    Check_Type(argv[0], T_HASH);
    (void)rb_funcall(self, rb_intern("update!"), 1, argv[0]);
  }

  // yield self if block_given?
  if (rb_block_given_p()) {
    rb_yield(self);
  }

  return self;
}

Class Attribute Details

.algorithmsObject (readonly)

Returns the value of attribute algorithms.



44
45
46
# File 'lib/wapiti/options.rb', line 44

def algorithms
  @algorithms
end

.attribute_namesObject (readonly)

Returns the value of attribute attribute_names.



44
45
46
# File 'lib/wapiti/options.rb', line 44

def attribute_names
  @attribute_names
end

.typesObject (readonly)

Returns the value of attribute types.



44
45
46
# File 'lib/wapiti/options.rb', line 44

def types
  @types
end

Instance Attribute Details

#compressObject Also known as: compress?

Returns the value of attribute compress.



52
53
54
# File 'lib/wapiti/options.rb', line 52

def compress
  @compress
end

Class Method Details

.defaultsObject

Returns the default options.



47
48
49
# File 'lib/wapiti/options.rb', line 47

def defaults
  @defaults ||= new.attributes
end

Instance Method Details

#<=>(other) ⇒ Object



157
158
159
# File 'lib/wapiti/options.rb', line 157

def <=>(other)
  other.respond_to?(:attributes) ? attributes <=> other.attributes : nil
end

#[](name) ⇒ Object

Returns the value of the attribute identified by name or nil if there is no such attribute.



58
59
60
# File 'lib/wapiti/options.rb', line 58

def [](name)
  has_attribute?(name) ? send(name) : nil
end

#[]=(name, value) ⇒ Object

Updates the value of the attribute identified by name with the passed-in value.

Raises:

  • (ArgumentError)


64
65
66
67
68
69
# File 'lib/wapiti/options.rb', line 64

def []=(name, value)
  raise ArgumentError,
    "bad attribute name: #{name}" unless has_attribute?(name)

  send("#{name}=", value)
end

#algorithmObject Also known as: algo



417
418
419
420
# File 'ext/wapiti/native.c', line 417

static VALUE options_algorithm(VALUE self) {
  const char *algorithm = get_options(self)->algo;
  return rb_str_new2(algorithm ? algorithm : "");
}

#algorithm=(rb_string) ⇒ Object Also known as: algo=



422
423
424
425
426
# File 'ext/wapiti/native.c', line 422

static VALUE options_set_algorithm(VALUE self, VALUE rb_string) {
  opt_t *options = get_options(self);
  copy_string((char**)&(options->algo), rb_string);
  return rb_string;
}

#alphaObject



250
251
252
# File 'ext/wapiti/native.c', line 250

static VALUE options_alpha(VALUE self) {
  return rb_float_new(get_options(self)->sgdl1.alpha);
}

#alpha=(rb_numeric) ⇒ Object



254
255
256
257
# File 'ext/wapiti/native.c', line 254

static VALUE options_set_alpha(VALUE self, VALUE rb_numeric) {
  get_options(self)->sgdl1.alpha = NUM2DBL(rb_numeric);
  return rb_numeric;
}

#attributes(attrs = Options.attribute_names) ⇒ Object Also known as: to_hash

Returns a hash of the given attributes with their names and values.



104
105
106
# File 'lib/wapiti/options.rb', line 104

def attributes(attrs = Options.attribute_names)
  Hash[*attrs.map { |a| [a, send(a)] }.flatten]
end

#bcdObject



95
96
97
# File 'lib/wapiti/options.rb', line 95

def bcd
  attributes(:kappa)
end

#checkObject Also known as: check?



335
336
337
# File 'ext/wapiti/native.c', line 335

static VALUE options_check(VALUE self) {
  return get_options(self)->check ? Qtrue : Qfalse;
}

#check=(rb_boolean) ⇒ Object



339
340
341
342
# File 'ext/wapiti/native.c', line 339

static VALUE options_set_check(VALUE self, VALUE rb_boolean) {
  get_options(self)->check = !(TYPE(rb_boolean) == T_NIL || !rb_boolean);
  return rb_boolean;
}

#clipObject



371
372
373
# File 'ext/wapiti/native.c', line 371

static VALUE options_clip(VALUE self) {
  return get_options(self)->lbfgs.clip ? Qtrue : Qfalse;
}

#clip=(rb_boolean) ⇒ Object



375
376
377
378
# File 'ext/wapiti/native.c', line 375

static VALUE options_set_clip(VALUE self, VALUE rb_boolean) {
  get_options(self)->lbfgs.clip = !(TYPE(rb_boolean) == T_NIL || !rb_boolean);
  return rb_boolean;
}

#compactObject Also known as: compact?



317
318
319
# File 'ext/wapiti/native.c', line 317

static VALUE options_compact(VALUE self) {
  return get_options(self)->compact ? Qtrue : Qfalse;
}

#compact=(rb_boolean) ⇒ Object



321
322
323
324
# File 'ext/wapiti/native.c', line 321

static VALUE options_set_compact(VALUE self, VALUE rb_boolean) {
  get_options(self)->compact = !(TYPE(rb_boolean) == T_NIL || !rb_boolean);
  return rb_boolean;
}

#cutoffObject



380
381
382
# File 'ext/wapiti/native.c', line 380

static VALUE options_cutoff(VALUE self) {
  return get_options(self)->rprop.cutoff ? Qtrue : Qfalse;
}

#cutoff=(rb_boolean) ⇒ Object



384
385
386
387
# File 'ext/wapiti/native.c', line 384

static VALUE options_set_cutoff(VALUE self, VALUE rb_boolean) {
  get_options(self)->rprop.cutoff = !(TYPE(rb_boolean) == T_NIL || !rb_boolean);
  return rb_boolean;
}

#eta0Object



241
242
243
# File 'ext/wapiti/native.c', line 241

static VALUE options_eta0(VALUE self) {
  return rb_float_new(get_options(self)->sgdl1.eta0);
}

#eta0=(rb_numeric) ⇒ Object



245
246
247
248
# File 'ext/wapiti/native.c', line 245

static VALUE options_set_eta0(VALUE self, VALUE rb_numeric) {
  get_options(self)->sgdl1.eta0 = NUM2DBL(rb_numeric);
  return rb_numeric;
}

#has_attribute?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/wapiti/options.rb', line 110

def has_attribute?(attribute)
  Options.attribute_names.include?(attribute)
end

#histszObject



189
190
191
# File 'ext/wapiti/native.c', line 189

static VALUE options_histsz(VALUE self) {
  return INT2FIX(get_options(self)->lbfgs.histsz);
}

#histsz=(rb_fixnum) ⇒ Object



193
194
195
196
197
198
# File 'ext/wapiti/native.c', line 193

static VALUE options_set_histsz(VALUE self, VALUE rb_fixnum) {
  Check_Type(rb_fixnum, T_FIXNUM);
  get_options(self)->lbfgs.histsz = FIX2INT(rb_fixnum);

  return rb_fixnum;
}

#jobsizeObject



163
164
165
# File 'ext/wapiti/native.c', line 163

static VALUE options_jobsize(VALUE self) {
  return INT2FIX(get_options(self)->jobsize);
}

#jobsize=(rb_fixnum) ⇒ Object



167
168
169
170
171
172
173
174
# File 'ext/wapiti/native.c', line 167

static VALUE options_set_jobsize(VALUE self, VALUE rb_fixnum) {
  opt_t *options = get_options(self);

  Check_Type(rb_fixnum, T_FIXNUM);
  options->jobsize = FIX2INT(rb_fixnum);

  return rb_fixnum;
}

#kappaObject



259
260
261
# File 'ext/wapiti/native.c', line 259

static VALUE options_kappa(VALUE self) {
  return rb_float_new(get_options(self)->bcd.kappa);
}

#kappa=(rb_numeric) ⇒ Object



263
264
265
266
# File 'ext/wapiti/native.c', line 263

static VALUE options_set_kappa(VALUE self, VALUE rb_numeric) {
  get_options(self)->bcd.kappa = NUM2DBL(rb_numeric);
  return rb_numeric;
}

#lbfgsObject



87
88
89
# File 'lib/wapiti/options.rb', line 87

def lbfgs
  attributes(:clip, :histsz, :maxls)
end

#lblpostObject Also known as: lblpost?, posterior, posterior?



362
363
364
# File 'ext/wapiti/native.c', line 362

static VALUE options_lblpost(VALUE self) {
  return get_options(self)->lblpost ? Qtrue : Qfalse;
}

#lblpost=(rb_boolean) ⇒ Object Also known as: posterior=



366
367
368
369
# File 'ext/wapiti/native.c', line 366

static VALUE options_set_lblpost(VALUE self, VALUE rb_boolean) {
  get_options(self)->lblpost = !(TYPE(rb_boolean) == T_NIL || !rb_boolean);
  return rb_boolean;
}

#maxentObject Also known as: maxent?

Boolean Accessors



308
309
310
# File 'ext/wapiti/native.c', line 308

static VALUE options_maxent(VALUE self) {
  return get_options(self)->maxent ? Qtrue : Qfalse;
}

#maxent=(rb_boolean) ⇒ Object



312
313
314
315
# File 'ext/wapiti/native.c', line 312

static VALUE options_set_maxent(VALUE self, VALUE rb_boolean) {
  get_options(self)->maxent = !(TYPE(rb_boolean) == T_NIL || !rb_boolean);
  return rb_boolean;
}

#maxiterObject Also known as: max_iterations



150
151
152
# File 'ext/wapiti/native.c', line 150

static VALUE options_maxiter(VALUE self) {
  return INT2FIX(get_options(self)->maxiter);
}

#maxiter=(rb_fixnum) ⇒ Object Also known as: max_iterations=



154
155
156
157
158
159
160
161
# File 'ext/wapiti/native.c', line 154

static VALUE options_set_maxiter(VALUE self, VALUE rb_fixnum) {
  opt_t *options = get_options(self);

  Check_Type(rb_fixnum, T_FIXNUM);
  options->maxiter = FIX2INT(rb_fixnum);

  return rb_fixnum;
}

#maxlsObject



200
201
202
# File 'ext/wapiti/native.c', line 200

static VALUE options_maxls(VALUE self) {
  return INT2FIX(get_options(self)->lbfgs.maxls);
}

#maxls=(rb_fixnum) ⇒ Object



204
205
206
207
208
209
# File 'ext/wapiti/native.c', line 204

static VALUE options_set_maxls(VALUE self, VALUE rb_fixnum) {
  Check_Type(rb_fixnum, T_FIXNUM);
  get_options(self)->lbfgs.maxls = FIX2INT(rb_fixnum);

  return rb_fixnum;
}

#modelObject



406
407
408
409
# File 'ext/wapiti/native.c', line 406

static VALUE options_model(VALUE self) {
  const char *model = get_options(self)->model;
  return rb_str_new2(model ? model : "");
}

#model=(rb_string) ⇒ Object



411
412
413
414
415
# File 'ext/wapiti/native.c', line 411

static VALUE options_set_model(VALUE self, VALUE rb_string) {
  opt_t *options = get_options(self);
  copy_string(&(options->model), rb_string);
  return rb_string;
}

#nbestObject

Fixnum Accessors



115
116
117
# File 'ext/wapiti/native.c', line 115

static VALUE options_nbest(VALUE self) {
  return INT2FIX(get_options(self)->nbest);
}

#nbest=(rb_fixnum) ⇒ Object



119
120
121
122
123
124
# File 'ext/wapiti/native.c', line 119

static VALUE options_set_nbest(VALUE self, VALUE rb_fixnum) {
  Check_Type(rb_fixnum, T_FIXNUM);
  get_options(self)->nbest = FIX2INT(rb_fixnum);

  return rb_fixnum;
}

#nthreadObject Also known as: threads



176
177
178
# File 'ext/wapiti/native.c', line 176

static VALUE options_nthread(VALUE self) {
  return INT2FIX(get_options(self)->nthread);
}

#nthread=(rb_fixnum) ⇒ Object Also known as: threads=



180
181
182
183
184
185
186
187
# File 'ext/wapiti/native.c', line 180

static VALUE options_set_nthread(VALUE self, VALUE rb_fixnum) {
  opt_t *options = get_options(self);

  Check_Type(rb_fixnum, T_FIXNUM);
  options->nthread = FIX2INT(rb_fixnum);

  return rb_fixnum;
}

#objwinObject Also known as: convergence_window



138
139
140
# File 'ext/wapiti/native.c', line 138

static VALUE options_objwin(VALUE self) {
  return INT2FIX(get_options(self)->objwin);
}

#objwin=(rb_fixnum) ⇒ Object Also known as: convergence_window=



142
143
144
145
146
147
# File 'ext/wapiti/native.c', line 142

static VALUE options_set_objwin(VALUE self, VALUE rb_fixnum) {
  Check_Type(rb_fixnum, T_FIXNUM);
  get_options(self)->objwin = FIX2INT(rb_fixnum);

  return rb_fixnum;
}

#outscObject Also known as: outsc?, score, score?



353
354
355
# File 'ext/wapiti/native.c', line 353

static VALUE options_outsc(VALUE self) {
  return get_options(self)->outsc ? Qtrue : Qfalse;
}

#outsc=(rb_boolean) ⇒ Object Also known as: score=



357
358
359
360
# File 'ext/wapiti/native.c', line 357

static VALUE options_set_outsc(VALUE self, VALUE rb_boolean) {
  get_options(self)->outsc = !(TYPE(rb_boolean) == T_NIL || !rb_boolean);
  return rb_boolean;
}

#patternObject Also known as: template

String Accessors



394
395
396
397
# File 'ext/wapiti/native.c', line 394

static VALUE options_pattern(VALUE self) {
  const char *pattern = get_options(self)->pattern;
  return rb_str_new2(pattern ? pattern : "");
}

#pattern=(rb_string) ⇒ Object Also known as: template=



399
400
401
402
403
404
# File 'ext/wapiti/native.c', line 399

static VALUE options_set_pattern(VALUE self, VALUE rb_string) {
  opt_t *options = get_options(self);
  copy_string((char**)&(options->pattern), rb_string);

  return rb_string;
}

#rho1Object

Float Accessors



214
215
216
# File 'ext/wapiti/native.c', line 214

static VALUE options_rho1(VALUE self) {
  return rb_float_new(get_options(self)->rho1);
}

#rho1=(rb_numeric) ⇒ Object



218
219
220
221
# File 'ext/wapiti/native.c', line 218

static VALUE options_set_rho1(VALUE self, VALUE rb_numeric) {
  get_options(self)->rho1 = NUM2DBL(rb_numeric);
  return rb_numeric;
}

#rho2Object



223
224
225
# File 'ext/wapiti/native.c', line 223

static VALUE options_rho2(VALUE self) {
  return rb_float_new(get_options(self)->rho2);
}

#rho2=(rb_numeric) ⇒ Object



227
228
229
230
# File 'ext/wapiti/native.c', line 227

static VALUE options_set_rho2(VALUE self, VALUE rb_numeric) {
  get_options(self)->rho2 = NUM2DBL(rb_numeric);
  return rb_numeric;
}

#rpropObject



99
100
101
# File 'lib/wapiti/options.rb', line 99

def rprop
  attributes(:stpmin, :stpmax, :stpinc, :stpdec, :cutoff)
end

#sgdl1Object



91
92
93
# File 'lib/wapiti/options.rb', line 91

def sgdl1
  attributes(:eta0, :alpha)
end

#skip_tokensObject Also known as: skip_tokens?



344
345
346
# File 'ext/wapiti/native.c', line 344

static VALUE options_label(VALUE self) {
  return get_options(self)->label ? Qtrue : Qfalse;
}

#skip_tokens=(rb_boolean) ⇒ Object



348
349
350
351
# File 'ext/wapiti/native.c', line 348

static VALUE options_set_label(VALUE self, VALUE rb_boolean) {
  get_options(self)->label = !(TYPE(rb_boolean) == T_NIL || !rb_boolean);
  return rb_boolean;
}

#sparseObject Also known as: sparse?



326
327
328
# File 'ext/wapiti/native.c', line 326

static VALUE options_sparse(VALUE self) {
  return get_options(self)->sparse ? Qtrue : Qfalse;
}

#sparse=(rb_boolean) ⇒ Object



330
331
332
333
# File 'ext/wapiti/native.c', line 330

static VALUE options_set_sparse(VALUE self, VALUE rb_boolean) {
  get_options(self)->sparse = !(TYPE(rb_boolean) == T_NIL || !rb_boolean);
  return rb_boolean;
}

#stopepsObject Also known as: stop_epsilon



232
233
234
# File 'ext/wapiti/native.c', line 232

static VALUE options_stopeps(VALUE self) {
  return rb_float_new(get_options(self)->stopeps);
}

#stopeps=(rb_numeric) ⇒ Object Also known as: stop_epsilon=



236
237
238
239
# File 'ext/wapiti/native.c', line 236

static VALUE options_set_stopeps(VALUE self, VALUE rb_numeric) {
  get_options(self)->stopeps = NUM2DBL(rb_numeric);
  return rb_numeric;
}

#stopwinObject Also known as: stop_window

Option Accessors



127
128
129
# File 'ext/wapiti/native.c', line 127

static VALUE options_stopwin(VALUE self) {
  return INT2FIX(get_options(self)->stopwin);
}

#stopwin=(rb_fixnum) ⇒ Object Also known as: stop_window=



131
132
133
134
135
136
# File 'ext/wapiti/native.c', line 131

static VALUE options_set_stopwin(VALUE self, VALUE rb_fixnum) {
  Check_Type(rb_fixnum, T_FIXNUM);
  get_options(self)->stopwin = FIX2INT(rb_fixnum);

  return rb_fixnum;
}

#stpdecObject



295
296
297
# File 'ext/wapiti/native.c', line 295

static VALUE options_stpdec(VALUE self) {
  return rb_float_new(get_options(self)->rprop.stpdec);
}

#stpdec=(rb_numeric) ⇒ Object



299
300
301
302
# File 'ext/wapiti/native.c', line 299

static VALUE options_set_stpdec(VALUE self, VALUE rb_numeric) {
  get_options(self)->rprop.stpdec = NUM2DBL(rb_numeric);
  return rb_numeric;
}

#stpincObject



286
287
288
# File 'ext/wapiti/native.c', line 286

static VALUE options_stpinc(VALUE self) {
  return rb_float_new(get_options(self)->rprop.stpinc);
}

#stpinc=(rb_numeric) ⇒ Object



290
291
292
293
# File 'ext/wapiti/native.c', line 290

static VALUE options_set_stpinc(VALUE self, VALUE rb_numeric) {
  get_options(self)->rprop.stpinc = NUM2DBL(rb_numeric);
  return rb_numeric;
}

#stpmaxObject



277
278
279
# File 'ext/wapiti/native.c', line 277

static VALUE options_stpmax(VALUE self) {
  return rb_float_new(get_options(self)->rprop.stpmax);
}

#stpmax=(rb_numeric) ⇒ Object



281
282
283
284
# File 'ext/wapiti/native.c', line 281

static VALUE options_set_stpmax(VALUE self, VALUE rb_numeric) {
  get_options(self)->rprop.stpmax = NUM2DBL(rb_numeric);
  return rb_numeric;
}

#stpminObject



268
269
270
# File 'ext/wapiti/native.c', line 268

static VALUE options_stpmin(VALUE self) {
  return rb_float_new(get_options(self)->rprop.stpmin);
}

#stpmin=(rb_numeric) ⇒ Object



272
273
274
275
# File 'ext/wapiti/native.c', line 272

static VALUE options_set_stpmin(VALUE self, VALUE rb_numeric) {
  get_options(self)->rprop.stpmin = NUM2DBL(rb_numeric);
  return rb_numeric;
}

#typeObject



428
429
430
431
# File 'ext/wapiti/native.c', line 428

static VALUE options_type(VALUE self) {
  const char *type = get_options(self)->type;
  return rb_str_new2(type ? type : "");
}

#type=(rb_string) ⇒ Object



433
434
435
436
437
# File 'ext/wapiti/native.c', line 433

static VALUE options_set_type(VALUE self, VALUE rb_string) {
  opt_t *options = get_options(self);
  copy_string((char**)&(options->type), rb_string);
  return rb_string;
}

#update(attributes = {}) ⇒ Object Also known as: update_attributes

Updates all the attributes from the passed-in hash.



72
73
74
75
76
77
78
# File 'lib/wapiti/options.rb', line 72

def update(attributes = {})
  attributes.each_pair do |k, v|
    mid = "#{k}="
    send(mid, v) if respond_to?(mid)
  end
  self
end

#update!(*args) ⇒ Object



82
83
84
85
# File 'lib/wapiti/options.rb', line 82

def update!(*args)
  update(*args)
  validate!
end

#valid?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/wapiti/options.rb', line 122

def valid?
  validate.empty?
end

#valid_algorithm?Boolean

Returns:

  • (Boolean)


114
115
116
# File 'lib/wapiti/options.rb', line 114

def valid_algorithm?
  self.class.algorithms.include?(algorithm)
end

#valid_type?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/wapiti/options.rb', line 118

def valid_type?
  self.class.types.include?(type)
end

#validateObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/wapiti/options.rb', line 127

def validate
  e = []

  %w{ threads jobsize alpha histsz maxls eta0 alpha nbest }.each do |name|
    e << "invalid value for #{name}: #{send(name)}" unless send(name) > 0
  end

  %w{ rho1 rho2 }.each do |name|
    e << "invalid value for #{name}: #{send(name)}" unless send(name) >= 0.0
  end

  e << "unknown type: #{type}" unless valid_type?
  e << "unknown algorithm: #{algorithm}" unless valid_algorithm?
  e << "BCD not supported for training maxent models" if maxent && algorithm == 'bcd'
  e
end

#validate!Object

Raises:

  • (ArgumentError)


152
153
154
155
# File 'lib/wapiti/options.rb', line 152

def validate!
  errors = validate
  raise ArgumentError, errors.join('; ') unless errors.empty?
end