def self_test(): '''自己构造测试数据,检查算法运行是否正确''' # v1 v2 v3 v4 v5 v6 v7 v8 v9 v10v11v12v13v14 matrix = [ [0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0], # v1 [0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0], # v2 [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0], # v3 [0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0], # v4 [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0], # v5 [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0], # v6 [1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0], # v7 [0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0], # v8 [0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 0], # v9 [1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0], # v10 [0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0], # v11 [0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1], # v12 [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1], # v13 [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0] # v14 ] graph = apiGraph(matrix) # category数据写在了文件中 categories = [] with open('../dataset/test_category.txt', 'r', encoding='utf-8') as f: for line in f: line = line[:-1] # 去除换行符 fields = line.split('\t') categories.append(fields[1:]) keywords = ['k1', 'k2'] algorithm = GreedyMethod(graph, categories) weight, nodes = algorithm.run(keywords) print(weight) print(nodes)
def __init__(self): self.graph = apiGraph(json.load(open('../dataset/graph.json'))) self.categories = json.load(open('../dataset/api_categories.json')) self.api_dict = json.load(open('../dataset/connected_api_dict.json')) self.reverseApiDict() self.combineCategoryList() print('total vertexes:', self.graph.dimension)
def prepare_data(): ''' 加载关键词图结构,返回关键字图和类别列表 :return: ''' #加载预先生成的图结构 graph = apiGraph(json.load(open('../dataset/graph.json'))) categories = json.load(open('../dataset/api_categories.json')) category_list = json.load(open('../dataset/category_list.json')) return graph, categories, category_list
def test(): graph = apiGraph(json.load(open('../dataset/graph.json'))) categories = json.load(open('../dataset/api_categories.json')) category_list = json.load(open('../dataset/category_list.json')) minimal_steiner = MinimalSteinerTree(graph, categories) count_options = [2, 3, 4, 5, 6] num_of_options = len(count_options) keywords = ['England', 'Home Automation', 'Barcodes', 'Web Site Management', 'Metadata', 'Classifieds'] begin = time.time() min_tree = minimal_steiner.run(keywords); if min_tree is None: print('weight:%d, time:%fs' % (0, time.time() - begin)) print('weight:%d, time:%fs' % (min_tree.weight, time.time() - begin)) # test()