class GnCrossmap::ResultProcessor

Processes data received from the GN Resolver

Attributes

input[R]
writer[R]

Public Class Methods

new(writer, stats, with_classification = false) click to toggle source
# File lib/gn_crossmap/result_processor.rb, line 8
def initialize(writer, stats, with_classification = false)
  @with_classification = with_classification
  @parser = ScientificNameParser.new
  @stats = stats
  @writer = writer
  @input = {}
end

Public Instance Methods

process(results, original_data) click to toggle source
# File lib/gn_crossmap/result_processor.rb, line 16
def process(results, original_data)
  @original_data = original_data
  results.each do |result|
    res = rubyfy(result)
    res[:data].each do |d|
      d[:results].nil? ? write_empty_result(d) : write_result(d)
    end
  end
end

Private Instance Methods

canonical(name_string) click to toggle source

rubocop:enable all

# File lib/gn_crossmap/result_processor.rb, line 81
def canonical(name_string)
  parsed = @parser.parse(name_string)[:scientificName]
  return nil if parsed[:canonical].nil? || parsed[:hybrid]
  parsed[:canonical]
rescue StandardError
  @parser = ScientificNameParser.new
  nil
end
classification(result) click to toggle source

rubocop:disable Metrics/AbcSize

# File lib/gn_crossmap/result_processor.rb, line 100
def classification(result)
  return nil if result[:classification_path].to_s.strip == ""
  path = result[:classification_path].split("|")
  ranks = result[:classification_path_ranks].split("|")
  if path.size == ranks.size
    path = path.zip(ranks).map { |e| "#{e[0]}(#{e[1]})" }
  end
  path.join(", ")
end
collect_stats(datum) click to toggle source
# File lib/gn_crossmap/result_processor.rb, line 55
def collect_stats(datum)
  match_num = datum[:results].map { |d| d[:match_type] }.min
  @stats.stats[:matches][match_num] += 1
end
compile_empty_result(datum) click to toggle source
# File lib/gn_crossmap/result_processor.rb, line 38
def compile_empty_result(datum)
  res = @original_data[datum[:supplied_id]]
  res += [GnCrossmap::MATCH_TYPES[0], 0, datum[:supplied_name_string],
          nil, nil, nil, nil,
          @input[datum[:supplied_id]][:rank], nil, nil, nil, nil, nil]
  res <<  nil if @with_classification
  res
end
compile_result(datum, result, match_size) click to toggle source
# File lib/gn_crossmap/result_processor.rb, line 60
def compile_result(datum, result, match_size)
  @original_data[datum[:supplied_id]] + new_data(datum,
                                                 result, match_size)
end
matched_rank(record) click to toggle source
# File lib/gn_crossmap/result_processor.rb, line 90
def matched_rank(record)
  record[:classification_path_ranks].split("|").last
end
matched_type(record) click to toggle source
# File lib/gn_crossmap/result_processor.rb, line 94
def matched_type(record)
  GnCrossmap::MATCH_TYPES[record[:match_type]]
end
new_data(datum, result, match_size) click to toggle source

rubocop:disable Metrics/AbcSize

# File lib/gn_crossmap/result_processor.rb, line 67
def new_data(datum, result, match_size)
  synonym = result[:current_name_string] ? "synonym" : nil
  res = [matched_type(result), match_size, datum[:supplied_name_string],
         result[:name_string], canonical(datum[:supplied_name_string]),
         result[:canonical_form], result[:edit_distance],
         @input[datum[:supplied_id]][:rank], matched_rank(result), synonym,
         result[:current_name_string] || result[:name_string],
         result[:score], result[:taxon_id]]
  res << classification(result) if @with_classification
  res
end
rubyfy(result) click to toggle source
# File lib/gn_crossmap/result_processor.rb, line 28
def rubyfy(result)
  JSON.parse(result, symbolize_names: true)
end
write_empty_result(datum) click to toggle source
# File lib/gn_crossmap/result_processor.rb, line 32
def write_empty_result(datum)
  @stats.stats[:matches][0] += 1
  res = compile_empty_result(datum)
  @writer.write(res)
end
write_result(datum) click to toggle source
# File lib/gn_crossmap/result_processor.rb, line 47
def write_result(datum)
  collect_stats(datum)
  match_size = datum[:results].size
  datum[:results].each do |result|
    @writer.write(compile_result(datum, result, match_size))
  end
end