Commit
·
620fe76
1
Parent(s):
7a380ae
Adding the "force_extract" feature.
Browse files
hupd.py
CHANGED
|
@@ -81,6 +81,7 @@ class PatentsConfig(datasets.BuilderConfig):
|
|
| 81 |
query_string: str = None,
|
| 82 |
val_set_balancer=False,
|
| 83 |
uniform_split=False,
|
|
|
|
| 84 |
**kwargs
|
| 85 |
):
|
| 86 |
"""
|
|
@@ -99,6 +100,7 @@ class PatentsConfig(datasets.BuilderConfig):
|
|
| 99 |
train_filing_end_date: End date for patents in train set
|
| 100 |
val_filing_start_date: Start date for patents in val set
|
| 101 |
val_filing_end_date: End date for patents in val set (and train set if random split is used)
|
|
|
|
| 102 |
**kwargs: keyword arguments forwarded to super
|
| 103 |
"""
|
| 104 |
super().__init__(**kwargs)
|
|
@@ -114,6 +116,7 @@ class PatentsConfig(datasets.BuilderConfig):
|
|
| 114 |
self.query_string = query_string
|
| 115 |
self.val_set_balancer = val_set_balancer
|
| 116 |
self.uniform_split = uniform_split
|
|
|
|
| 117 |
|
| 118 |
|
| 119 |
class Patents(datasets.GeneratorBasedBuilder):
|
|
@@ -198,6 +201,29 @@ class Patents(datasets.GeneratorBasedBuilder):
|
|
| 198 |
# Filter metadata based on arbitrary query string
|
| 199 |
if self.config.query_string:
|
| 200 |
df = df.query(self.config.query_string)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
# Train-validation split (either uniform or by date)
|
| 203 |
if self.config.uniform_split:
|
|
@@ -291,7 +317,7 @@ class Patents(datasets.GeneratorBasedBuilder):
|
|
| 291 |
decision = x.decision
|
| 292 |
yield id_, {
|
| 293 |
"patent_number": application_number,
|
| 294 |
-
"decision": decision,
|
| 295 |
"title": patent["title"],
|
| 296 |
"abstract": patent["abstract"],
|
| 297 |
"claims": patent["claims"],
|
|
|
|
| 81 |
query_string: str = None,
|
| 82 |
val_set_balancer=False,
|
| 83 |
uniform_split=False,
|
| 84 |
+
force_extract=False,
|
| 85 |
**kwargs
|
| 86 |
):
|
| 87 |
"""
|
|
|
|
| 100 |
train_filing_end_date: End date for patents in train set
|
| 101 |
val_filing_start_date: Start date for patents in val set
|
| 102 |
val_filing_end_date: End date for patents in val set (and train set if random split is used)
|
| 103 |
+
force_extract: Extract only the relevant years if this parameter is used.
|
| 104 |
**kwargs: keyword arguments forwarded to super
|
| 105 |
"""
|
| 106 |
super().__init__(**kwargs)
|
|
|
|
| 116 |
self.query_string = query_string
|
| 117 |
self.val_set_balancer = val_set_balancer
|
| 118 |
self.uniform_split = uniform_split
|
| 119 |
+
self.force_extract = force_extract
|
| 120 |
|
| 121 |
|
| 122 |
class Patents(datasets.GeneratorBasedBuilder):
|
|
|
|
| 201 |
# Filter metadata based on arbitrary query string
|
| 202 |
if self.config.query_string:
|
| 203 |
df = df.query(self.config.query_string)
|
| 204 |
+
|
| 205 |
+
if self.config.force_extract:
|
| 206 |
+
if self.config.name == 'all':
|
| 207 |
+
if self.config.train_filing_start_date and self.config.val_filing_end_date:
|
| 208 |
+
if self.config.train_filing_end_date and self.config.val_filing_start_date:
|
| 209 |
+
training_year_range = set(range(int(self.config.train_filing_start_date[:4]), int(self.config.train_filing_end_date[:4]) + 1))
|
| 210 |
+
validation_year_range = set(range(int(self.config.val_filing_start_date[:4]), int(self.config.val_filing_end_date[:4]) + 1))
|
| 211 |
+
full_year_range = training_year_range.union(validation_year_range)
|
| 212 |
+
else:
|
| 213 |
+
full_year_range = set(range(int(self.config.train_filing_start_date[:4]), int(self.config.val_filing_end_date[:4]) + 1))
|
| 214 |
+
else:
|
| 215 |
+
full_year_range = set(range(2004, 2019))
|
| 216 |
+
|
| 217 |
+
|
| 218 |
+
import tarfile
|
| 219 |
+
for year in full_year_range:
|
| 220 |
+
tar_file_path = f'{json_dir}/{year}.tar.gz'
|
| 221 |
+
print(f'Extracting {tar_file_path}')
|
| 222 |
+
# open file
|
| 223 |
+
tar_file = tarfile.open(tar_file_path)
|
| 224 |
+
# extracting file
|
| 225 |
+
tar_file.extractall(f'{json_dir}')
|
| 226 |
+
tar_file.close()
|
| 227 |
|
| 228 |
# Train-validation split (either uniform or by date)
|
| 229 |
if self.config.uniform_split:
|
|
|
|
| 317 |
decision = x.decision
|
| 318 |
yield id_, {
|
| 319 |
"patent_number": application_number,
|
| 320 |
+
"decision": patent["decision"], # decision,
|
| 321 |
"title": patent["title"],
|
| 322 |
"abstract": patent["abstract"],
|
| 323 |
"claims": patent["claims"],
|