Class: AnyStyle::ParserCore
- Inherits:
-
Object
- Object
- AnyStyle::ParserCore
show all
- Includes:
- StringUtils
- Defined in:
- lib/anystyle/parser.rb
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
canonize, count, display_chars, display_width, indent, nnum, page_break?, scrub, strip_html, transliterate
Constructor Details
#initialize(options = {}) ⇒ ParserCore
Returns a new instance of ParserCore.
20
21
22
23
24
|
# File 'lib/anystyle/parser.rb', line 20
def initialize(options = {})
def_opts = self.class.defaults || Parser.defaults
@options = def_opts.merge(options)
load_model
end
|
Class Attribute Details
.defaults ⇒ Object
Returns the value of attribute defaults.
6
7
8
|
# File 'lib/anystyle/parser.rb', line 6
def defaults
@defaults
end
|
Returns the value of attribute formats.
6
7
8
|
# File 'lib/anystyle/parser.rb', line 6
def formats
@formats
end
|
Instance Attribute Details
#features ⇒ Object
Returns the value of attribute features.
18
19
20
|
# File 'lib/anystyle/parser.rb', line 18
def features
@features
end
|
#model ⇒ Object
Returns the value of attribute model.
18
19
20
|
# File 'lib/anystyle/parser.rb', line 18
def model
@model
end
|
#mtime ⇒ Object
Returns the value of attribute mtime.
18
19
20
|
# File 'lib/anystyle/parser.rb', line 18
def mtime
@mtime
end
|
#normalizers ⇒ Object
Returns the value of attribute normalizers.
18
19
20
|
# File 'lib/anystyle/parser.rb', line 18
def normalizers
@normalizers
end
|
#options ⇒ Object
Returns the value of attribute options.
18
19
20
|
# File 'lib/anystyle/parser.rb', line 18
def options
@options
end
|
Class Method Details
.instance ⇒ Object
Returns a default parser instance
13
14
15
|
# File 'lib/anystyle/parser.rb', line 13
def instance
Thread.current["anystyle_#{name.downcase}"] ||= new
end
|
.load(path) ⇒ Object
8
9
10
|
# File 'lib/anystyle/parser.rb', line 8
def load(path)
new model: path
end
|
Instance Method Details
#check(input) ⇒ Object
52
53
54
|
# File 'lib/anystyle/parser.rb', line 52
def check(input)
model.check prepare(input, tagged: true)
end
|
#expand(dataset) ⇒ Object
79
80
81
|
# File 'lib/anystyle/parser.rb', line 79
def expand(dataset)
raise NotImplementedError
end
|
#label(input, **opts) ⇒ Object
48
49
50
|
# File 'lib/anystyle/parser.rb', line 48
def label(input, **opts)
model.label prepare(input, **opts)
end
|
#learn(input) ⇒ Object
64
65
66
|
# File 'lib/anystyle/parser.rb', line 64
def learn(input)
train(input, truncate: false)
end
|
#load_model(file = ) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
|
# File 'lib/anystyle/parser.rb', line 26
def load_model(file = options[:model])
unless file.nil?
@model = Wapiti.load(file)
@model.options.update_attributes options
@mtime = File.mtime(file)
else
@model = Wapiti::Model.new(options.reject { |k, _| k == :model })
@model.path = options[:model]
@mtime = Time.now
end
self
end
|
#normalize(hash, **opts) ⇒ Object
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/anystyle/parser.rb', line 68
def normalize(hash, **opts)
normalizers.each do |n|
begin
hash = n.normalize(hash, **opts) unless n.skip?
rescue => e
warn "Error in #{n.name} normalizer: #{e.message}"
end
end
hash
end
|
#prepare(input, **opts) ⇒ Object
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/anystyle/parser.rb', line 83
def prepare(input, **opts)
case input
when Wapiti::Dataset
expand input
when Wapiti::Sequence
expand Wapiti::Dataset.new([input])
else
expand Wapiti::Dataset.parse(input, **opts)
end
end
|
#reload ⇒ Object
40
41
42
|
# File 'lib/anystyle/parser.rb', line 40
def reload
load_model(model.path)
end
|
#stale? ⇒ Boolean
44
45
46
|
# File 'lib/anystyle/parser.rb', line 44
def stale?
File.exist?(model.path) && File.mtime(model.path) > mtime
end
|
#train(input = , truncate: true) ⇒ Object
56
57
58
59
60
61
62
|
# File 'lib/anystyle/parser.rb', line 56
def train(input = options[:training_data], truncate: true)
load_model(nil) if truncate
unless input.nil? || input.empty?
model.train prepare(input, tagged: true)
end
model
end
|