<commit_before># coding: utf-8
from collections import namedtuple
from os import path
import sys
from tempfile import mkdtemp
from time import sleep
from threading import Thread


Event = namedtuple('Event', 'name description')


CONSOLE_HELP_TEXT = '''After each step, a prompt will be shown.
On the prompt, you can just press Enter to continue to the next step.
But you can also have this commands:
(write the command you want to use and then press Enter)
* h: get help.
* g PATH_TO_PNG_IMAGE: create png with graph of the current state.
* e: run non-interactively to the end of the algorithm.
* s: show statistics of the execution (max fringe size, visited nodes).
* q: quit program.'''


class BaseViewer(object):
    def __init__(self, multiple_runs=False):
        self.multiple_runs = multiple_runs

        self.successor_color = '#DD4814'
        self.fringe_color = '#20a0c0'
        self.font_size = 11

        self.clean()

    def clean(self):
        self.last_event = None
        self.events = []

        self.stats = {
            'max_fringe_size': 0,
            'visited_nodes': 0,
        }

        self.current_fringe = []
        self.last_chosen = None
        self.last_is_goal = False
        self.last_expandeds = []
        self.last_successors = []

    def event(self, name, description):
        self.last_event = Event(name=name,
                                description=description)
        self.events.append(self.last_event)

    def started(self):
        self.event('started', 'Algorithm just started.')

    def new_iteration(self, fringe):
        self.current_fringe = fringe
        self.stats['max_fringe_size'] = max(self.stats['max_fringe_size'], len(fringe))

        description = 'New iteration with %i elements in the fringe:\n%s'
        description = description % (len(fringe), str(fringe))
        self.event('new_iteration', description)

    def chosen_node(self, node, is_goal=None):
        self.last_chosen, self.last_is_goal = node, is_goal
        self.stats['visited_nodes'] += 1

        goal_text = 'Is goal!' if is_goal else 'Not goal'
        description = 'Chosen node: %s' % node
        if is_goal is not None:
            description += '\n' + goal_text
        self.event('chosen_node', description)

    def expanded(self, nodes, successors):
        self.last_expandeds, self.last_successors = nodes, successors

        description = 'Expanded %s\nSuccessors: %s'
        description = description % (nodes, successors)
        self.event('expanded', description)

    def finished(self, fringe, node, solution_type):
        self.current_fringe = fringe
        self.solution_node = node
        self.solution_type = solution_type

        if self.multiple_runs:
            description = 'Finished one of the runs of the inner algorithm returning %s\nSolution type: %s'
        else:
            description = 'Finished algorithm returning %s\nSolution type: %s'
        description = description % (node, solution_type)

        if node is not None and node.parent is not None:
            description += '\nPath from initial to goal: %s' % str(node.path())
        self.event('finished', description)

    def no_more_runs(self, node, solution_type):
        self.solution_node = node
        self.solution_type = solution_type

        description = 'Finished all of the runs of the inner algorithm returning %s\nSolution type: %s'
        description = description % (node, solution_type)

        if node is not None and node.parent is not None:
            description += '\nPath from initial to goal: %s' % str(node.path())
        self.event('no_more_runs', description)

    def create_graph(self, image_format, image_path):
        from pydot import Dot, Edge, Node

        graph = Dot(graph_type='digraph')

        graph_nodes = {}
        graph_edges = {}
        done = set()

        def add_node(node, expanded=False, chosen=False, in_fringe=False,
                     in_successors=False):
            node_id = id(node)
            if node_id not in graph_nodes:
                label = node.state_representation()
                if hasattr(node, 'cost'):
                    label += '\nCost: %s' % node.cost
                if hasattr(node, 'heuristic'):
                    label += '\nHeuristic: %s' % node.heuristic
                if hasattr(node, 'value'):
                    label += '\nValue: %s' % node.value

                new_g_node = Node(node_id,
                                  label=label,
                                  style='filled',<commit_msg>Fixed small typos on event descriptions<commit_after># coding: utf-8
from collections import namedtuple
from os import path
import sys
from tempfile import mkdtemp
from time import sleep
from threading import Thread


Event = namedtuple('Event', 'name description')


CONSOLE_HELP_TEXT = '''After each step, a prompt will be shown.
On the prompt, you can just press Enter to continue to the next step.
But you can also have this commands:
(write the command you want to use and then press Enter)
* h: get help.
* g PATH_TO_PNG_IMAGE: create png with graph of the current state.
* e: run non-interactively to the end of the algorithm.
* s: show statistics of the execution (max fringe size, visited nodes).
* q: quit program.'''


class BaseViewer(object):
    def __init__(self, multiple_runs=False):
        self.multiple_runs = multiple_runs

        self.successor_color = '#DD4814'
        self.fringe_color = '#20a0c0'
        self.font_size = 11

        self.clean()

    def clean(self):
        self.last_event = None
        self.events = []

        self.stats = {
            'max_fringe_size': 0,
            'visited_nodes': 0,
        }

        self.current_fringe = []
        self.last_chosen = None
        self.last_is_goal = False
        self.last_expandeds = []
        self.last_successors = []

    def event(self, name, description):
        self.last_event = Event(name=name,
                                description=description)
        self.events.append(self.last_event)

    def started(self):
        self.event('started', 'Algorithm just started.')

    def new_iteration(self, fringe):
        self.current_fringe = fringe
        self.stats['max_fringe_size'] = max(self.stats['max_fringe_size'], len(fringe))

        description = 'New iteration with %i elements in the fringe:\n%s'
        description = description % (len(fringe), str(fringe))
        self.event('new_iteration', description)

    def chosen_node(self, node, is_goal=None):
        self.last_chosen, self.last_is_goal = node, is_goal
        self.stats['visited_nodes'] += 1

        goal_text = 'Is goal!' if is_goal else 'Not goal'
        description = 'Chosen node: %s' % node
        if is_goal is not None:
            description += '\n' + goal_text
        self.event('chosen_node', description)

    def expanded(self, nodes, successors):
        self.last_expandeds, self.last_successors = nodes, successors

        description = 'Expanded %s\nSuccessors: %s'
        description = description % (nodes, successors)
        self.event('expanded', description)

    def finished(self, fringe, node, solution_type):
        self.current_fringe = fringe
        self.solution_node = node
        self.solution_type = solution_type

        if self.multiple_runs:
            description = 'Finished one of the runs of the inner algorithm returning %s.\nSolution type: %s'
        else:
            description = 'Finished algorithm returning %s.\nSolution type: %s'
        description = description % (node, solution_type)

        if node is not None and node.parent is not None:
            description += '\nPath from initial to goal: %s' % str(node.path())
        self.event('finished', description)

    def no_more_runs(self, node, solution_type):
        self.solution_node = node
        self.solution_type = solution_type

        description = 'Finished all of the runs of the inner algorithm returning %s.\nSolution type: %s'
        description = description % (node, solution_type)

        if node is not None and node.parent is not None:
            description += '\nPath from initial to goal: %s' % str(node.path())
        self.event('no_more_runs', description)

    def create_graph(self, image_format, image_path):
        from pydot import Dot, Edge, Node

        graph = Dot(graph_type='digraph')

        graph_nodes = {}
        graph_edges = {}
        done = set()

        def add_node(node, expanded=False, chosen=False, in_fringe=False,
                     in_successors=False):
            node_id = id(node)
            if node_id not in graph_nodes:
                label = node.state_representation()
                if hasattr(node, 'cost'):
                    label += '\nCost: %s' % node.cost
                if hasattr(node, 'heuristic'):
                    label += '\nHeuristic: %s' % node.heuristic
                if hasattr(node, 'value'):
                    label += '\nValue: %s' % node.value

                new_g_node = Node(node_id,
                                  label=label,
                                  style='filled',
{'fraction_non_alphanumeric': 0.09732970027247956, 'fraction_numerical': 0.0030517711171662125, 'mean_word_length': 2.316226960607156, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4908216', 'n_tokens_mistral': 2697, 'n_tokens_neox': 2542, 'n_words': 776}