def assign_com_keywords(g, comkwdict, attr_name): """ Takes a graph, a {community number: keywords} dict and the name of the graph attribute to add and adds the attribute to the graph """ g.graph[attr_name] = [] for com, keywords in comkwdict.items(): # Get the top keywords for this community top_com_words = tu.get_most_frequent(keywords, 20) # Add this community's keywords to list of (com_num, keywords) pairs in graph attribute if attr_name in self.graph.graph: g.graph[attr_name].append([com, top_com_words]) else: g.graph[attr_name] = [[com, top_com_words],] return g
def test_get_most_frequent(self): words = ['to', 'be', 'or', 'not', 'to', 'be'] top = tu.get_most_frequent(words, 2) self.assertTrue(len(top) == 2) self.assertTrue('to' in top and 'be' in top)