brian920128 commited on
Commit
5d8c50a
1 Parent(s): 1d46df2
Files changed (2) hide show
  1. app.py +5 -1
  2. doc_retrieve_metrics.py +51 -9
app.py CHANGED
@@ -3,4 +3,8 @@ from evaluate.utils import launch_gradio_widget
3
 
4
 
5
  module = evaluate.load("brian920128/doc_retrieve_metrics")
6
- launch_gradio_widget(module)
 
 
 
 
 
3
 
4
 
5
  module = evaluate.load("brian920128/doc_retrieve_metrics")
6
+
7
+
8
+ result = module.compute(references=[[["e", ""], ["", ""]]], predictions=[[["", ""]]])
9
+ print(result)
10
+ #launch_gradio_widget(module)
doc_retrieve_metrics.py CHANGED
@@ -12,11 +12,11 @@
12
  # See the License for the specific language governing permissions and
13
  # limitations under the License.
14
  """TODO: Add a description here."""
 
15
 
16
  import evaluate
17
  import datasets
18
 
19
-
20
  # TODO: Add BibTeX citation
21
  _CITATION = """\
22
  @InProceedings{huggingface:module,
@@ -31,7 +31,6 @@ _DESCRIPTION = """\
31
  This new module is designed to solve this great ML task and is crafted with a lot of care.
32
  """
33
 
34
-
35
  # TODO: Add description of the arguments of the module here
36
  _KWARGS_DESCRIPTION = """
37
  Calculates how good are predictions given some references, using certain scores
@@ -53,8 +52,49 @@ Examples:
53
  {'accuracy': 1.0}
54
  """
55
 
56
- # TODO: Define external resources urls if needed
57
- BAD_WORDS_URL = "http://url/to/external/resource/bad_words.txt"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58
 
59
 
60
  @evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
@@ -87,9 +127,11 @@ class DocRetrieveMetrics(evaluate.Metric):
87
  pass
88
 
89
  def _compute(self, predictions, references):
90
- """Returns the scores"""
91
- # TODO: Compute the different scores of the module
92
- accuracy = sum(i == j for i, j in zip(predictions, references)) / len(predictions)
93
  return {
94
- "accuracy": accuracy,
95
- }
 
 
 
12
  # See the License for the specific language governing permissions and
13
  # limitations under the License.
14
  """TODO: Add a description here."""
15
+ from typing import List
16
 
17
  import evaluate
18
  import datasets
19
 
 
20
  # TODO: Add BibTeX citation
21
  _CITATION = """\
22
  @InProceedings{huggingface:module,
 
31
  This new module is designed to solve this great ML task and is crafted with a lot of care.
32
  """
33
 
 
34
  # TODO: Add description of the arguments of the module here
35
  _KWARGS_DESCRIPTION = """
36
  Calculates how good are predictions given some references, using certain scores
 
52
  {'accuracy': 1.0}
53
  """
54
 
55
+
56
+ def calculate_precision(
57
+ predictions: List[List[str]],
58
+ reference: List[List[str]]
59
+ ) -> float:
60
+ precision = 0
61
+ count = 0
62
+
63
+ for i, d in enumerate(reference):
64
+ if len(d) == 0:
65
+ continue
66
+
67
+ predicted_pages = predictions[i]
68
+ hits = predicted_pages.intersection(d)
69
+ if len(predicted_pages) != 0:
70
+ precision += len(hits) / len(predicted_pages)
71
+
72
+ count += 1
73
+
74
+ return precision / count
75
+
76
+
77
+ def calculate_recall(
78
+ predictions: List[List[str]],
79
+ reference: List[List[str]]
80
+ ) -> float:
81
+ recall = 0
82
+ count = 0
83
+
84
+ for i, d in enumerate(reference):
85
+ if len(d) == 0:
86
+ continue
87
+
88
+ predicted_pages = predictions[i]
89
+ hits = predicted_pages.intersection(d)
90
+ recall += len(hits) / len(d)
91
+
92
+ count += 1
93
+
94
+ return recall / count
95
+
96
+
97
+ beta = 0.7
98
 
99
 
100
  @evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
 
127
  pass
128
 
129
  def _compute(self, predictions, references):
130
+ recall = calculate_recall(predictions, references)
131
+ precision = calculate_precision(predictions, references)
132
+ f_score = (1 + beta) * precision * recall / (beta * beta)(precision + recall)
133
  return {
134
+ "f1": float(
135
+ f_score
136
+ )
137
+ }