Drunper commited on
Commit
6aa51b8
·
1 Parent(s): e22e6b5

First version

Browse files
Files changed (3) hide show
  1. README.md +0 -2
  2. metrica_tesi.py +26 -13
  3. tests.py +6 -6
README.md CHANGED
@@ -1,7 +1,5 @@
1
  ---
2
  title: Metrica Tesi
3
- datasets:
4
- - placeholder
5
  tags:
6
  - evaluate
7
  - metric
 
1
  ---
2
  title: Metrica Tesi
 
 
3
  tags:
4
  - evaluate
5
  - metric
metrica_tesi.py CHANGED
@@ -15,6 +15,7 @@
15
 
16
  import evaluate
17
  import datasets
 
18
 
19
 
20
  # TODO: Add BibTeX citation
@@ -36,13 +37,15 @@ This new module is designed to solve this great ML task and is crafted with a lo
36
  _KWARGS_DESCRIPTION = """
37
  Calculates how good are predictions given some references, using certain scores
38
  Args:
39
- predictions: list of predictions to score. Each predictions
40
  should be a string with tokens separated by spaces.
 
41
  references: list of reference for each prediction. Each
42
- reference should be a string with tokens separated by spaces.
 
 
43
  Returns:
44
- accuracy: description of the first score,
45
- another_score: description of the second score,
46
  Examples:
47
  Examples should be written in doctest format, and should illustrate how
48
  to use the function.
@@ -57,6 +60,10 @@ Examples:
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)
61
  class MetricaTesi(evaluate.Metric):
62
  """TODO: Short description of my evaluation module."""
@@ -70,15 +77,17 @@ class MetricaTesi(evaluate.Metric):
70
  citation=_CITATION,
71
  inputs_description=_KWARGS_DESCRIPTION,
72
  # This defines the format of each prediction and reference
73
- features=datasets.Features({
74
- 'predictions': datasets.Value('int64'),
75
- 'references': datasets.Value('int64'),
76
- }),
 
 
77
  # Homepage of the module for documentation
78
  homepage="http://module.homepage",
79
  # Additional links to the codebase or references
80
  codebase_urls=["http://github.com/path/to/codebase/of/new_module"],
81
- reference_urls=["http://path.to.reference.url/new_module"]
82
  )
83
 
84
  def _download_and_prepare(self, dl_manager):
@@ -86,10 +95,14 @@ class MetricaTesi(evaluate.Metric):
86
  # TODO: Download external resources if needed
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
- }
 
15
 
16
  import evaluate
17
  import datasets
18
+ from itertools import repeat
19
 
20
 
21
  # TODO: Add BibTeX citation
 
37
  _KWARGS_DESCRIPTION = """
38
  Calculates how good are predictions given some references, using certain scores
39
  Args:
40
+ predictions: list of predictions to score. Each prediction
41
  should be a string with tokens separated by spaces.
42
+ Special tokens must be included.
43
  references: list of reference for each prediction. Each
44
+ reference should be a string with tokens separated by spaces.
45
+ Special tokens must be included.
46
+ n: number of last tokens to be considered for the calculation.
47
  Returns:
48
+ score: accuracy score calculated on the last n action tokens of every pair prediction-reference.
 
49
  Examples:
50
  Examples should be written in doctest format, and should illustrate how
51
  to use the function.
 
60
  BAD_WORDS_URL = "http://url/to/external/resource/bad_words.txt"
61
 
62
 
63
+ def get_last_n_tokens(string_of_tokens, n):
64
+ return string_of_tokens.split(" ")[-n:]
65
+
66
+
67
  @evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
68
  class MetricaTesi(evaluate.Metric):
69
  """TODO: Short description of my evaluation module."""
 
77
  citation=_CITATION,
78
  inputs_description=_KWARGS_DESCRIPTION,
79
  # This defines the format of each prediction and reference
80
+ features=datasets.Features(
81
+ {
82
+ "predictions": datasets.Value("int64"),
83
+ "references": datasets.Value("int64"),
84
+ }
85
+ ),
86
  # Homepage of the module for documentation
87
  homepage="http://module.homepage",
88
  # Additional links to the codebase or references
89
  codebase_urls=["http://github.com/path/to/codebase/of/new_module"],
90
+ reference_urls=["http://path.to.reference.url/new_module"],
91
  )
92
 
93
  def _download_and_prepare(self, dl_manager):
 
95
  # TODO: Download external resources if needed
96
  pass
97
 
98
+ def _compute(self, predictions, references, n):
99
  """Returns the scores"""
100
  # TODO: Compute the different scores of the module
101
+ score = 0
102
+ for pair in zip(map(get_last_n_tokens, predictions, repeat(n)), map(get_last_n_tokens, references, repeat(n))):
103
+ if len(pair[0]) == 2:
104
+ score += sum(pred_token == ref_token for pred_token, ref_token in zip(*pair)) / 2
105
+ score /= len(predictions)
106
  return {
107
+ "score": score,
108
+ }
tests.py CHANGED
@@ -1,17 +1,17 @@
1
  test_cases = [
2
  {
3
- "predictions": [0, 0],
4
- "references": [1, 1],
5
  "result": {"metric_score": 0}
6
  },
7
  {
8
- "predictions": [1, 1],
9
- "references": [1, 1],
10
  "result": {"metric_score": 1}
11
  },
12
  {
13
- "predictions": [1, 0],
14
- "references": [1, 1],
15
  "result": {"metric_score": 0.5}
16
  }
17
  ]
 
1
  test_cases = [
2
  {
3
+ "predictions": ["blu red yellow", "green orange violet"],
4
+ "references": ["pizza spaghetti mandolino", "piplup prinplup empoleon"],
5
  "result": {"metric_score": 0}
6
  },
7
  {
8
+ "predictions": ["a random string", "another string maybe"],
9
+ "references": ["random random string", "string string maybe"],
10
  "result": {"metric_score": 1}
11
  },
12
  {
13
+ "predictions": ["conda search evaluate", "conda search transformers"],
14
+ "references": ["conda search evaluate", "conda install pytorch"],
15
  "result": {"metric_score": 0.5}
16
  }
17
  ]