module Biodiversity::Parser

Parser provides a namespace for functions to parse scientific names.

Constants

POINTER_SIZE

Public Class Methods

csv_row(row) click to toggle source
# File lib/biodiversity/parser.rb, line 84
def self.csv_row(row)
  {
    id: row[0],
    verbatim: row[1],
    cardinality: row[2].to_i,
    canonical: {
      stem: row[3],
      simple: row[4],
      full: row[5]
    },
    authorship: row[6],
    year: row[7],
    quality: row[8].to_i
  }
end
output(parsed, simple) click to toggle source
# File lib/biodiversity/parser.rb, line 71
def self.output(parsed, simple)
  if simple
    parsed = parsed.force_encoding('UTF-8')
    csv = CSV.new(parsed)
    row = csv.readlines[0]
    csv_row(row)
  else
    parsed = JSON.parse(parsed, symbolize_names: true)
    parsed[:parserVersion] = Biodiversity.gnparser_version
    parsed
  end
end
parse(name, simple: false, with_cultivars: false) click to toggle source
# File lib/biodiversity/parser.rb, line 36
def self.parse(name, simple: false, with_cultivars: false)
  format = simple ? 'csv' : 'compact'
  with_details = simple ? 0 : 1
  with_cultivars = with_cultivars ? 1 : 0

  parsed, ptr = parse_go(name, format, with_details, with_cultivars)
  free_mem(ptr)
  output(parsed, simple)
end
parse_ary(ary, simple: false, with_cultivars: false) click to toggle source
# File lib/biodiversity/parser.rb, line 46
def self.parse_ary(ary, simple: false, with_cultivars: false)
  format = simple ? 'csv' : 'compact'
  with_details = simple ? 0 : 1
  with_cultivars = with_cultivars ? 1 : 0

  in_ptr = FFI::MemoryPointer.new(:pointer, ary.length)
  in_ptr.write_array_of_pointer(
    ary.map { |s| FFI::MemoryPointer.from_string(s) }
  )

  parsed, ptr = parse_ary_go(in_ptr, ary.length, format,
                             with_details, with_cultivars)
  free_mem(ptr)
  if simple
    CSV.new(parsed.force_encoding('UTF-8')).map do |row|
      csv_row(row)
    end
  else
    JSON.parse(parsed, symbolize_names: true).map do |item|
      item[:parserVersion] = Biodiversity.gnparser_version
      item
    end
  end
end