1111# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212# See the License for the specific language governing permissions and
1313# limitations under the License.
14-
15- from typing import Dict
14+ from typing import List , Dict , Tuple , Set
1615
1716SYMMETRIC_RELATIONS = {"equivalentclass" , "sameas" , "disjointwith" }
1817
19- def text2onto_metrics (y_true , y_pred , similarity_threshold : float = 0.8 ) -> Dict :
20- def jaccard_similarity (a , b ) :
18+ def text2onto_metrics (y_true : List [ str ] , y_pred : List [ str ] , similarity_threshold : float = 0.8 ) -> Dict [ str , float | int ] :
19+ def jaccard_similarity (a : str , b : str ) -> float :
2120 set_a = set (a .lower ().split ())
2221 set_b = set (b .lower ().split ())
2322 if not set_a and not set_b :
@@ -46,10 +45,13 @@ def jaccard_similarity(a, b):
4645 return {
4746 "f1_score" : f1_score ,
4847 "precision" : precision ,
49- "recall" : recall
48+ "recall" : recall ,
49+ "total_correct" : total_correct ,
50+ "total_predicted" : total_predicted ,
51+ "total_ground_truth" : total_ground_truth
5052 }
5153
52- def term_typing_metrics (y_true , y_pred ) -> Dict :
54+ def term_typing_metrics (y_true : List [ Dict [ str , List [ str ]]], y_pred : List [ Dict [ str , List [ str ]]] ) -> Dict [ str , float | int ] :
5355 """
5456 Compute precision, recall, and F1-score for term typing
5557 using (term, type) pair-level matching instead of ID-based lookups.
@@ -77,13 +79,17 @@ def term_typing_metrics(y_true, y_pred) -> Dict:
7779 precision = total_correct / total_predicted if total_predicted > 0 else 0.0
7880 recall = total_correct / total_ground_truth if total_ground_truth > 0 else 0.0
7981 f1_score = (2 * precision * recall ) / (precision + recall ) if (precision + recall ) > 0 else 0.0
82+
8083 return {
8184 "f1_score" : f1_score ,
8285 "precision" : precision ,
83- "recall" : recall
86+ "recall" : recall ,
87+ "total_correct" : total_correct ,
88+ "total_predicted" : total_predicted ,
89+ "total_ground_truth" : total_ground_truth
8490 }
8591
86- def taxonomy_discovery_metrics (y_true , y_pred ) -> Dict :
92+ def taxonomy_discovery_metrics (y_true : List [ Dict [ str , str ]], y_pred : List [ Dict [ str , str ]] ) -> Dict [ str , float | int ] :
8793 total_predicted = len (y_pred )
8894 total_ground_truth = len (y_true )
8995 # Convert ground truth and predictions to sets of tuples for easy comparison
@@ -102,18 +108,22 @@ def taxonomy_discovery_metrics(y_true, y_pred) -> Dict:
102108 return {
103109 "f1_score" : f1_score ,
104110 "precision" : precision ,
105- "recall" : recall
111+ "recall" : recall ,
112+ "total_correct" : total_correct ,
113+ "total_predicted" : total_predicted ,
114+ "total_ground_truth" : total_ground_truth
106115 }
107116
108- def non_taxonomic_re_metrics (y_true , y_pred ) -> Dict :
109- def normalize_triple (item ):
117+
118+ def non_taxonomic_re_metrics (y_true : List [Dict [str , str ]], y_pred : List [Dict [str , str ]]) -> Dict [str , float | int ]:
119+ def normalize_triple (item : Dict [str , str ]) -> Tuple [str , str , str ]:
110120 return (
111121 item ["head" ].strip ().lower (),
112122 item ["relation" ].strip ().lower (),
113123 item ["tail" ].strip ().lower ()
114124 )
115125
116- def expand_symmetric (triples ) :
126+ def expand_symmetric (triples : Set [ Tuple [ str , str , str ]]) -> Set [ Tuple [ str , str , str ]] :
117127 expanded = set ()
118128 for h , r , t in triples :
119129 expanded .add ((h , r , t ))
@@ -136,5 +146,8 @@ def expand_symmetric(triples):
136146 return {
137147 "f1_score" : f1_score ,
138148 "precision" : precision ,
139- "recall" : recall
149+ "recall" : recall ,
150+ "total_correct" : total_correct ,
151+ "total_predicted" : total_predicted ,
152+ "total_ground_truth" : total_ground_truth
140153 }
0 commit comments