holylovenia
commited on
Commit
•
e106e2c
1
Parent(s):
7a2a76f
Upload thai_sum.py with huggingface_hub
Browse files- thai_sum.py +144 -0
thai_sum.py
ADDED
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import csv
|
2 |
+
from pathlib import Path
|
3 |
+
from typing import Dict, List, Tuple
|
4 |
+
|
5 |
+
import datasets
|
6 |
+
|
7 |
+
from seacrowd.utils.configs import SEACrowdConfig
|
8 |
+
from seacrowd.utils.constants import Tasks, Licenses
|
9 |
+
from seacrowd.utils import schemas
|
10 |
+
|
11 |
+
_CITATION = """\
|
12 |
+
@mastersthesis{chumpolsathien_2020,
|
13 |
+
title={Using Knowledge Distillation from Keyword Extraction to Improve the Informativeness of Neural Cross-lingual Summarization},
|
14 |
+
author={Chumpolsathien, Nakhun},
|
15 |
+
year={2020},
|
16 |
+
school={Beijing Institute of Technology}
|
17 |
+
"""
|
18 |
+
|
19 |
+
_DATASETNAME = "thai_sum"
|
20 |
+
|
21 |
+
_DESCRIPTION = """
|
22 |
+
We present ThaiSum, a large-scale corpus for Thai text summarization obtained from several online news websites namely Thairath, ThaiPBS, Prachathai, and The Standard. This dataset consists of over 350,000 article and summary pairs written by journalists.
|
23 |
+
"""
|
24 |
+
|
25 |
+
_HOMEPAGE = "https://github.com/nakhunchumpolsathien/ThaiSum"
|
26 |
+
|
27 |
+
_LICENSE = Licenses.MIT.value
|
28 |
+
|
29 |
+
_LANGUAGES = ["tha"]
|
30 |
+
|
31 |
+
_LOCAL = False
|
32 |
+
|
33 |
+
_URLS = {
|
34 |
+
"train": "https://nakhun-chumpolsathien.oss-us-west-1.aliyuncs.com/thaisum/thaisum.csv",
|
35 |
+
"val": "https://nakhun-chumpolsathien.oss-us-west-1.aliyuncs.com/thaisum/validation_set.csv",
|
36 |
+
"test": "https://nakhun-chumpolsathien.oss-us-west-1.aliyuncs.com/thaisum/test_set.csv",
|
37 |
+
}
|
38 |
+
|
39 |
+
_SUPPORTED_TASKS = [Tasks.SUMMARIZATION]
|
40 |
+
|
41 |
+
_SOURCE_VERSION = "1.0.0"
|
42 |
+
|
43 |
+
_SEACROWD_VERSION = "2024.06.20"
|
44 |
+
|
45 |
+
|
46 |
+
class ThaiSumDataset(datasets.GeneratorBasedBuilder):
|
47 |
+
"""
|
48 |
+
Sequence-to-sequence (Seq2Seq) models have shown great achievement in text summarization.
|
49 |
+
However, Seq2Seq model often requires large-scale training data to achieve effective results.
|
50 |
+
Although many impressive advancements in text summarization field have been made,
|
51 |
+
most of summarization studies focus on resource-rich languages.
|
52 |
+
The progress of Thai text summarization is still far behind.
|
53 |
+
The dearth of large-scale dataset keeps Thai text summarization in its infancy.
|
54 |
+
As far as our knowledge goes, there is not a large-scale dataset for Thai text summarization available anywhere.
|
55 |
+
Thus, we present ThaiSum, a large-scale corpus for Thai text summarization
|
56 |
+
obtained from several online news websites namely Thairath, ThaiPBS, Prachathai, and The Standard.
|
57 |
+
This dataset consists of over 350,000 article and summary pairs written by journalists.
|
58 |
+
We evaluate the performance of various existing summarization models on ThaiSum dataset and analyse
|
59 |
+
the characteristic of the dataset to present its difficulties.
|
60 |
+
"""
|
61 |
+
|
62 |
+
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
|
63 |
+
SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION)
|
64 |
+
|
65 |
+
BUILDER_CONFIGS = [
|
66 |
+
SEACrowdConfig(
|
67 |
+
name=f"{_DATASETNAME}_source",
|
68 |
+
version=SOURCE_VERSION,
|
69 |
+
description=f"{_DATASETNAME} source schema",
|
70 |
+
schema="source",
|
71 |
+
subset_id=f"{_DATASETNAME}",
|
72 |
+
),
|
73 |
+
SEACrowdConfig(
|
74 |
+
name=f"{_DATASETNAME}_seacrowd_t2t",
|
75 |
+
version=SEACROWD_VERSION,
|
76 |
+
description=f"{_DATASETNAME} SEACrowd schema",
|
77 |
+
schema="seacrowd_t2t",
|
78 |
+
subset_id=f"{_DATASETNAME}",
|
79 |
+
),
|
80 |
+
]
|
81 |
+
|
82 |
+
DEFAULT_CONFIG_NAME = f"{_DATASETNAME}_source"
|
83 |
+
|
84 |
+
def _info(self) -> datasets.DatasetInfo:
|
85 |
+
if self.config.schema == "source":
|
86 |
+
features = datasets.Features({"title": datasets.Value("string"), "body": datasets.Value("string"), "summary": datasets.Value("string"), "type": datasets.Value("string"), "tags": datasets.Value("string"), "url": datasets.Value("string")})
|
87 |
+
elif self.config.schema == "seacrowd_t2t":
|
88 |
+
features = schemas.text2text_features
|
89 |
+
|
90 |
+
return datasets.DatasetInfo(
|
91 |
+
description=_DESCRIPTION,
|
92 |
+
features=features,
|
93 |
+
homepage=_HOMEPAGE,
|
94 |
+
license=_LICENSE,
|
95 |
+
citation=_CITATION,
|
96 |
+
)
|
97 |
+
|
98 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
99 |
+
path_dict = dl_manager.download_and_extract(_URLS)
|
100 |
+
train_path, val_path, test_path = path_dict["train"], path_dict["val"], path_dict["test"]
|
101 |
+
|
102 |
+
return [
|
103 |
+
datasets.SplitGenerator(
|
104 |
+
name=datasets.Split.TRAIN,
|
105 |
+
gen_kwargs={
|
106 |
+
"filepath": train_path,
|
107 |
+
},
|
108 |
+
),
|
109 |
+
datasets.SplitGenerator(
|
110 |
+
name=datasets.Split.TEST,
|
111 |
+
gen_kwargs={"filepath": test_path},
|
112 |
+
),
|
113 |
+
datasets.SplitGenerator(
|
114 |
+
name=datasets.Split.VALIDATION,
|
115 |
+
gen_kwargs={
|
116 |
+
"filepath": val_path,
|
117 |
+
},
|
118 |
+
),
|
119 |
+
]
|
120 |
+
|
121 |
+
def _generate_examples(self, filepath: Path) -> Tuple[int, Dict]:
|
122 |
+
csv.field_size_limit(int(1000000))
|
123 |
+
with open(filepath, encoding="utf-8") as f:
|
124 |
+
csv_reader = csv.reader(f)
|
125 |
+
next(csv_reader) # skip header
|
126 |
+
if self.config.schema == "source":
|
127 |
+
for id_, row in enumerate(csv_reader):
|
128 |
+
yield id_, {
|
129 |
+
"title": row[0],
|
130 |
+
"body": row[1],
|
131 |
+
"summary": row[2],
|
132 |
+
"type": row[3],
|
133 |
+
"tags": row[4],
|
134 |
+
"url": row[5],
|
135 |
+
}
|
136 |
+
elif self.config.schema == "seacrowd_t2t":
|
137 |
+
for id_, row in enumerate(csv_reader):
|
138 |
+
yield id_, {
|
139 |
+
"id": str(id_),
|
140 |
+
"text_1": row[1],
|
141 |
+
"text_2": row[2],
|
142 |
+
"text_1_name": "document",
|
143 |
+
"text_2_name": "summary",
|
144 |
+
}
|