class AnyStyle::ParserCore

Attributes

defaults[R]
formats[R]
features[R]
model[R]
mtime[R]
normalizers[R]
options[R]

Public Class Methods

instance() click to toggle source

Returns a default parser instance

   # File lib/anystyle/parser.rb
13 def instance
14   Thread.current["anystyle_#{name.downcase}"] ||= new
15 end
load(path) click to toggle source
   # File lib/anystyle/parser.rb
 8 def load(path)
 9   new model: path
10 end
new(options = {}) click to toggle source
   # File lib/anystyle/parser.rb
20 def initialize(options = {})
21   @options = self.class.defaults.merge(options)
22   load_model
23 end

Public Instance Methods

check(input) click to toggle source
   # File lib/anystyle/parser.rb
51 def check(input)
52   model.check prepare(input, tagged: true)
53 end
expand(dataset) click to toggle source
   # File lib/anystyle/parser.rb
78 def expand(dataset)
79   raise NotImplementedError
80 end
label(input, **opts) click to toggle source
   # File lib/anystyle/parser.rb
47 def label(input, **opts)
48   model.label prepare(input, **opts)
49 end
learn(input) click to toggle source
   # File lib/anystyle/parser.rb
63 def learn(input)
64   train(input, truncate: false)
65 end
load_model(file = options[:model]) click to toggle source
   # File lib/anystyle/parser.rb
25 def load_model(file = options[:model])
26   unless file.nil?
27     @model = Wapiti.load(file)
28     @model.options.update_attributes options
29     @mtime = File.mtime(file)
30   else
31     @model = Wapiti::Model.new(options.reject { |k, _| k == :model })
32     @model.path = options[:model]
33     @mtime = Time.now
34   end
35 
36   self
37 end
normalize(hash, **opts) click to toggle source
   # File lib/anystyle/parser.rb
67 def normalize(hash, **opts)
68   normalizers.each do |n|
69     begin
70       hash = n.normalize(hash, **opts) unless n.skip?
71     rescue => e
72       warn "Error in #{n.name} normalizer: #{e.message}"
73     end
74   end
75   hash
76 end
prepare(input, **opts) click to toggle source
   # File lib/anystyle/parser.rb
82 def prepare(input, **opts)
83   case input
84   when Wapiti::Dataset
85     expand input
86   when Wapiti::Sequence
87     expand Wapiti::Dataset.new([input])
88   when String
89     if !input.tainted? && input.length < 1024 && File.exists?(input)
90       expand Wapiti::Dataset.open(input, **opts)
91     else
92       expand Wapiti::Dataset.parse(input, **opts)
93     end
94   else
95     expand Wapiti::Dataset.parse(input, **opts)
96   end
97 end
reload() click to toggle source
   # File lib/anystyle/parser.rb
39 def reload
40   load_model(model.path)
41 end
stale?() click to toggle source
   # File lib/anystyle/parser.rb
43 def stale?
44   File.exist?(model.path) && File.mtime(model.path) > mtime
45 end
train(input = options[:training_data], truncate: true) click to toggle source
   # File lib/anystyle/parser.rb
55 def train(input = options[:training_data], truncate: true)
56   load_model(nil) if truncate
57   unless input.nil? || input.empty?
58     model.train prepare(input, tagged: true)
59   end
60   model
61 end