Evelyn18 commited on
Commit
2b881e6
1 Parent(s): 390c9c6
Files changed (1) hide show
  1. app.py +15 -0
app.py CHANGED
@@ -46,6 +46,21 @@ def exact_match(prediction, truth):
46
  def compute_f1(prediction, truth):
47
  pred_tokens = normalize_text(prediction).split()
48
  truth_tokens = normalize_text(truth).split()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
 
51
 
 
46
  def compute_f1(prediction, truth):
47
  pred_tokens = normalize_text(prediction).split()
48
  truth_tokens = normalize_text(truth).split()
49
+
50
+ # if either the prediction or the truth is no-answer then f1 = 1 if they agree, 0 otherwise
51
+ if len(pred_tokens) == 0 or len(truth_tokens) == 0:
52
+ return int(pred_tokens == truth_tokens)
53
+
54
+ common_tokens = set(pred_tokens) & set(truth_tokens)
55
+
56
+ # if there are no common tokens then f1 = 0
57
+ if len(common_tokens) == 0:
58
+ return 0
59
+
60
+ prec = len(common_tokens) / len(pred_tokens)
61
+ rec = len(common_tokens) / len(truth_tokens)
62
+
63
+ return round(2 * (prec * rec) / (prec + rec), 2)
64
 
65
 
66