shunk031 commited on
Commit
1199a0e
1 Parent(s): 85453b9

Refactor with dataclass (#8)

Browse files

* use dataclass

* update README

Files changed (2) hide show
  1. README.md +20 -0
  2. livedoor-news-corpus.py +22 -40
README.md CHANGED
@@ -1,3 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  # Dataset Card for Livedoor News Corpus
2
 
3
  [![CI](https://github.com/shunk031/huggingface-datasets_livedoor-news-corpus/actions/workflows/ci.yaml/badge.svg)](https://github.com/shunk031/huggingface-datasets_livedoor-news-corpus/actions/workflows/ci.yaml)
 
1
+ ---
2
+ annotations_creators: []
3
+ language:
4
+ - ja
5
+ language_creators:
6
+ - found
7
+ license:
8
+ - cc-by-nd-4.0
9
+ multilinguality:
10
+ - monolingual
11
+ pretty_name: livedoor-news-corpus
12
+ size_categories: []
13
+ source_datasets: []
14
+ tags: []
15
+ task_categories:
16
+ - text-classification
17
+ task_ids:
18
+ - multi-class-classification
19
+ ---
20
+
21
  # Dataset Card for Livedoor News Corpus
22
 
23
  [![CI](https://github.com/shunk031/huggingface-datasets_livedoor-news-corpus/actions/workflows/ci.yaml/badge.svg)](https://github.com/shunk031/huggingface-datasets_livedoor-news-corpus/actions/workflows/ci.yaml)
livedoor-news-corpus.py CHANGED
@@ -1,9 +1,11 @@
1
  import logging
 
2
  import pathlib
3
- from typing import Dict, List, Union, Optional
4
  import random
 
 
 
5
  import datasets as ds
6
- import math
7
 
8
  logger = logging.getLogger(__name__)
9
 
@@ -25,45 +27,24 @@ _LICENSE = """\
25
  _DOWNLOAD_URL = "https://www.rondhuit.com/download/ldcc-20140209.tar.gz"
26
 
27
 
 
28
  class LivedoorNewsCorpusConfig(ds.BuilderConfig):
29
- def __init__(
30
- self,
31
- train_ratio: float = 0.8,
32
- val_ratio: float = 0.1,
33
- test_ratio: float = 0.1,
34
- shuffle: bool = False,
35
- random_state: int = 0,
36
- name: str = "default",
37
- version: Optional[Union[ds.utils.Version, str]] = ds.utils.Version("0.0.0"),
38
- data_dir: Optional[str] = None,
39
- data_files: Optional[ds.data_files.DataFilesDict] = None,
40
- description: Optional[str] = None,
41
- ) -> None:
42
- super().__init__(
43
- name=name,
44
- version=version,
45
- data_dir=data_dir,
46
- data_files=data_files,
47
- description=description,
48
- )
49
- assert train_ratio + val_ratio + test_ratio == 1.0
50
-
51
- self.train_ratio = train_ratio
52
- self.val_ratio = val_ratio
53
- self.test_ratio = test_ratio
54
 
55
- self.shuffle = shuffle
56
- self.random_state = random_state
57
 
58
 
59
  class LivedoorNewsCorpusDataset(ds.GeneratorBasedBuilder):
60
- VERSION = ds.Version("1.0.0") # type: ignore
61
-
62
- BUILDER_CONFIG_CLASS = LivedoorNewsCorpusConfig # type: ignore
63
-
64
  BUILDER_CONFIGS = [
65
  LivedoorNewsCorpusConfig(
66
- version=VERSION, # type: ignore
67
  description="Livedoor ニュースコーパス",
68
  )
69
  ]
@@ -105,14 +86,16 @@ class LivedoorNewsCorpusDataset(ds.GeneratorBasedBuilder):
105
  article_paths = list(dataset_root_dir.glob("*/**/*.txt"))
106
  article_paths = list(filter(lambda p: p.name != "LICENSE.txt", article_paths))
107
 
108
- if self.config.shuffle: # type: ignore
109
- random.seed(self.config.random_state) # type: ignore
 
 
110
  random.shuffle(article_paths)
111
 
112
  num_articles = len(article_paths)
113
- num_tng = math.ceil(num_articles * self.config.train_ratio) # type: ignore
114
- num_val = math.ceil(num_articles * self.config.val_ratio) # type: ignore
115
- num_tst = math.ceil(num_articles * self.config.test_ratio) # type: ignore
116
 
117
  tng_articles = article_paths[:num_tng]
118
  val_articles = article_paths[num_tng : num_tng + num_val]
@@ -150,7 +133,6 @@ class LivedoorNewsCorpusDataset(ds.GeneratorBasedBuilder):
150
  return example_dict
151
 
152
  def _generate_examples(self, article_paths: List[pathlib.Path]): # type: ignore[override]
153
-
154
  for i, article_path in enumerate(article_paths):
155
  article_category = article_path.parent.name
156
  with open(article_path, "r") as rf:
 
1
  import logging
2
+ import math
3
  import pathlib
 
4
  import random
5
+ from dataclasses import dataclass
6
+ from typing import Dict, List
7
+
8
  import datasets as ds
 
9
 
10
  logger = logging.getLogger(__name__)
11
 
 
27
  _DOWNLOAD_URL = "https://www.rondhuit.com/download/ldcc-20140209.tar.gz"
28
 
29
 
30
+ @dataclass
31
  class LivedoorNewsCorpusConfig(ds.BuilderConfig):
32
+ train_ratio: float = 0.8
33
+ val_ratio: float = 0.1
34
+ test_ratio: float = 0.1
35
+ shuffle: bool = False
36
+ random_state: int = 0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
 
38
+ def __post_init__(self):
39
+ assert self.train_ratio + self.val_ratio + self.test_ratio == 1.0
40
 
41
 
42
  class LivedoorNewsCorpusDataset(ds.GeneratorBasedBuilder):
43
+ VERSION = ds.Version("1.0.0")
44
+ BUILDER_CONFIG_CLASS = LivedoorNewsCorpusConfig
 
 
45
  BUILDER_CONFIGS = [
46
  LivedoorNewsCorpusConfig(
47
+ version=VERSION,
48
  description="Livedoor ニュースコーパス",
49
  )
50
  ]
 
86
  article_paths = list(dataset_root_dir.glob("*/**/*.txt"))
87
  article_paths = list(filter(lambda p: p.name != "LICENSE.txt", article_paths))
88
 
89
+ config: LivedoorNewsCorpusConfig = self.config
90
+
91
+ if config.shuffle:
92
+ random.seed(config.random_state)
93
  random.shuffle(article_paths)
94
 
95
  num_articles = len(article_paths)
96
+ num_tng = math.ceil(num_articles * config.train_ratio)
97
+ num_val = math.ceil(num_articles * config.val_ratio)
98
+ num_tst = math.ceil(num_articles * config.test_ratio)
99
 
100
  tng_articles = article_paths[:num_tng]
101
  val_articles = article_paths[num_tng : num_tng + num_val]
 
133
  return example_dict
134
 
135
  def _generate_examples(self, article_paths: List[pathlib.Path]): # type: ignore[override]
 
136
  for i, article_path in enumerate(article_paths):
137
  article_category = article_path.parent.name
138
  with open(article_path, "r") as rf: