take in this code and just rememebr it class Node: #TODO def init(self): self.children =…
- User
take in this code and just rememebr it class Node: #TODO def init(self): self.children = {} self.is_word = False self.path_cost = 0;
class Autocomplete(): def init(self, parent=None, document=""): self.root = Node() self.suggest = self.suggest_dfs #Default, change this to
suggest_dfs/ucs/bfsbased on which one you wish to use.codedef build_tree(self, document): for word in document.split(): node = self.root for char in word: if char not in node.children: node.children[char] = Node(); node = node.children[char]; node.is_word = True; - Assistant
Sure! I can remember the code you provided. Here's a summary of the key parts:
-
Node Class:
- Represents a node in a trie (prefix tree).
- Contains:
children: A dictionary to hold child nodes.is_word: A boolean indicating if the node represents the end of a word.path_cost: An integer initialized to 0 (though it seems unused here).
-
Autocomplete Class:
- Represents an autocomplete system.
- Contains:
- A root node of type
Node. - A
suggestmethod, which defaults tosuggest_dfs(this suggests that you might be implementing different search strategies like DFS, UCS, BFS).
- A root node of type
- Has a method
build_treethat:- Takes a document as input.
- Splits it into words and builds a trie structure from those words.
If you need further assistance or modifications, feel free to ask!
-
- User
TODO: suggest_ucs(prefix) What it does:
Implements the Uniform Cost Search (UCS) algorithm on the tree. Takes a prefix as input. Finds all words in the tree that start with the prefix. Prioritizes suggestions based on the frequency of characters appearing after previous characters. Your task:
Update build_tree() to store the path cost. The path cost is the inverse frequencies of that letter/char following that prefix of characters. Using the inverse of these frequencies creates a lower path cost for more frequent character sequences. Start from the node that corresponds to the last character of the prefix. Using UCS traverse the sub tree and build a list of suggestions. Run your code with the genZ.txt file and suggest_ucs() method that you just implemented with the prefix "th" and note the the autocompleted suggestions it generates in the Reports Section below. Make sure you note down the suggestions in the same order in which they are originally displayed on your screen
- Assistant
- User
Traceback (most recent call last): File "C:\Users<redacted>\Documents\GitHub\assignment-2-search-complete-\main.py", line 6, in read_file(filename, autocomplete_engine) File "C:\Users<redacted>\Documents\GitHub\assignment-2-search-complete-\utilities.py", line 9, in read_file autocomplete_engine.build_tree(document) File "C:\Users<redacted>\Documents\GitHub\assignment-2-search-complete-\autocomplete.py", line 43, in build_tree self.set_path_costs(self.root, "") File "C:\Users<redacted>\Documents\GitHub\assignment-2-search-complete-\autocomplete.py", line 49, in set_path_costs frequency_map = self.frequencies.get(prefix[-1], {}) ~~
- Assistant