<commit_before>require "strategies/strategy"
require "sources/tgz_source"
require "player"
require "frame"

module TRecs
  class TrecsFileStrategy
    include Strategy
    attr_reader :input_file
    attr_reader :frames
    attr_reader :from
    attr_reader :to
    attr_reader :speed
    attr_reader :timestamps

    def initialize(options={})
      @input_file = options.fetch(:input_file)

      source = TRecs::TgzSource.new(trecs_backend: input_file)
      @frames = get_frames(source)

      @from = options.fetch(:from) { 0 }
      @from = (@from || 0).to_i

      @to = options.fetch(:to) { nil }
      @to = @to.to_i if @to

      @speed = options.fetch(:speed) { nil }
      @speed &&= @speed.to_f
    end

    def perform
      timestamps.each do |time|
        current_time(((time-from)/speed.to_f).to_i)
        current_content(frames[time])
        save_frame
      end
    end

    private

    def timestamps
      @timestamps ||= frames.keys.sort

      @timestamps = @timestamps.select { |t| t > from }
      @timestamps.unshift(from)

      if to && to > 0
        @timestamps = @timestamps.select { |t| t < to }
        @timestamps.push(to)
      end
      @timestamps
    end

    def get_frames(source)
      frames = {}
      source.read do
        json_string = source.read_entry("frames.json") || "{}"
        parsed_json = JSON.parse(json_string, symbolize_names: true)
        parsed_json.each do |time, value|
          frames[Integer(time.to_s)] = Frame(value)
        end
      end
      frames
    end
  end
end
<commit_msg>Made TrecsFileStrategy work on Xiki<commit_after>require "strategies/strategy"
require "sources/tgz_source"
require "player"
require "frame"

module TRecs
  class TrecsFileStrategy
    include Strategy
    attr_reader :input_file
    attr_reader :frames
    attr_reader :from
    attr_reader :to
    attr_reader :speed
    attr_reader :timestamps

    def initialize(options={})
      @input_file = options.fetch(:input_file)

      source = TRecs::TgzSource.new(trecs_backend: input_file)
      @frames = get_frames(source)

      @from = options.fetch(:from) { 0 }
      @from = (@from || 0).to_i

      @to = options.fetch(:to) { nil }
      @to = @to.to_i if @to

      @speed = options.fetch(:speed) { 1.0 }
      @speed = @speed.to_f
    end

    def perform
      timestamps.each do |time|
        current_time(((time-from)/speed.to_f).to_i)
        current_content(frames[time])
        save_frame
      end
    end

    private

    def timestamps
      @timestamps ||= frames.keys.sort

      @timestamps = @timestamps.select { |t| t > from }
      @timestamps.unshift(from)

      if to && to > 0
        @timestamps = @timestamps.select { |t| t < to }
        @timestamps.push(to)
      end
      @timestamps
    end

    def get_frames(source)
      frames = {}
      source.read do
        json_string = source.read_entry("frames.json") || "{}"
        parsed_json = JSON.parse(json_string, symbolize_names: true)
        parsed_json.each do |time, value|
          frames[Integer(time.to_s)] = Frame(value)
        end
      end
      frames
    end
  end
end

{'fraction_non_alphanumeric': 0.11086261980830671, 'fraction_numerical': 0.0025559105431309905, 'mean_word_length': 2.3812095032397407, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '922768', 'n_tokens_mistral': 1068, 'n_tokens_neox': 1040, 'n_words': 226}