module PMMLConsumer

Constants

VERSION

Public Class Methods

field_type(xml) click to toggle source
# File lib/pmml_consumer.rb, line 26
def self.field_type(xml)
  xml.xpath("/xmlns:PMML/xmlns:DataDictionary/xmlns:DataField").each_with_object({}) do |child, memo|
    memo[child.attributes["name"].value] = case child.attributes["optype"].value
    when "continuous"
      child.to_h.reject! { |key| %w(name optype).include?(key) }
    when "categorical"
      data_field_h = child.to_h.reject! { |key| %w(name optype).include?(key) }
      data_field_h["values"] = child.xpath("xmlns:Value").map do |cat|
        cat.attributes["value"].value
      end
      data_field_h
    else
      raise "unknow optype: #{child.attributes['optype'].value}"
    end
  end
end
load(pmml_string, model_name: nil) click to toggle source
# File lib/pmml_consumer.rb, line 13
def self.load(pmml_string, model_name: nil)
  xml = Nokogiri::XML(pmml_string)

  ml_model = model_node(xml, model_name)

  case ml_model.name
  when 'RegressionModel'
    RegressionModel.new(ml_model, field_type(xml))
  else
    MLModel.new(ml_model, field_type(xml))
  end
end
model_node(xml, model_name) click to toggle source
# File lib/pmml_consumer.rb, line 43
def self.model_node(xml, model_name)
  if model_name.nil?
    xml.xpath("/xmlns:PMML/*[contains(name(), 'Model') or (name() = 'NeuralNetwork') or (name() = 'Scorecard')]").first
  else
    xml.xpath("/xmlns:PMML/*[@modelName='#{model_name}']").first
  end
end