<commit_before>module Atlas
  class GraphFromYaml
    def self.build(graph_yaml)
      new(graph_yaml).build_graph
    end

    def initialize(graph_yaml)
      @graph_yaml = graph_yaml
      @graph      = Refinery::Catalyst::FromTurbine.call(GraphBuilder.build)
    end

    def build_graph
      graph_nodes.each do |node, node_attributes, slot_attributes|
        node_attributes.each_pair do |attr, val|
          node.set(attr, val)
        end

        update_slots(node.slots.in, slot_attributes[:in])
        update_slots(node.slots.out, slot_attributes[:out])
      end

      edges.each do |edge|
        edge.properties.merge!(properties_for_edge(edge))
      end

      @graph
    end

    private

    def graph_nodes
      @graph.nodes.map do |node|
        attributes = @graph_yaml.fetch(:nodes)[node.key] || {}

        [ node,
          attributes.except(:in, :out),
          attributes.slice(:in, :out) ]
      end
    end

    def update_slots(slots, attributes)
      attributes.each_pair do |carrier, share_attributes|
        ref_slot = if slots.include?(carrier)
          slots.get(carrier)
        else
          slots.add(carrier)
        end

        ref_slot.properties = share_attributes
      end
    end

    def edges
      @edges ||= @graph.nodes.map { |node| node.out_edges.to_a }.flatten
    end

    def properties_for_edge(edge)
      @graph_yaml.fetch(:edges).detect { |key, _|
        key == edge.properties[:model].key
      }.last
    end
  end
end<commit_msg>Fetch values for edge from Hash instead of detecting them<commit_after>module Atlas
  class GraphFromYaml
    def self.build(graph_yaml)
      new(graph_yaml).build_graph
    end

    def initialize(graph_yaml)
      @graph_yaml = graph_yaml
      @graph      = Refinery::Catalyst::FromTurbine.call(GraphBuilder.build)
    end

    def build_graph
      graph_nodes.each do |node, node_attributes, slot_attributes|
        node_attributes.each_pair do |attr, val|
          node.set(attr, val)
        end

        update_slots(node.slots.in, slot_attributes[:in])
        update_slots(node.slots.out, slot_attributes[:out])
      end

      edges.each do |edge|
        edge.properties.merge!(properties_for_edge(edge))
      end

      @graph
    end

    private

    def graph_nodes
      @graph.nodes.map do |node|
        attributes = @graph_yaml.fetch(:nodes)[node.key] || {}

        [ node,
          attributes.except(:in, :out),
          attributes.slice(:in, :out) ]
      end
    end

    def update_slots(slots, attributes)
      attributes.each_pair do |carrier, share_attributes|
        ref_slot = if slots.include?(carrier)
          slots.get(carrier)
        else
          slots.add(carrier)
        end

        ref_slot.properties = share_attributes
      end
    end

    def edges
      @graph.nodes.map { |node| node.out_edges.to_a }.flatten
    end

    def properties_for_edge(edge)
      @graph_yaml.fetch(:edges)[edge.properties[:model].key]
    end
  end
end
{'fraction_non_alphanumeric': 0.09792780748663102, 'fraction_numerical': 0.0, 'mean_word_length': 2.329254727474972, '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': '897792', 'n_tokens_mistral': 952, 'n_tokens_neox': 919, 'n_words': 187}