Ikala-allen commited on
Commit
cb05f51
1 Parent(s): 370291f

Update relation_extraction.py

Browse files
Files changed (1) hide show
  1. relation_extraction.py +30 -49
relation_extraction.py CHANGED
@@ -2,40 +2,32 @@ import evaluate
2
  import datasets
3
  import numpy as np
4
 
5
- # Add BibTeX citation
6
  _CITATION = """\
7
- @Paper{
8
- author = {Bruno Taillé, Vincent Guigue, Geoffrey Scoutheeten, Patrick Gallinari},
9
- title = {Let's Stop Incorrect Comparisons in End-to-end Relation Extraction!},
10
- year = {2020},
11
- link = https://arxiv.org/abs/2009.10684
12
  }
13
  """
14
 
15
- # Add description of the module here
16
  _DESCRIPTION = """\
17
- This metric is used for evaluating the quality of relation extraction output. By calculating the Micro and Macro F1 score of every relation extraction outputs to ensure the quality.
18
  """
19
 
20
 
21
- # Add description of the arguments of the module here
22
  _KWARGS_DESCRIPTION = """
23
- Calculates how good are predictions given some references, using Micro and Macro F1 scores
24
  Args:
25
- predictions: list of list of dictionary, including relation and its type
26
- dictionary should be key value pair like entity name link to its type
27
- references: list of list of dictionary, including relation and its type
28
- dictionary should be entity name pair like entity name link to its type
29
  Returns:
30
- evaluation result:
31
- - **sell** (`dictionary`): score of type sell
32
- - **tp** : true positive count
33
- - **fp** : false positive count
34
- - **fn** : false negative count
35
- - **p** : precision
36
- - **r** : recall
37
- - **f1** : micro f1 score
38
- - **ALL** (`dictionary`): score of all of the type (sell and belongs to)
39
  - **tp** : true positive count
40
  - **fp** : false positive count
41
  - **fn** : false negative count
@@ -46,25 +38,15 @@ Returns:
46
  - **Macro_p** : macro precision
47
  - **Macro_r** : macro recall
48
  Examples:
49
- >>> metric_path = "Ikala-allen/relation_extraction"
50
- >>> module = evaluate.load(metric_path)
51
- >>> references = [
52
- ... [
53
- ... {"head": "phip igments", "head_type": "brand", "type": "sell", "tail": "國際認證之色乳", "tail_type": "product"},
54
- ... {"head": "tinadaviespigments", "head_type": "brand", "type": "sell", "tail": "國際認證之色乳", "tail_type": "product"},
55
- ... ]
56
- ... ]
57
- >>> predictions = [
58
- ... [
59
- ... {"head": "phipigments", "head_type": "product", "type": "sell", "tail": "國際認證之色乳", "tail_type": "product"},
60
- ... {"head": "tinadaviespigments", "head_type": "brand", "type": "sell", "tail": "國際認證之色乳", "tail_type": "product"},
61
- ... ]
62
- ... ]
63
- >>> evaluation_scores = module.compute(predictions=predictions, references=references)
64
- >>> print(evaluation_scores)
65
- {'sell': {'tp': 1, 'fp': 1, 'fn': 1, 'p': 50.0, 'r': 50.0, 'f1': 50.0}, 'ALL': {'tp': 1, 'fp': 1, 'fn': 1, 'p': 50.0, 'r': 50.0, 'f1': 50.0, 'Macro_f1': 50.0, 'Macro_p': 50.0, 'Macro_r': 50.0}}
66
  """
67
 
 
68
  def convert_format(data:list):
69
  """
70
  Args:
@@ -75,12 +57,13 @@ def convert_format(data:list):
75
  'head_type': ['product', 'brand'...],
76
  'type': ['sell', 'sell'...],
77
  'tail': ['國際認證之色乳', '國際認證之色乳'...],
78
- 'tail_type': ['product', 'product'...]}, # first element
 
79
  {'head': ['SABONTAIWAN', 'SNTAIWAN'...],
80
  'head_type': ['brand', 'brand'...],
81
  'type': ['sell', 'sell'...],
82
  'tail': ['大馬士革玫瑰有機光燦系列', '大馬士革玫瑰有機光燦系列'...],
83
- 'tail_type': ['product', 'product'...]} # second element
84
  ...
85
  ]
86
  """
@@ -101,12 +84,7 @@ def convert_format(data:list):
101
 
102
  @evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
103
  class relation_extraction(evaluate.Metric):
104
- """
105
- evaluation metric of relation extraction
106
- inputs:
107
- predictions : (`list` of `list`s of `dictionary`s) about relation and its type of prediction
108
- references : (`list` of `list`s of `dictionary`s) about references for each relation and its type.
109
- """
110
 
111
  def _info(self):
112
  # TODO: Specifies the evaluate.EvaluationModuleInfo object
@@ -145,7 +123,7 @@ class relation_extraction(evaluate.Metric):
145
  # TODO: Download external resources if needed
146
  pass
147
 
148
- def _compute(self, predictions, references, mode="strict", relation_types=[]):
149
  """Returns the scores"""
150
  # TODO: Compute the different scores of the module
151
 
@@ -229,4 +207,7 @@ class relation_extraction(evaluate.Metric):
229
  scores["ALL"]["Macro_p"] = np.mean([scores[ent_type]["p"] for ent_type in relation_types])
230
  scores["ALL"]["Macro_r"] = np.mean([scores[ent_type]["r"] for ent_type in relation_types])
231
 
 
 
 
232
  return scores
 
2
  import datasets
3
  import numpy as np
4
 
5
+ # TODO: Add BibTeX citation
6
  _CITATION = """\
7
+ @InProceedings{huggingface:module,
8
+ title = {A great new module},
9
+ authors={huggingface, Inc.},
10
+ year={2020}
 
11
  }
12
  """
13
 
14
+ # TODO: Add description of the module here
15
  _DESCRIPTION = """\
16
+ This new module is designed to solve this great ML task and is crafted with a lot of care.
17
  """
18
 
19
 
20
+ # TODO: Add description of the arguments of the module here
21
  _KWARGS_DESCRIPTION = """
22
+ Calculates how good are predictions given some references, using certain scores
23
  Args:
24
+ predictions (list of list of dictionary): relation and its type of prediction
25
+
26
+ references (list of list of dictionary): references for each relation and its type
27
+
28
  Returns:
29
+ **output** (`dictionary` of `dictionary`s) with multiple key-value pairs
30
+ - **entity type** (`dictionary`): score of all of the type
 
 
 
 
 
 
 
31
  - **tp** : true positive count
32
  - **fp** : false positive count
33
  - **fn** : false negative count
 
38
  - **Macro_p** : macro precision
39
  - **Macro_r** : macro recall
40
  Examples:
41
+ Examples should be written in doctest format, and should illustrate how
42
+ to use the function.
43
+ my_new_module = evaluate.load("my_new_module")
44
+ results = my_new_module.compute(references=[0, 1], predictions=[0, 1])
45
+ print(results)
46
+ {'accuracy': 1.0}
 
 
 
 
 
 
 
 
 
 
 
47
  """
48
 
49
+
50
  def convert_format(data:list):
51
  """
52
  Args:
 
57
  'head_type': ['product', 'brand'...],
58
  'type': ['sell', 'sell'...],
59
  'tail': ['國際認證之色乳', '國際認證之色乳'...],
60
+ 'tail_type': ['product', 'product'...]},
61
+
62
  {'head': ['SABONTAIWAN', 'SNTAIWAN'...],
63
  'head_type': ['brand', 'brand'...],
64
  'type': ['sell', 'sell'...],
65
  'tail': ['大馬士革玫瑰有機光燦系列', '大馬士革玫瑰有機光燦系列'...],
66
+ 'tail_type': ['product', 'product'...]}
67
  ...
68
  ]
69
  """
 
84
 
85
  @evaluate.utils.file_utils.add_start_docstrings(_DESCRIPTION, _KWARGS_DESCRIPTION)
86
  class relation_extraction(evaluate.Metric):
87
+ """TODO: Short description of my evaluation module."""
 
 
 
 
 
88
 
89
  def _info(self):
90
  # TODO: Specifies the evaluate.EvaluationModuleInfo object
 
123
  # TODO: Download external resources if needed
124
  pass
125
 
126
+ def _compute(self, predictions, references, mode, only_all=True, relation_types=[]):
127
  """Returns the scores"""
128
  # TODO: Compute the different scores of the module
129
 
 
207
  scores["ALL"]["Macro_p"] = np.mean([scores[ent_type]["p"] for ent_type in relation_types])
208
  scores["ALL"]["Macro_r"] = np.mean([scores[ent_type]["r"] for ent_type in relation_types])
209
 
210
+ if only_all:
211
+ return scores["ALL"]
212
+
213
  return scores