From 65840e35694b2cee7dc61e4b2c3e96edeb7d3022 Mon Sep 17 00:00:00 2001
From: Malo Revel <malo.revel@irisa.fr>
Date: Thu, 25 May 2023 14:04:49 +0200
Subject: [PATCH] Add F-measure

---
 scorer.py | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/scorer.py b/scorer.py
index 569dfae..5c3b353 100644
--- a/scorer.py
+++ b/scorer.py
@@ -8,6 +8,10 @@ import os
 import sys
 from rdflib import Graph
 
+def f_measure(precision, recall):
+	if precision == 0 or recall == 0: return None
+	else: return 2/(1/precision + 1/recall)
+
 class ODRL:
 	
 	def __init__(self):
@@ -136,11 +140,16 @@ class Scores:
 	def __str__(self):
 		s = ""
 		for crit in self.scores:
+			precision = self.get_precision(crit)
+			recall = self.get_recall(crit)
 			s += f"{crit}:\n"
 			s += f'\tTP = {self.scores[crit]["tp"]}, FP = {self.scores[crit]["fp"]}, FN = {self.scores[crit]["fn"]}\n'
-			s += f'\tPrecision = {self.get_precision(crit)}, Recall = {self.get_recall(crit)}\n'
-		s += f'Total precision = {self.get_total_precision()}\n'
-		s += f'Total recall = {self.get_total_recall()}\n'
+			s += f'\tPrecision = {precision}, Recall = {recall}, F-measure = {f_measure(precision, recall)}\n'
+		precision = self.get_total_precision()
+		recall = self.get_total_recall()
+		s += f'Total precision = {precision}\n'
+		s += f'Total recall = {recall}\n'
+		s += f'Total F-measure = {f_measure(precision, recall)}\n'
 		return s
 		
 
@@ -177,7 +186,7 @@ def main_scorer(path_target, path_output):
 	
 	for fname in os.listdir(path_target):
 		if fname.endswith(".ttl"):
-		
+			print(fname)
 			odrl_target = ODRL()
 			odrl_target.parse(path_target + fname)
 			odrl_output = ODRL()
@@ -194,7 +203,7 @@ def main_scorer(path_target, path_output):
 if __name__ == "__main__":
 
 	if len(sys.argv) < 3:
-		print(f"Usage: python3 {sys.argv[0]} <path_corpus_odrl> <path_output_odrl")
+		print(f"Usage: python3 {sys.argv[0]} <path_corpus_odrl> <path_output_odrl>")
 		exit(1)
 
 	path_target = sys.argv[1]
-- 
GitLab