class AnyStyle::Parser

Public Class Methods

new(options = {}) click to toggle source
Calls superclass method AnyStyle::ParserCore::new
    # File lib/anystyle/parser.rb
118 def initialize(options = {})
119   super(options)
120 
121   @features = [
122     Feature::Canonical.new,
123     Feature::Category.new,
124     Feature::Affix.new(size: 2),
125     Feature::Affix.new(size: 2, suffix: true),
126     Feature::Caps.new,
127     Feature::Number.new,
128     Feature::Dictionary.new(
129       dictionary: self.options[:dictionary] || Dictionary.instance
130     ),
131     Feature::Keyword.new,
132     Feature::Position.new,
133     Feature::Punctuation.new,
134     Feature::Brackets.new,
135     Feature::Terminal.new,
136     Feature::Locator.new
137   ]
138 
139   @normalizers = [
140     Normalizer::Unicode.new,
141     Normalizer::Quotes.new,
142     Normalizer::Brackets.new,
143     Normalizer::Punctuation.new,
144     Normalizer::Journal.new,
145     Normalizer::Container.new,
146     Normalizer::Edition.new,
147     Normalizer::Volume.new,
148     Normalizer::Page.new,
149     Normalizer::Date.new,
150     Normalizer::Location.new,
151     Normalizer::Locator.new,
152     Normalizer::Publisher.new,
153     Normalizer::PubMed.new,
154     Normalizer::ArXiv.new,
155     Normalizer::Names.new,
156     Normalizer::Locale.new,
157     Normalizer::Type.new
158   ]
159 end

Public Instance Methods

expand(dataset) click to toggle source
    # File lib/anystyle/parser.rb
161 def expand(dataset)
162   dataset.each do |seq|
163     seq.tokens.each_with_index do |tok, idx|
164       alpha = scrub tok.value
165       tok.observations = features.map { |f|
166         f.observe tok.value, alpha: alpha, idx: idx, seq: seq
167       }
168     end
169   end
170 end
flatten_values(hash, skip: [], spacer: ' ') click to toggle source
    # File lib/anystyle/parser.rb
178 def flatten_values(hash, skip: [], spacer: ' ')
179   hash.each_pair do |key, value|
180     unless !value.is_a?(Array) || skip.include?(key)
181       if value.length > 1 && value[0].respond_to?(:join)
182         hash[key] = value.join(spacer)
183       else
184         hash[key] = value[0]
185       end
186     end
187   end
188 end
format_hash(dataset, symbolize_keys: true) click to toggle source
    # File lib/anystyle/parser.rb
172 def format_hash(dataset, symbolize_keys: true)
173   dataset.inject([]) { |out, seq|
174     out << normalize(seq.to_h(symbolize_keys: symbolize_keys), prev: out)
175   }
176 end
parse(input, format: options[:format], **opts) click to toggle source
    # File lib/anystyle/parser.rb
194 def parse(input, format: options[:format], **opts)
195   case format.to_sym
196   when :wapiti
197     label(input, **opts)
198   when :hash, :bibtex, :citeproc, :csl
199     formatter = "format_#{format}".to_sym
200     send(formatter, label(input, **opts), **opts)
201   else
202     raise ArgumentError, "format not supported: #{format}"
203   end
204 end
prepare(input, **opts) click to toggle source
Calls superclass method AnyStyle::ParserCore#prepare
    # File lib/anystyle/parser.rb
206 def prepare(input, **opts)
207   opts[:separator] ||= options[:separator]
208   opts[:delimiter] ||= options[:delimiter]
209   input = input.join("\n") if input.is_a?(Array) && input[0].is_a?(String)
210   super(input, **opts)
211 end
rename_value(hash, name, new_name) click to toggle source
    # File lib/anystyle/parser.rb
190 def rename_value(hash, name, new_name)
191   hash[new_name] = hash.delete name if hash.key?(name)
192 end