Skip to content

Commit

Permalink
detect cycle for sud project
Browse files Browse the repository at this point in the history
  • Loading branch information
khansadaoudi committed Dec 6, 2024
1 parent 5f3ab68 commit a8572d6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
8 changes: 8 additions & 0 deletions app/trees/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("/<string:project_name>/samples/<string:sample_name>/trees/all")
class SaveAllTreesResource(Resource):
Expand Down
23 changes: 22 additions & 1 deletion app/trees/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand Down

0 comments on commit a8572d6

Please sign in to comment.