<commit_before>require 'json'

module KvgCharacterRecognition
  class JSONDatastore
    def initialize filename = 'characters.json'
      @data = load_file(filename)
      @filename = filename
    end

    def load_file filename
      begin
        JSON.parse(File.read(filename), symbolize_names: true)
      rescue
        puts "WARNING: Can't load file, returning empty character collection."
        []
      end
    end

    def characters_in_stroke_range range
      @data.select { |character| range === character[:number_of_strokes] }
    end

    def store character
      @data.push character
    end

    def persist!
      dump @filename
    end

    def dump filename
      File.write(filename, @data.to_json)
    end
  end
end<commit_msg>Add encoding utf-8 to file otherwise some systems fall back to ascii<commit_after>require 'json'

module KvgCharacterRecognition
  class JSONDatastore
    def initialize filename = 'characters.json'
      @data = load_file(filename)
      @filename = filename
    end

    def load_file filename
      begin
        JSON.parse(File.read(filename, encoding: 'utf-8'), symbolize_names: true)
      rescue
        puts "WARNING: Can't load file, returning empty character collection."
        []
      end
    end

    def characters_in_stroke_range range
      @data.select { |character| range === character[:number_of_strokes] }
    end

    def store character
      @data.push character
    end

    def persist!
      dump @filename
    end

    def dump filename
      File.write(filename, @data.to_json)
    end
  end
end
{'fraction_non_alphanumeric': 0.07106598984771574, 'fraction_numerical': 0.0012690355329949238, 'mean_word_length': 2.6004566210045663, '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': '2482264', 'n_tokens_mistral': 459, 'n_tokens_neox': 441, 'n_words': 134}