MCPcopy Create free account
hub / github.com/DataArcTech/ToG / clean_relations

Function clean_relations

ToG/freebase_func.py:60–80  ·  view source on GitHub ↗
(string, entity_id, head_relations)

Source from the content-addressed store, hash-verified

58
59
60def clean_relations(string, entity_id, head_relations):
61 pattern = r"{\s*(?P<relation>[^()]+)\s+\(Score:\s+(?P<score>[0-9.]+)\)}"
62 relations=[]
63 for match in re.finditer(pattern, string):
64 relation = match.group("relation").strip()
65 if ';' in relation:
66 continue
67 score = match.group("score")
68 if not relation or not score:
69 return False, "output uncompleted.."
70 try:
71 score = float(score)
72 except ValueError:
73 return False, "Invalid score"
74 if relation in head_relations:
75 relations.append({"entity": entity_id, "relation": relation, "score": score, "head": True})
76 else:
77 relations.append({"entity": entity_id, "relation": relation, "score": score, "head": False})
78 if not relations:
79 return False, "No relations found"
80 return True, relations
81
82
83def if_all_zero(topn_scores):

Callers 1

relation_search_pruneFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected