jpwahle commited on
Commit
72f0c4f
1 Parent(s): c6e9551

Update etpc.py

Browse files
Files changed (1) hide show
  1. etpc.py +67 -19
etpc.py CHANGED
@@ -16,13 +16,12 @@
16
  """ETPC: The Extended Typology Paraphrase Corpus"""
17
 
18
  import os
19
- from typing import Dict, Generator, Any, Union, Optional, List, Tuple
20
 
21
  import datasets
22
  from datasets.tasks import TextClassification
23
  from lxml import etree
24
 
25
-
26
  logger = datasets.logging.get_logger(__name__)
27
 
28
 
@@ -63,13 +62,22 @@ class ETPC(datasets.GeneratorBasedBuilder):
63
  def _info(self):
64
  features = datasets.Features(
65
  {
66
- "idx": datasets.Value("int32"),
67
  "sentence1": datasets.Value("string"),
68
  "sentence2": datasets.Value("string"),
69
  "etpc_label": datasets.Value("int8"),
70
  "mrpc_label": datasets.Value("int8"),
71
  "negation": datasets.Value("int8"),
72
- "paraphrase_types": datasets.Value("string"),
 
 
 
 
 
 
 
 
 
73
  }
74
  )
75
 
@@ -101,26 +109,66 @@ class ETPC(datasets.GeneratorBasedBuilder):
101
  parser = etree.XMLParser(encoding="utf-8", recover=True)
102
 
103
  tree_text_pairs = etree.parse(text_pairs_path, parser=parser)
104
- tree_paraphrase_types = etree.parse(paraphrase_types_path, parser=parser)
 
 
105
 
106
  root_text_pairs = tree_text_pairs.getroot()
107
  root_paraphrase_types = tree_paraphrase_types.getroot()
108
 
109
- for idx, row in enumerate(root_text_pairs):
110
- children = row.getchildren()
 
111
  current_pair_id = row.find(".//pair_id").text
112
  paraphrase_types = root_paraphrase_types.xpath(
113
  f".//pair_id[text()='{current_pair_id}']/parent::relation/type_name/text()"
114
  )
115
- str_paraphrase_types = ",".join(paraphrase_types)
116
- yield idx, {
117
- "idx": int(row.find(".//pair_id").text)
118
- if row.find(".//pair_id") is not None
119
- else int(idx),
120
- "sentence1": row.find(".//sent1_raw").text,
121
- "sentence2": row.find(".//sent2_raw").text,
122
- "etpc_label": int(row.find(".//etpc_label").text),
123
- "mrpc_label": int(row.find(".//mrpc_label").text),
124
- "negation": int(row.find(".//negation").text),
125
- "paraphrase_types": str_paraphrase_types,
126
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  """ETPC: The Extended Typology Paraphrase Corpus"""
17
 
18
  import os
19
+ from typing import Any, Dict, Generator, List, Optional, Tuple, Union
20
 
21
  import datasets
22
  from datasets.tasks import TextClassification
23
  from lxml import etree
24
 
 
25
  logger = datasets.logging.get_logger(__name__)
26
 
27
 
 
62
  def _info(self):
63
  features = datasets.Features(
64
  {
65
+ "idx": datasets.Value("string"),
66
  "sentence1": datasets.Value("string"),
67
  "sentence2": datasets.Value("string"),
68
  "etpc_label": datasets.Value("int8"),
69
  "mrpc_label": datasets.Value("int8"),
70
  "negation": datasets.Value("int8"),
71
+ "paraphrase_type": datasets.Value("string"),
72
+ "paraphrase_type_id": datasets.Value("int32"),
73
+ "sentence1_phrase_span": datasets.Sequence(
74
+ datasets.Value("int32")
75
+ ),
76
+ "sentence2_phrase_span": datasets.Sequence(
77
+ datasets.Value("int32")
78
+ ),
79
+ "sentence1_phrase_text": datasets.Value("string"),
80
+ "sentence2_phrase_text": datasets.Value("string"),
81
  }
82
  )
83
 
 
109
  parser = etree.XMLParser(encoding="utf-8", recover=True)
110
 
111
  tree_text_pairs = etree.parse(text_pairs_path, parser=parser)
112
+ tree_paraphrase_types = etree.parse(
113
+ paraphrase_types_path, parser=parser
114
+ )
115
 
116
  root_text_pairs = tree_text_pairs.getroot()
117
  root_paraphrase_types = tree_paraphrase_types.getroot()
118
 
119
+ idx = -1
120
+
121
+ for row in root_text_pairs:
122
  current_pair_id = row.find(".//pair_id").text
123
  paraphrase_types = root_paraphrase_types.xpath(
124
  f".//pair_id[text()='{current_pair_id}']/parent::relation/type_name/text()"
125
  )
126
+ paraphrase_type_ids = root_paraphrase_types.xpath(
127
+ f".//pair_id[text()='{current_pair_id}']/parent::relation/type_id/text()"
128
+ )
129
+ sentence1_phrase_text = root_paraphrase_types.xpath(
130
+ f".//pair_id[text()='{current_pair_id}']/parent::relation/s1_text/text()"
131
+ )
132
+ sentence2_phrase_text = root_paraphrase_types.xpath(
133
+ f".//pair_id[text()='{current_pair_id}']/parent::relation/s2_text/text()"
134
+ )
135
+ sentence1_phrase_spans = root_paraphrase_types.xpath(
136
+ f".//pair_id[text()='{current_pair_id}']/parent::relation/s1_scope/text()"
137
+ )
138
+ sentence2_phrase_spans = root_paraphrase_types.xpath(
139
+ f".//pair_id[text()='{current_pair_id}']/parent::relation/s2_scope/text()"
140
+ )
141
+ for (
142
+ paraphrase_type,
143
+ paraphrase_type_id,
144
+ sentence1_phrase_span,
145
+ sentence2_phrase_span,
146
+ sentence1_phrase_text,
147
+ sentence2_phrase_text,
148
+ ) in zip(
149
+ paraphrase_types,
150
+ paraphrase_type_ids,
151
+ sentence1_phrase_spans,
152
+ sentence2_phrase_spans,
153
+ sentence1_phrase_text,
154
+ sentence2_phrase_text,
155
+ ):
156
+ idx += 1
157
+ yield idx, {
158
+ "idx": row.find(".//pair_id").text
159
+ + "_"
160
+ + paraphrase_type_id
161
+ + "_"
162
+ + str(idx),
163
+ "sentence1": row.find(".//sent1_raw").text,
164
+ "sentence2": row.find(".//sent2_raw").text,
165
+ "etpc_label": int(row.find(".//etpc_label").text),
166
+ "mrpc_label": int(row.find(".//mrpc_label").text),
167
+ "negation": int(row.find(".//negation").text),
168
+ "paraphrase_type": paraphrase_type,
169
+ "paraphrase_type_id": paraphrase_type_id,
170
+ "sentence1_phrase_span": sentence1_phrase_span.split(","),
171
+ "sentence2_phrase_span": sentence2_phrase_span.split(","),
172
+ "sentence1_phrase_text": sentence1_phrase_text,
173
+ "sentence2_phrase_text": sentence2_phrase_text,
174
+ }