Example #1
0
class Operation(object):
    
    def __init__(self, start_word, word_list):
        self.init_aplhabet()
        self.child_node_list = []

        self.start_word = start_word
        self.word_list = word_list

        self.old_tree = Bintree()
        self.queue = LinkedQ()
        self.d = {}

        self.start_node = Node(self.start_word)
        self.queue.put(self.start_node)

        self.start_word_bucket_list = self.make_buckets(self.start_word)
            
        self.dict_handler()

    
    def make_buckets(self, word):
        bucket_list = []
        for i in range(len(word)):
            bucket = word[:i] + '_' + word[i+1:]
            bucket_list.append(bucket)
        return bucket_list

    def init_aplhabet(self):
        self.alphabet = []
        for x in range(ord("a"),ord("z")+1):
            self.alphabet.append(chr(x))
        for x in ("å","ä","ö"):
            self.alphabet.append(x)

    def dict_handler(self):
        for line in self.word_list:
            word = line
            for i in range(len(word)):
                bucket = word[:i] + '_' + word[i+1:]
                if not(bucket in self.d):
                    self.d[bucket] = [] # create list with bucket as key
                    self.d[bucket].append(word)
                else:
                    self.d[bucket].append(word)
        
    def make_children(self, parent_node):
        word = parent_node.value
        bucket_list = self.make_buckets(word)
        for bucket in bucket_list:
            word_list = self.d[bucket]
            for new_word in word_list:
                child_node = Node(new_word, parent=parent_node)
                
                if self.queue.isEmpty(): 
                    self.child_node_list.append(child_node)
                
                if not(self.old_tree.exists(new_word)):
                    self.old_tree.put(new_word)
                    self.queue.put(child_node)
class Operation(object):
    def __init__(self):
        self.init_aplhabet()
        self.child_node_list = []

        self.start_word = "söt"

        self.old_tree = Bintree()
        self.queue = LinkedQ()
        self.d = {}

        self.start_node = Node(self.start_word)
        self.queue.put(self.start_node)

        self.start_word_bucket_list = self.make_buckets(self.start_word)
        self.file_reader()

    # Creates a list of bucketed words
    def make_buckets(self, word):
        bucket_list = []
        for i in range(len(word)):
            bucket = word[:i] + '_' + word[i + 1:]
            bucket_list.append(bucket)
        return bucket_list

    # Creates alphabet attribute
    def init_aplhabet(self):
        self.alphabet = []
        for x in range(ord("a"), ord("z") + 1):
            self.alphabet.append(chr(x))
        for x in ("å", "ä", "ö"):
            self.alphabet.append(x)

    # Reads file and places lines in dict sorted
    def file_reader(self):
        self.word_list = []

        file_name = "word3.txt"
        f = open(file_name, "r", encoding="utf-8")
        for line in f:
            word = line[:-1]
            for i in range(len(word)):
                bucket = word[:i] + '_' + word[i + 1:]
                if not (bucket in self.d):
                    self.d[bucket] = []  # create list with bucket as key
                    self.d[bucket].append(word)
                else:
                    self.d[bucket].append(word)
        f.close()

    # Creates children of parent_node by getting the corresponding child from graph object
    def make_children(self, parent_node):
        word = parent_node.value
        bucket_list = self.make_buckets(word)

        for bucket in bucket_list:
            word_list = self.d[bucket]

            for new_word in word_list:
                child_node = Node(new_word, parent=parent_node)

                if self.queue.isEmpty():
                    self.child_node_list.append(child_node)

                if not (self.old_tree.exists(new_word)):
                    self.old_tree.put(new_word)
                    self.queue.put(child_node)
class Operation(object):
    def __init__(self, start_time):
        self.init_aplhabet()

        # Used for file with 3 letter-words ––––––––––––––

        self.g = buildGraph("word3.txt")
        self.start_word = "söt"
        self.end_word = "sur"

        # Used for file with 5 letter-words ––––––––––––––

        # self.start_word = "anstå"
        # self.end_word = "anslå"
        # self.g = buildGraph("word5.txt")

        self.old_tree = Bintree()
        self.queue = LinkedQ()

        self.start_time = start_time

        self.start_node = Node(self.start_word)
        self.queue.put(self.start_node)

    # Creates alphabet attribute
    def init_aplhabet(self):
        self.alphabet = []
        for x in range(ord("a"), ord("z") + 1):
            self.alphabet.append(chr(x))
        for x in ("å", "ä", "ö"):
            self.alphabet.append(x)

    # Creates children of parent_node by getting the corresponding child from graph object
    def make_children(self, parent_node):
        word = parent_node.word
        parent_vertex = self.g.getVertex(
            word)  # Gets the vertex corresponding with the word

        if parent_vertex == None:
            print("Error there is no way to " + self.end_word)
            pass
        else:
            connected_vertices = parent_vertex.getConnections(
            )  # Gets the connected vertecies

            for vertex in connected_vertices:
                vertex_name = vertex.getId()  # Gets the word
                new_word = vertex_name

                child_node = Node(new_word, parent=parent_node)

                if new_word == self.end_word:
                    self.queue.put(child_node)
                    write_chain(child_node, self.start_time, self)

                if not (self.old_tree.exists(new_word)):
                    self.old_tree.put(new_word)
                    self.queue.put(child_node)

                if new_word != self.end_word and self.queue.isEmpty():
                    print("Error there is no way to " + self.end_word)
                    raise SystemExit