-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph_construct.py
82 lines (62 loc) · 2.3 KB
/
graph_construct.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import re
from pathlib import Path
class GraphConstruct:
def __init__(self, name: str):
self._name = name
def name(self) -> str:
return self._name.upper()
def file_name(self) -> str:
pass
def type(self) -> str:
return self.__class__.__name__
@staticmethod
def parse(file: Path) -> "Event | Entity | Relation":
if file.name.startswith("EVENT"):
result = re.search(r"EVENT\((\w+)\)", file.name)
return Event(result.group(1))
elif file.name.startswith("ENTITY"):
result = re.search(r"ENTITY\((\w+)\)", file.name)
return Entity(result.group(1))
elif file.name.startswith("RELATION"):
result = re.search(
r"RELATION\((\w+)\)_(\w+)\((\w+)\)_(\w+)\((\w+)\)",
file.name,
)
relation_name = result.group(1)
source_type = result.group(2)
source_name = result.group(3)
target_type = result.group(4)
target_name = result.group(5)
source = (
Event(source_name) if source_type == "EVENT" else Entity(source_name)
)
target = (
Event(target_name) if target_type == "EVENT" else Entity(target_name)
)
return Relation(relation_name, source, target)
class Event(GraphConstruct):
def __init__(self, name: str):
super().__init__(name)
def file_name(self) -> str:
return f"EVENT({self.name()})"
class Entity(GraphConstruct):
def __init__(self, name: str):
super().__init__(name)
def file_name(self) -> str:
return f"ENTITY({self.name()})"
class Relation(GraphConstruct):
def __init__(self, name: str, source: GraphConstruct, target: GraphConstruct):
super().__init__(name)
self._source = source
self._target = target
def file_name(self) -> str:
source = self.source().file_name()
target = self.target().file_name()
return f"RELATION({self.name()})_{source}_{target}"
def source(self) -> GraphConstruct:
return self._source
def target(self) -> GraphConstruct:
return self._target
def set_source(self, source: GraphConstruct) -> "Relation":
self._source = source
return self