albertvillanova HF staff commited on
Commit
0f27e65
1 Parent(s): 074aeed

Update dataset loading script

Browse files
Files changed (1) hide show
  1. big_patent.py +37 -28
big_patent.py CHANGED
@@ -15,7 +15,6 @@
15
 
16
  # Lint as: python3
17
  """BigPatent Dataset."""
18
- import glob
19
  import gzip
20
  import json
21
  import os
@@ -53,12 +52,8 @@ There are two features:
53
 
54
  _LICENSE = "Creative Commons Attribution 4.0 International"
55
 
56
- _REPO = "https://huggingface.co/datasets/big_patent/resolve/main/data"
57
- _URLS = {
58
- "train": f"{_REPO}/train.zip",
59
- "validation": f"{_REPO}/val.zip",
60
- "test": f"{_REPO}/test.zip",
61
- }
62
 
63
  _DOCUMENT = "description"
64
  _SUMMARY = "abstract"
@@ -78,38 +73,44 @@ _CPC_DESCRIPTION = {
78
  # Available versions:
79
  # 1.0.0 lower cased tokenized words.
80
  # 2.0.0 cased raw strings.
81
- # 2.1.0 cased raw strings (fixed).
82
- # TODO Add raw string versions
83
 
84
- _VERSION = "1.0.0"
85
 
86
 
87
  class BigPatentConfig(datasets.BuilderConfig):
88
  """BuilderConfig for BigPatent."""
89
 
90
- def __init__(self, *args, cpc_codes=None, **kwargs):
91
  """BuilderConfig for BigPatent.
92
  Args:
93
- cpc_codes: str, cpc_codes
 
94
  **kwargs: keyword arguments forwarded to super.
95
  """
96
- super().__init__(*args, version=_VERSION, **kwargs)
97
- self.cpc_codes = cpc_codes
 
 
 
 
 
 
 
98
 
99
 
100
  class BigPatent(datasets.GeneratorBasedBuilder):
101
  """BigPatent datasets."""
102
 
 
103
  BUILDER_CONFIGS = [
104
  BigPatentConfig(
105
- cpc_codes=list(_CPC_DESCRIPTION),
106
- name="all",
107
  description="Patents under all categories.",
108
  ),
109
  ] + [
110
- BigPatentConfig( # pylint:disable=g-complex-comprehension
111
- cpc_codes=[k],
112
- name=k,
113
  description=f"Patents under Cooperative Patent Classification (CPC) {k}: {v}",
114
  )
115
  for k, v in sorted(_CPC_DESCRIPTION.items())
@@ -129,22 +130,30 @@ class BigPatent(datasets.GeneratorBasedBuilder):
129
 
130
  def _split_generators(self, dl_manager):
131
  """Returns SplitGenerators."""
132
- dl_paths = dl_manager.download_and_extract(_URLS)
133
- split_dirs = {datasets.Split.TRAIN: "train", datasets.Split.VALIDATION: "val", datasets.Split.TEST: "test"}
 
 
 
 
 
 
 
 
 
134
  return [
135
  datasets.SplitGenerator(
136
  name=split,
137
- gen_kwargs={"path": dl_paths[split], "split_dir": split_dirs[split]},
138
  )
139
- for split in split_dirs
140
  ]
141
 
142
- def _generate_examples(self, path=None, split_dir=None):
143
  """Yields examples."""
144
- for cpc_code in self.config.cpc_codes:
145
- filenames = glob.glob(os.path.join(path, split_dir, cpc_code, "*"))
146
- for filename in sorted(filenames):
147
- with open(filename, "rb") as fin:
148
  fin = gzip.GzipFile(fileobj=fin)
149
  for row in fin:
150
  json_obj = json.loads(row)
15
 
16
  # Lint as: python3
17
  """BigPatent Dataset."""
 
18
  import gzip
19
  import json
20
  import os
52
 
53
  _LICENSE = "Creative Commons Attribution 4.0 International"
54
 
55
+ _SPLIT_NAMES = {datasets.Split.TRAIN: "train", datasets.Split.VALIDATION: "val", datasets.Split.TEST: "test"}
56
+ _URL = "data/{version}/{split_name}.zip"
 
 
 
 
57
 
58
  _DOCUMENT = "description"
59
  _SUMMARY = "abstract"
73
  # Available versions:
74
  # 1.0.0 lower cased tokenized words.
75
  # 2.0.0 cased raw strings.
76
+ # 2.1.2 cased raw strings (fixed).
 
77
 
78
+ _VERSION = "2.1.2"
79
 
80
 
81
  class BigPatentConfig(datasets.BuilderConfig):
82
  """BuilderConfig for BigPatent."""
83
 
84
+ def __init__(self, codes="all", version=_VERSION, **kwargs):
85
  """BuilderConfig for BigPatent.
86
  Args:
87
+ codes (str or list, default 'all'): CPC codes. Either 'all' or a combination
88
+ of {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'y'}.
89
  **kwargs: keyword arguments forwarded to super.
90
  """
91
+ if isinstance(codes, str):
92
+ codes = [codes]
93
+ name = "+".join(codes)
94
+ if name == "all":
95
+ codes = list(_CPC_DESCRIPTION)
96
+ if version != _VERSION:
97
+ name = f"{name}-{version}"
98
+ super().__init__(name=name, version=version, **kwargs)
99
+ self.codes = codes
100
 
101
 
102
  class BigPatent(datasets.GeneratorBasedBuilder):
103
  """BigPatent datasets."""
104
 
105
+ BUILDER_CONFIG_CLASS = BigPatentConfig
106
  BUILDER_CONFIGS = [
107
  BigPatentConfig(
108
+ codes="all",
 
109
  description="Patents under all categories.",
110
  ),
111
  ] + [
112
+ BigPatentConfig(
113
+ codes=k,
 
114
  description=f"Patents under Cooperative Patent Classification (CPC) {k}: {v}",
115
  )
116
  for k, v in sorted(_CPC_DESCRIPTION.items())
130
 
131
  def _split_generators(self, dl_manager):
132
  """Returns SplitGenerators."""
133
+ urls = {
134
+ split: _URL.format(version=self.config.version, split_name=split_name)
135
+ for split, split_name in _SPLIT_NAMES.items()
136
+ }
137
+ dl_paths = dl_manager.download_and_extract(urls)
138
+ paths = {
139
+ split: [
140
+ dl_manager.iter_files(os.path.join(dl_paths[split], split_name, code)) for code in self.config.codes
141
+ ]
142
+ for split, split_name in _SPLIT_NAMES.items()
143
+ }
144
  return [
145
  datasets.SplitGenerator(
146
  name=split,
147
+ gen_kwargs={"paths": paths[split]},
148
  )
149
+ for split in _SPLIT_NAMES
150
  ]
151
 
152
+ def _generate_examples(self, paths=None):
153
  """Yields examples."""
154
+ for paths_per_code in paths:
155
+ for path in paths_per_code:
156
+ with open(path, "rb") as fin:
 
157
  fin = gzip.GzipFile(fileobj=fin)
158
  for row in fin:
159
  json_obj = json.loads(row)