Class: AnyStyle::Parser
- Inherits:
-
ParserCore
- Object
- ParserCore
- AnyStyle::Parser
- Includes:
- Format::BibTeX, Format::CSL
- Defined in:
- lib/anystyle/parser.rb
Constant Summary
Constants included from Format::BibTeX
Instance Attribute Summary
Attributes inherited from ParserCore
#features, #model, #normalizers, #options
Instance Method Summary collapse
- #expand(dataset) ⇒ Object
- #flatten_values(hash, skip: [], spacer: ' ') ⇒ Object
- #format_hash(dataset, symbolize_keys: true) ⇒ Object
-
#initialize(options = {}) ⇒ Parser
constructor
A new instance of Parser.
- #parse(input, format: , **opts) ⇒ Object
- #prepare(input, **opts) ⇒ Object
- #rename_value(hash, name, new_name) ⇒ Object
Methods included from Format::CSL
Methods included from Format::BibTeX
#format_bibtex, #names_to_bibtex
Methods inherited from ParserCore
#check, instance, #label, #learn, load, #load_model, #normalize, #train
Methods included from StringUtils
canonize, count, display_chars, display_width, indent, page_break?, scrub, transliterate
Constructor Details
#initialize(options = {}) ⇒ Parser
Returns a new instance of Parser.
Instance Method Details
#expand(dataset) ⇒ Object
147 148 149 150 151 152 153 154 155 156 |
# File 'lib/anystyle/parser.rb', line 147 def (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
164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/anystyle/parser.rb', line 164 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
158 159 160 161 162 |
# File 'lib/anystyle/parser.rb', line 158 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: , **opts) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/anystyle/parser.rb', line 180 def parse(input, format: [: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
192 193 194 195 196 197 |
# File 'lib/anystyle/parser.rb', line 192 def prepare(input, **opts) opts[:separator] ||= [:separator] opts[:delimiter] ||= [: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
176 177 178 |
# File 'lib/anystyle/parser.rb', line 176 def rename_value(hash, name, new_name) hash[new_name] = hash.delete name if hash.key?(name) end |