<commit_before>module NLP
  class Dictionary
    
    attr_accessor :tree

    def initialize(category_file=:rid,restore = true)
      state_file = File.expand_path(DICTIONARY_CACHE_DIR+".#{category_file.to_s}")
      if restore and File.exist?(state_file)
        @tree = Dictionary.restore(state_file) 
      else
        @tree = PlTrie.new
        load_categories(File.dirname(__FILE__)+"/../../dict/#{category_file.to_s}", category_file )
        store(state_file)
      end

    end

    def store( state_file )
      File.open( File.expand_path( state_file ), "w" ) do |file|
        Marshal.dump( self.tree, file )
      end
      self
    end

    def self.restore( state_file )
      File.open( File.expand_path( state_file ) ) do |file|
        Marshal.restore( file )
      end
    end

    def find(word)
        @tree.find(word)
    end

    def load_categories(category_file,type)
      category = nil
      primary = nil
      secondary = nil
      tertiary = nil

      if type == :rid
        cat_class = NLP.const_get("RIDCategory")
      else
        cat_class = NLP.const_get("LIWCCategory")<commit_msg>handling find error<commit_after>module NLP
  class Dictionary
    
    attr_accessor :tree

    def initialize(category_file=:rid,restore = true)
      state_file = File.expand_path(DICTIONARY_CACHE_DIR+".#{category_file.to_s}")
      if restore and File.exist?(state_file)
        @tree = Dictionary.restore(state_file) 
      else
        @tree = PlTrie.new
        load_categories(File.dirname(__FILE__)+"/../../dict/#{category_file.to_s}", category_file )
        store(state_file)
      end

    end

    def store( state_file )
      File.open( File.expand_path( state_file ), "w" ) do |file|
        Marshal.dump( self.tree, file )
      end
      self
    end

    def self.restore( state_file )
      File.open( File.expand_path( state_file ) ) do |file|
        Marshal.restore( file )
      end
    end

    def find(word)
      begin 
        @tree.find(word)
      rescue
        nil
      end
    end

    def load_categories(category_file,type)
      category = nil
      primary = nil
      secondary = nil
      tertiary = nil

      if type == :rid
        cat_class = NLP.const_get("RIDCategory")
      else
        cat_class = NLP.const_get("LIWCCategory")
{'fraction_non_alphanumeric': 0.10173913043478261, 'fraction_numerical': 0.0, 'mean_word_length': 2.25, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '902043', 'n_tokens_mistral': 743, 'n_tokens_neox': 725, 'n_words': 153}