Class: AnyStyle::Parser

Inherits:
ParserCore show all
Includes:
Format::BibTeX, Format::CSL
Defined in:
lib/anystyle/parser.rb

Constant Summary

Constants included from Format::BibTeX

Format::BibTeX::TYPES

Instance Attribute Summary

Attributes inherited from ParserCore

#features, #model, #mtime, #normalizers, #options

Instance Method Summary collapse

Methods included from Format::CSL

#dates_to_citeproc, #format_csl

Methods included from Format::BibTeX

#format_bibtex, #names_to_bibtex

Methods inherited from ParserCore

#check, instance, #label, #learn, load, #load_model, #normalize, #reload, #stale?, #train

Methods included from StringUtils

canonize, count, display_chars, display_width, indent, nnum, page_break?, scrub, strip_html, transliterate

Constructor Details

#initialize(options = {}) ⇒ Parser

Returns a new instance of Parser.


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
# File 'lib/anystyle/parser.rb', line 119

def initialize(options = {})
  super(options)

  @features = [
    Feature::Canonical.new,
    Feature::Category.new,
    Feature::Affix.new(size: 2),
    Feature::Affix.new(size: 2, suffix: true),
    Feature::Caps.new,
    Feature::Number.new,
    Feature::Dictionary.new(
      dictionary: self.options[:dictionary] || Dictionary.instance
    ),
    Feature::Keyword.new,
    Feature::Position.new,
    Feature::Punctuation.new,
    Feature::Brackets.new,
    Feature::Terminal.new,
    Feature::Locator.new
  ]

  @normalizers = [
    Normalizer::Unicode.new,
    Normalizer::Quotes.new,
    Normalizer::Brackets.new,
    Normalizer::Punctuation.new,
    Normalizer::Journal.new,
    Normalizer::Container.new,
    Normalizer::Edition.new,
    Normalizer::Volume.new,
    Normalizer::Page.new,
    Normalizer::Date.new,
    Normalizer::Location.new,
    Normalizer::Locator.new,
    Normalizer::Publisher.new,
    Normalizer::PubMed.new,
    Normalizer::ArXiv.new,
    Normalizer::Names.new,
    Normalizer::Locale.new,
    Normalizer::Type.new
  ]
end

Instance Method Details

#expand(dataset) ⇒ Object


162
163
164
165
166
167
168
169
170
171
# File 'lib/anystyle/parser.rb', line 162

def expand(dataset)
  dataset.each do |seq|
    seq.tokens.each_with_index do |tok, idx|
      alpha = scrub tok.value
      tok.observations = features.map { |f|
        f.observe tok.value, alpha: alpha, idx: idx, seq: seq
      }
    end
  end
end

#flatten_values(hash, skip: [], spacer: ' ') ⇒ Object


179
180
181
182
183
184
185
186
187
188
189
# File 'lib/anystyle/parser.rb', line 179

def flatten_values(hash, skip: [], spacer: ' ')
  hash.each_pair do |key, value|
    unless !value.is_a?(Array) || skip.include?(key)
      if value.length > 1 && value[0].respond_to?(:join)
        hash[key] = value.join(spacer)
      else
        hash[key] = value[0]
      end
    end
  end
end

#format_hash(dataset, symbolize_keys: true) ⇒ Object


173
174
175
176
177
# File 'lib/anystyle/parser.rb', line 173

def format_hash(dataset, symbolize_keys: true)
  dataset.inject([]) { |out, seq|
    out << normalize(seq.to_h(symbolize_keys: symbolize_keys), prev: out)
  }
end

#parse(input, format: options[:format], **opts) ⇒ Object


195
196
197
198
199
200
201
202
203
204
205
# File 'lib/anystyle/parser.rb', line 195

def parse(input, format: options[:format], **opts)
  case format.to_sym
  when :wapiti
    label(input, **opts)
  when :hash, :bibtex, :citeproc, :csl
    formatter = "format_#{format}".to_sym
    send(formatter, label(input, **opts), **opts)
  else
    raise ArgumentError, "format not supported: #{format}"
  end
end

#prepare(input, **opts) ⇒ Object


207
208
209
210
211
212
# File 'lib/anystyle/parser.rb', line 207

def prepare(input, **opts)
  opts[:separator] ||= options[:separator]
  opts[:delimiter] ||= options[:delimiter]
  input = input.join("\n") if input.is_a?(Array) && input[0].is_a?(String)
  super(input, **opts)
end

#rename_value(hash, name, new_name) ⇒ Object


191
192
193
# File 'lib/anystyle/parser.rb', line 191

def rename_value(hash, name, new_name)
  hash[new_name] = hash.delete name if hash.key?(name)
end