diff --git a/app/trees/controller.py b/app/trees/controller.py index 393c1fe..a0bc33a 100644 --- a/app/trees/controller.py +++ b/app/trees/controller.py @@ -174,6 +174,14 @@ def post(self, project_name: str): else: message, passed = validate_ud(None, 3, data) return { "message": message if message != '---\n' else '', "passed": passed } + else: #for the moment if sud we check only the cycles + cycle_nodes = TreeService.check_cycle(data) + if cycle_nodes: + error_message = 'Non tree structure, tokens: ' + ', '.join([str(tuple_nodes) for tuple_nodes in cycle_nodes]) + ' form a cycle' + return { "message": error_message, "passed": False } + else: + return { "message": '', "passed": True } + @api.route("//samples//trees/all") class SaveAllTreesResource(Resource): diff --git a/app/trees/service.py b/app/trees/service.py index 38fecf7..cc4bd03 100644 --- a/app/trees/service.py +++ b/app/trees/service.py @@ -12,7 +12,28 @@ VALIDATED = "validated" class TreeService: - + + @staticmethod + def check_cycle(conll): + sentence_json = sentenceConllToJson(conll) + nodes_json = sentence_json['treeJson']['nodesJson'] + + nodes_children_list = {} + for index in nodes_json: + token_head = str(nodes_json[index]['HEAD']) + if token_head not in nodes_children_list.keys(): + nodes_children_list[token_head] = [index] + else: + nodes_children_list[token_head].append(index) + + cycle_nodes = [] + for node, list_children in nodes_children_list.items(): + for child in list_children: + if child in nodes_children_list.keys() and node in nodes_children_list[child]: + cycle_nodes.append((child, node)) + + return list(set(tuple(sorted(nodes_tuple)) for nodes_tuple in cycle_nodes)) + @staticmethod def samples_to_trees(samples, sample_name): """ transforms a list of samples into a trees object """