# A class implementing a graph where the vertices are words or roots # of words (stems, tokens), and the egdes semantic connections between # them. Vertices (words) get a score signifying their importance in a # document. Edges have a weight signifying the strength of the # connection between words. # Author: Dana Vrajitoru and ... # Class: C463/B551/I400 Artificial Intelligence, Fall 2025 class Word_Graph: # Constructor with no parameters def __init__(self): self.ids = {} # dictionary of words and indexes self.names = [] # names for each index self.scores = [] # list of importance scores by vertex index self.edges = [] # for each vertex, store all connected vertices with weights self.directed = False # Adds one vertex at a time with a given score. def add_vertex(self, name, score = 1): if self.has_vertex(name): # already exists id = self.ids[name] self.scores[id] = score else: id = len(self.scores) self.ids[name] = id self.names.append(name) self.scores.append(score) self.edges.append([]) # Checks if a vertex with the given name exists in the graph. def has_vertex(self, name): return name in self.names # Checks if an edge between the two names exists in the graph. def has_edge(self, name1, name2): if not self.has_vertex(name1) or not self.has_vertex(name2): return False id1 = self.ids[name1] id2 = self.ids[name2] for pair in self.edges[id1]: if pair[0] == id2: return True return False # Returns the weight of a given edge if is exists, 0 if not. def edge_weight(self, name1, name2): if not self.has_vertex(name1) or not self.has_vertex(name2): return 0 id1 = self.ids[name1] id2 = self.ids[name2] for pair in self.edges[id1]: if pair[0] == id2: return pair[1] return 0 # Adds an edge between the given names with a given weight. # If either name is not in the graph, a vertex is added for it. def add_edge(self, name1, name2, weight = 1): if not self.has_vertex(name1): self.add_vertex(name1) if not self.has_vertex(name2): self.add_vertex(name2) id1 = self.ids[name1] id2 = self.ids[name2] self.edges[id1].append([id2, weight]) if not self.directed: self.edges[id2].append([id1, weight]) # Adds a score to the score of an existing vertex. # If the vertex doesn't exists, it is created. def add_vertex_score(self, name, val = 1): if not self.has_vertex(name): self.add_vertex(name, val) else: self.scores[self.ids[name]] += val # add the value to existing score # Adds the value to the weight of an existing edge. # If the edge doesn't exists, it is created. # It must add it in both directions if the graph is undirected. def add_edge_score(self, name1, name2, val = 1): if not self.has_edge(name1, name2): self.add_edge(name1, name2, val) else: id1 = self.ids[name1] id2 = self.ids[name2] for pair in self.edges[id1]: if pair[0] == id2: pair[1] += val # add the value to existing weight if not self.directed: for pair in self.edges[id2]: if pair[0] == id1: pair[1] += val # add the value to existing weight # Displays the information for one vertex def print_info(self, name): if not self.has_vertex(name): print(f"Vertex {name} is not in the graph") else: print("Information for vertex", name) id = self.ids[name] print("Score:", self.scores[id]) print("Connections (Name Weight):") for pair in self.edges[id]: print(f"{self.names[pair[0]]}, {pair[1]}") # Displays the top "count" vertices in the graph with the highest score. # To be implemented by the student. def print_top(self, count): print(f"Here are the top {count} vertices by importance") # Outputs the graph to a file for long-term storage and visualization. # To be implemented by the student. def output_file(self, filename): pass # quick test for the class if __name__ == '__main__': wg = Word_Graph() wg.add_vertex("where") wg.add_vertex("nightmares") wg.add_vertex("live") wg.add_vertex("strange") wg.add_vertex("dark") wg.add_vertex("mysterious") wg.add_vertex_score("strange", 1) wg.add_vertex_score("strange", 1) wg.add_vertex_score("dark", 1) wg.add_edge("nightmare", "strange") wg.add_edge("dark", "strange") wg.add_edge_score("dark", "strange", 1) wg.add_edge_score("live", "strange", 1) wg.print_info("strange") wg.print_info("dark")