def load_tets(loadpath, limit=None): ''' description: This function loads and rebuilds the tets saved by save_tets parameters: the loadpath is the path to the file where the tets are saved return: the return is a dictionary of all tets ''' tets = {} count = 1 with open(loadpath, 'r', encoding="utf-8") as file: for stringtet in tqdm(csv.reader(file)): tetchildren = [] for stringsubtet in stringtet[1:]: stringsubtet = stringsubtet.replace('[', '').replace(']', '').split(':') stringsubtet[0] = stringsubtet[0].split(',') genres = [] for genre in stringsubtet[0][1:]: genres.append(tet.TETChild(genre)) partlist = [tet.TETChild(stringsubtet[0][0], children=genres)]\ * int(stringsubtet[1]) tetchildren = tetchildren + partlist tets[stringtet[0]] = tet.TET(stringtet[0], children=tetchildren) if limit is not None: if count >= limit: break count += 1 return tets
def test_tetchild_tostring3(self): ''' test tostring in tetchild ''' children = [tet.TETChild('action'), tet.TETChild('comedy')] test_objekt = tet.TETChild('high', children=children) self.assertEqual(test_objekt.tostring(), '[high,[[action],[comedy]]]')
def test_tetchild_construction3(self): ''' test the construction of a tetchild ''' children = [tet.TETChild('action')] test_objekt = tet.TETChild('high', children=children[0]) self.assertEqual(test_objekt.getchildren(), children)
def test_tet_count_children2(self): ''' test count_children in tet with children ''' username = '******' children = [tet.TETChild('high', children=tet.TETChild('action'))] test_objekt = tet.TET(root=username, children=children) self.assertEqual(test_objekt.count_children(), {'[high,[[action]]]': 1})
def test_tet_tostring2(self): ''' test tostring in tet with children ''' username = '******' children = [tet.TETChild('high', children=tet.TETChild('action'))] test_objekt = tet.TET(root=username, children=children) self.assertEqual(test_objekt.tostring(), '[u:1234,[[high,[[action]]]:1]]')
def test_tet_find_most_with_rating5(self): ''' test find_most_with_rating in tet ''' username = '******' children = [ tet.TETChild('low', children=tet.TETChild('action')), tet.TETChild('low', children=tet.TETChild('comedy')) ] test_objekt = tet.TET(root=username, children=children) self.assertEqual(test_objekt.find_most_with_rating('high'), [['[high,[[nohigh]]]', 0]])
def test_tet_find_tree2(self): ''' tests the find_tree where the tet does not exists ''' username = '******' children = [ tet.TETChild('high', children=tet.TETChild('action')), tet.TETChild('high', children=tet.TETChild('action')) ] test_tet = tet.TET(root=username, children=children) test_tets = {username: test_tet} self.assertNotEqual(build_tet.tet_find_tree('u:5678', test_tets), test_tet)
def test_tetchild_construction1(self): ''' test the construction of a tetchild ''' test_objekt = tet.TETChild('action') self.assertIsInstance(test_objekt, tet.TETChild) self.assertIsInstance(test_objekt, tet.TET)
def test_tet_getchildren(self): ''' test get children in tet ''' username = '******' children = [tet.TETChild('Lars')] test_objekt = tet.TET(root=username, children=children) self.assertEqual(test_objekt.getchildren(), children)
def construct_child(movieid, rating, vmoviedict): ''' description: construct_child constructs the child whoms root is a rating (low, mid or high) and its children. parameters: movieid is the movie that an edge between user and movie leads to via rating rating is the rating binding a user and a vmoviedict is the dictionary of all movies in the graph. return: this return a subTET. ''' movie = vmoviedict[movieid] genres = [] for genre in movie[3]: genres.append(tet.TETChild(genre)) if float(rating) < 2.5: child = tet.TETChild("low", children=genres) elif float(rating) > 3.5: child = tet.TETChild("high", children=genres) else: child = tet.TETChild("mid", children=genres) return child
def test_tetchild_tostring1(self): ''' test tostring in tetchild ''' test_objekt = tet.TETChild('action') self.assertEqual(test_objekt.tostring(), '[action]')