Skip to content

Commit 21f84f0

Browse files
committed
Work on architecture
1 parent c9436dd commit 21f84f0

2 files changed

Lines changed: 25 additions & 37 deletions

File tree

semantic_matcher/matcher.py

Lines changed: 23 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,41 +14,29 @@ class SemanticMatch(BaseModel):
1414
base_semantic_id: str
1515
match_semantic_id: str
1616
score: float
17+
meta_information: Dict
1718

1819

19-
class SemanticMatcher:
20-
def __init__(
21-
self,
22-
equivalence_table: Optional[Dict[str, List[SemanticMatch]]] = None
23-
):
24-
if equivalence_table is None:
25-
equivalence_table = {}
26-
self.equivalence_table: Dict[str, List[SemanticMatch]] = equivalence_table
27-
28-
def add_semantic_match(
29-
self,
30-
base_semantic_id: str,
31-
match_semantic_id: str,
32-
score: float,
33-
) -> None:
34-
semantic_match: SemanticMatch = SemanticMatch(
35-
base_semantic_id=base_semantic_id,
36-
match_semantic_id=match_semantic_id,
37-
score=score,
38-
)
39-
if self.equivalence_table.get(base_semantic_id) is not None:
40-
self.equivalence_table[base_semantic_id].append(semantic_match)
20+
class EquivalenceTable(BaseModel):
21+
matches: Dict[str, List[SemanticMatch]]
22+
23+
def add_semantic_match(self, match: SemanticMatch) -> None:
24+
if self.matches.get(match.base_semantic_id) is not None:
25+
self.equivalence_table[match.base_semantic_id].append(match)
4126
else:
42-
self.equivalence_table[base_semantic_id] = [semantic_match]
43-
44-
def remove_semantic_match(
45-
self,
46-
semantic_match: SemanticMatch
47-
) -> None:
48-
if self.equivalence_table.get(semantic_match.base_semantic_id) is not None:
49-
self.equivalence_table.get(semantic_match.base_semantic_id).remove(semantic_match)
50-
if len(self.equivalence_table.get(semantic_match.base_semantic_id)) == 0:
51-
self.equivalence_table.pop(semantic_match.base_semantic_id)
52-
53-
def get_matches(self, semantic_id: str) -> Optional[List[SemanticMatch]]:
54-
return self.equivalence_table.get(semantic_id)
27+
self.equivalence_table[match.base_semantic_id] = [match]
28+
29+
def remove_semantic_match(self, match: SemanticMatch) -> None:
30+
if self.equivalence_table.get(match.base_semantic_id) is not None:
31+
self.equivalence_table.get(match.base_semantic_id).remove(match)
32+
if len(self.equivalence_table.get(match.base_semantic_id)) == 0:
33+
self.equivalence_table.pop(match.base_semantic_id)
34+
35+
def to_file(self, filename: str) -> None:
36+
with open(filename, "w") as file:
37+
file.write(self.model_dump_json(indent=4))
38+
39+
@classmethod
40+
def from_file(cls, filename: str) -> "EquivalenceTable":
41+
with open(filename, "r") as file:
42+
return EquivalenceTable.model_validate_json(file.read())

semantic_matcher/service.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ def __init__(self, semantic_matcher: matcher.SemanticMatcher):
1414
self.router = APIRouter()
1515
self.router.add_api_route(
1616
"/get_match",
17-
self.get_match,
17+
self.get_matches,
1818
methods=["GET"]
1919
)
2020
self.semantic_matcher: matcher.SemanticMatcher = semantic_matcher
2121

22-
def get_match(
22+
def get_matches(
2323
self,
2424
request_body: service_model.MatchRequest
2525
) -> service_model.MatchResponse:

0 commit comments

Comments
 (0)