<commit_before>require 'psych'
require 'graphviz'
require_relative 'tree'
require_relative 'nodes/node'
require_relative 'behaviors/dottable'

module ConceptQL
  class Graph
    attr :g, :file_path, :dangler, :title, :statement, :db, :suffix
    def initialize(statement, opts = {})
      @statement = statement
      @db = opts.fetch(:db, nil)
      @dangler = opts.fetch(:dangler, false)
      @tree = opts.fetch(:tree, Tree.new)
      @title = opts.fetch(:title, nil)
      @suffix = opts.fetch(:suffix, 'pdf')
      ConceptQL::Nodes::Node.send(:include, ConceptQL::Behaviors::Dottable)
    end

    def graph_it(file_path)
      build_graph(g)
      g.output(suffix.to_sym =>  file_path + ".#{suffix}")
    end

    def g
      @g ||= begin
        opts = { type: :digraph }
        opts[:label] = title if title
        GraphViz.new(:G, opts)
      end
    end

    private
    attr :yaml, :tree, :db

    def build_graph(g)
      tree.root(self).each.with_index do |last_node, index|
        last_node.build_temp_tables(db)
        last_node.graph_it(g, db)
        if dangler
          blank_node = g.add_nodes("_#{index}")
          blank_node[:shape] = 'none'
          blank_node[:height] = 0
          blank_node[:label] = ''<commit_msg>Graph: use @graph instead of @g<commit_after>require 'psych'
require 'graphviz'
require_relative 'tree'
require_relative 'nodes/node'
require_relative 'behaviors/dottable'

module ConceptQL
  class Graph
    attr :g, :file_path, :dangler, :title, :statement, :db, :suffix
    def initialize(statement, opts = {})
      @statement = statement
      @db = opts.fetch(:db, nil)
      @dangler = opts.fetch(:dangler, false)
      @tree = opts.fetch(:tree, Tree.new)
      @title = opts.fetch(:title, nil)
      @suffix = opts.fetch(:suffix, 'pdf')
      ConceptQL::Nodes::Node.send(:include, ConceptQL::Behaviors::Dottable)
    end

    def graph_it(file_path)
      build_graph(g)
      graph.output(suffix.to_sym =>  file_path + ".#{suffix}")
    end

    def graph
      @graph ||= begin
        opts = { type: :digraph }
        opts[:label] = title if title
        GraphViz.new(:G, opts)
      end
    end

    private
    attr :yaml, :tree, :db

    def build_graph(g)
      tree.root(self).each.with_index do |last_node, index|
        last_node.build_temp_tables(db)
        last_node.graph_it(g, db)
        if dangler
          blank_node = g.add_nodes("_#{index}")
          blank_node[:shape] = 'none'
          blank_node[:height] = 0
          blank_node[:label] = ''
{'fraction_non_alphanumeric': 0.13499604117181313, 'fraction_numerical': 0.000791765637371338, 'mean_word_length': 2.7271386430678466, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6849950', 'n_tokens_mistral': 873, 'n_tokens_neox': 856, 'n_words': 185}