@@ -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 ())
0 commit comments