<commit_before>require "chronic"
require "nokogiri"

require "tam_tam/logs"

module TamTam
  class Logs
    class Adium < Logs
      class << self
        def default_path
          "#{Dir.home}/Library/Application Support/Adium 2.0/Users/Default/Logs"
        end

        def default_matches(path)
          Dir["#{path}/**/*.xml"]
        end
      end

      def as(*accounts)
        add_filter(Dir["#{path}/*\.{#{accounts.join(',')}}/**/*.xml"])
      end

      def with(*participants)
        add_filter(Dir["#{path}/*\.*/{#{participants.join(',')}}/**/*.xml"])
      end

      def on(start)
        start = Chronic.parse(start.to_s)

        add_filter(Dir["#{path}/**/*#{start.to_date.to_s}*/*.xml"])
      end

      def between(start, stop)
        start = Chronic.parse(start.to_s).to_date
        stop = Chronic.parse(stop.to_s).to_date

        filter_matches = Dir["#{path}/**/*.xml"].select do |log|
          match = log.match(/(\d{4})-(\d{2})-(\d{2})/)
          log_date = Time.new(match[1], match[2], match[3]).to_date
          log_date >= start && log_date <= stop
        end

        add_filter(filter_matches)
      end

      def messages
        return @messages if @messages

        data = matches.map do |log_path|
          xml = File.read(log_path)
          doc = Nokogiri.parse(xml)

          doc.css("chat message").map do |message|
            {
              sender: message.attribute("sender").value,
              text: message.text,
              time: Chronic.parse(message.attribute("time").value)
            }
          end
        end.flatten

        @messages = MessageSet.new(data)
      end
    end
  end
end<commit_msg>Extract message loading logic into a private method.<commit_after>require "chronic"
require "nokogiri"

require "tam_tam/logs"

module TamTam
  class Logs
    class Adium < Logs
      class << self
        def default_path
          "#{Dir.home}/Library/Application Support/Adium 2.0/Users/Default/Logs"
        end

        def default_matches(path)
          Dir["#{path}/**/*.xml"]
        end
      end

      def as(*accounts)
        add_filter(Dir["#{path}/*\.{#{accounts.join(',')}}/**/*.xml"])
      end

      def with(*participants)
        add_filter(Dir["#{path}/*\.*/{#{participants.join(',')}}/**/*.xml"])
      end

      def on(start)
        start = Chronic.parse(start.to_s)

        add_filter(Dir["#{path}/**/*#{start.to_date.to_s}*/*.xml"])
      end

      def between(start, stop)
        start = Chronic.parse(start.to_s).to_date
        stop = Chronic.parse(stop.to_s).to_date

        filter_matches = Dir["#{path}/**/*.xml"].select do |log|
          match = log.match(/(\d{4})-(\d{2})-(\d{2})/)
          log_date = Time.new(match[1], match[2], match[3]).to_date
          log_date >= start && log_date <= stop
        end

        add_filter(filter_matches)
      end

      def messages
        return @messages if @messages

        @messages = MessageSet.new(load_messages)
      end

      private

      def load_messages
        matches.map do |log_path|
          xml = File.read(log_path)
          doc = Nokogiri.parse(xml)

          doc.css("chat message").map do |message|
            {
              sender: message.attribute("sender").value,
              text: message.text,
              time: Chronic.parse(message.attribute("time").value)
            }
          end
        end.flatten
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.15492137449039023, 'fraction_numerical': 0.004659289458357601, 'mean_word_length': 1.935897435897436, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 14}, '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': '2100738', 'n_tokens_mistral': 1127, 'n_tokens_neox': 1100, 'n_words': 201}