<commit_before>require "wurfl/handset"
require "rexml/document"
begin
  require "xml"
rescue LoadError
  require "rubygems"
  require "xml"
end

module Wurfl; end

=begin
A class the handles the loading, debug printing and inserting of WURFL 
handsets into a handset DB.
=end
class Wurfl::Loader
  
  def initialize 
    @handsets = Hash::new
  end

  # A simple debuging method to print all user agents in a WURFL file
  def print_handsets_in_wurfl(wurflfile)
    file = File.new(wurflfile)
    doc = REXML::Document.new file
    doc.elements.each("wurfl/devices/device") do |element| 
      puts element.attributes["user_agent"] 
    end
  end
  
  # Returns a Hashtable of handsets and a hashtable of Fallback id and agents
  def load_wurfl(wurflfilepath)
    parse_wurfl(XML::Document.file(wurflfilepath))
  end

  def parse_xml(s)
    parse_wurfl(XML::Document.string(s))
  end

  # Prints out WURFL handsets from a hashtable
  def print_wurfl(handsets)
    handsets.each do |key,value|
      puts "********************************************\n\n"
      puts "#{key}\n"
      value.each { |key,value|   puts "#{key} = #{value}" }
    end
  end

  private
  
  def parse_wurfl(doc)
    doc.find("///devices/device").each do |element| 
      hands = nil
      wurfl_id = element.attributes["id"]  
      hands = @handsets[wurfl_id] ||= Wurfl::Handset.new(wurfl_id, element.attributes["user_agent"])
      fall_back_id = element.attributes["fall_back"]
      if fall_back_id != "root"
        hands.fallback = @handsets[fall_back_id] ||= 
          Wurfl::Handset.new("","")
      end
      
      element.find("group/capability").each do |el2|
        hands[el2.attributes["name"]] = el2.attributes["value"]
      end
    end
  
    @handsets
  end

end
<commit_msg>Cleanup of variable names and spacing<commit_after>require "wurfl/handset"
require "rexml/document"
begin
  require "xml"
rescue LoadError
  require "rubygems"
  require "xml"
end

module Wurfl; end

=begin
A class the handles the loading, debug printing and inserting of WURFL 
handsets into a handset DB.
=end
class Wurfl::Loader
  
  def initialize 
    @handsets = Hash::new
  end

  # A simple debuging method to print all user agents in a WURFL file
  def print_handsets_in_wurfl(wurflfile)
    file = File.new(wurflfile)
    doc = REXML::Document.new file
    doc.elements.each("wurfl/devices/device") do |element| 
      puts element.attributes["user_agent"] 
    end
  end
  
  # Returns a Hashtable of handsets and a hashtable of Fallback id and agents
  def load_wurfl(wurflfilepath)
    parse_wurfl(XML::Document.file(wurflfilepath))
  end

  def parse_xml(s)
    parse_wurfl(XML::Document.string(s))
  end

  # Prints out WURFL handsets from a hashtable
  def print_wurfl(handsets)
    handsets.each do |key,value|
      puts "********************************************\n\n"
      puts "#{key}\n"
      value.each { |key,value|   puts "#{key} = #{value}" }
    end
  end

  private
  
  def parse_wurfl(doc)
    doc.find("///devices/device").each do |element| 
      wurfl_id = element.attributes["id"]  
      h = @handsets[wurfl_id] ||= Wurfl::Handset.new(wurfl_id, element.attributes["user_agent"])
      fall_back_id = element.attributes["fall_back"]
      if fall_back_id != "root"
        h.fallback = @handsets[fall_back_id] ||= Wurfl::Handset.new("","")
      end
      
      element.find("group/capability").each do |capability|
        h[capability.attributes["name"]] = capability.attributes["value"]
      end
    end
  
    @handsets
  end

end

{'fraction_non_alphanumeric': 0.13346501128668173, 'fraction_numerical': 0.0008465011286681715, 'mean_word_length': 3.3021844660194173, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '467709', 'n_tokens_mistral': 1241, 'n_tokens_neox': 1226, 'n_words': 297}