Upload thai_constitution.py with huggingface_hub
Browse files- thai_constitution.py +144 -0
thai_constitution.py
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
+
from typing import Dict, List, Tuple
|
| 3 |
+
|
| 4 |
+
import datasets
|
| 5 |
+
import pandas as pd
|
| 6 |
+
|
| 7 |
+
from seacrowd.utils import schemas
|
| 8 |
+
from seacrowd.utils.configs import SEACrowdConfig
|
| 9 |
+
from seacrowd.utils.constants import (DEFAULT_SEACROWD_VIEW_NAME,
|
| 10 |
+
DEFAULT_SOURCE_VIEW_NAME, Tasks, Licenses, TASK_TO_SCHEMA)
|
| 11 |
+
|
| 12 |
+
_DATASETNAME = "thai_constitution"
|
| 13 |
+
_SOURCE_VIEW_NAME = DEFAULT_SOURCE_VIEW_NAME
|
| 14 |
+
_UNIFIED_VIEW_NAME = DEFAULT_SEACROWD_VIEW_NAME
|
| 15 |
+
|
| 16 |
+
_CITATION = """\
|
| 17 |
+
@misc{
|
| 18 |
+
thaiconstitution,
|
| 19 |
+
title={Thai Constitution Corpus},
|
| 20 |
+
url={https://github.com/PyThaiNLP/Thai-constitution-corpus},
|
| 21 |
+
author={Wannaphong Phatthiyaphaibun}
|
| 22 |
+
}
|
| 23 |
+
"""
|
| 24 |
+
|
| 25 |
+
_LOCAL = False
|
| 26 |
+
|
| 27 |
+
_DESCRIPTION = """\
|
| 28 |
+
Thailand's constitutional archive since 1932
|
| 29 |
+
- Data collected from Office of the Council of State
|
| 30 |
+
- This project is part of the development plan PyThaiNLP
|
| 31 |
+
- The information collected in this text archive is in the public domain according to the Copyright Act 1994, Section 7 (The following are not considered copyrighted works under this Act: (1) daily news and Various facts which has the nature of being only news and not work in the literature department Science department or art department [...] (3) regulations, announcements, orders, clarifications and correspondence of ministries, bureaus, departments or any other government or local agencies [...])
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
_HOMEPAGE = "https://github.com/PyThaiNLP/Thai-constitution-corpus/tree/master"
|
| 35 |
+
|
| 36 |
+
_LICENSE = Licenses.CC0_1_0.value
|
| 37 |
+
|
| 38 |
+
_SUPPORTED_TASKS = [Tasks.SELF_SUPERVISED_PRETRAINING]
|
| 39 |
+
_SEACROWD_SCHEMA_NAME = TASK_TO_SCHEMA[_SUPPORTED_TASKS[0]].lower()
|
| 40 |
+
_LANGUAGES = ['tha']
|
| 41 |
+
_SOURCE_VERSION = "1.0.0"
|
| 42 |
+
_SEACROWD_VERSION = "2024.06.20"
|
| 43 |
+
|
| 44 |
+
_URLS = [
|
| 45 |
+
"https://raw.githubusercontent.com/PyThaiNLP/Thai-constitution-corpus/master/data/%E0%B8%98%E0%B8%A3%E0%B8%A3%E0%B8%A1%E0%B8%99%E0%B8%B9%E0%B8%8D%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B8%9B%E0%B8%81%E0%B8%84%E0%B8%A3%E0%B8%AD%E0%B8%87%E0%B8%A3%E0%B8%B2%E0%B8%8A%E0%B8%AD%E0%B8%B2%E0%B8%93%E0%B8%B2%E0%B8%88%E0%B8%B1%E0%B8%81%E0%B8%A3%20%E0%B8%9E%E0%B8%B8%E0%B8%97%E0%B8%98%E0%B8%A8%E0%B8%B1%E0%B8%81%E0%B8%A3%E0%B8%B2%E0%B8%8A%202502.txt",
|
| 46 |
+
"https://raw.githubusercontent.com/PyThaiNLP/Thai-constitution-corpus/master/data/%E0%B8%98%E0%B8%A3%E0%B8%A3%E0%B8%A1%E0%B8%99%E0%B8%B9%E0%B8%8D%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B8%9B%E0%B8%81%E0%B8%84%E0%B8%A3%E0%B8%AD%E0%B8%87%E0%B8%A3%E0%B8%B2%E0%B8%8A%E0%B8%AD%E0%B8%B2%E0%B8%93%E0%B8%B2%E0%B8%88%E0%B8%B1%E0%B8%81%E0%B8%A3%20%E0%B8%9E%E0%B8%B8%E0%B8%97%E0%B8%98%E0%B8%A8%E0%B8%B1%E0%B8%81%E0%B8%A3%E0%B8%B2%E0%B8%8A%202515.txt",
|
| 47 |
+
"https://raw.githubusercontent.com/PyThaiNLP/Thai-constitution-corpus/master/data/%E0%B8%98%E0%B8%A3%E0%B8%A3%E0%B8%A1%E0%B8%99%E0%B8%B9%E0%B8%8D%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B8%9B%E0%B8%81%E0%B8%84%E0%B8%A3%E0%B8%AD%E0%B8%87%E0%B8%A3%E0%B8%B2%E0%B8%8A%E0%B8%AD%E0%B8%B2%E0%B8%93%E0%B8%B2%E0%B8%88%E0%B8%B1%E0%B8%81%E0%B8%A3%20%E0%B8%9E%E0%B8%B8%E0%B8%97%E0%B8%98%E0%B8%A8%E0%B8%B1%E0%B8%81%E0%B8%A3%E0%B8%B2%E0%B8%8A%202520.txt",
|
| 48 |
+
"https://raw.githubusercontent.com/PyThaiNLP/Thai-constitution-corpus/master/data/%E0%B8%98%E0%B8%A3%E0%B8%A3%E0%B8%A1%E0%B8%99%E0%B8%B9%E0%B8%8D%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B8%9B%E0%B8%81%E0%B8%84%E0%B8%A3%E0%B8%AD%E0%B8%87%E0%B8%A3%E0%B8%B2%E0%B8%8A%E0%B8%AD%E0%B8%B2%E0%B8%93%E0%B8%B2%E0%B8%88%E0%B8%B1%E0%B8%81%E0%B8%A3%20%E0%B8%9E%E0%B8%B8%E0%B8%97%E0%B8%98%E0%B8%A8%E0%B8%B1%E0%B8%81%E0%B8%A3%E0%B8%B2%E0%B8%8A%202534.txt",
|
| 49 |
+
"https://raw.githubusercontent.com/PyThaiNLP/Thai-constitution-corpus/master/data/%E0%B8%9E%E0%B8%A3%E0%B8%B0%E0%B8%A3%E0%B8%B2%E0%B8%8A%E0%B8%9A%E0%B8%B1%E0%B8%8D%E0%B8%8D%E0%B8%B1%E0%B8%95%E0%B8%B4%E0%B8%98%E0%B8%A3%E0%B8%A3%E0%B8%A1%E0%B8%99%E0%B8%B9%E0%B8%8D%E0%B8%81%E0%B8%B2%E0%B8%A3%E0%B8%9B%E0%B8%81%E0%B8%84%E0%B8%A3%E0%B8%AD%E0%B8%87%E0%B9%81%E0%B8%9C%E0%B9%88%E0%B8%99%E0%B8%94%E0%B8%B4%E0%B8%99%E0%B8%AA%E0%B8%A2%E0%B8%B2%E0%B8%A1%E0%B8%8A%E0%B8%B1%E0%B9%88%E0%B8%A7%E0%B8%84%E0%B8%A3%E0%B8%B2%E0%B8%A72475.txt",
|
| 50 |
+
"https://raw.githubusercontent.com/PyThaiNLP/Thai-constitution-corpus/master/data/%E0%B8%A3%E0%B8%B1%E0%B8%90%E0%B8%98%E0%B8%A3%E0%B8%A3%E0%B8%A1%E0%B8%99%E0%B8%B9%E0%B8%8D2475.txt",
|
| 51 |
+
"https://raw.githubusercontent.com/PyThaiNLP/Thai-constitution-corpus/master/data/%E0%B8%A3%E0%B8%B1%E0%B8%90%E0%B8%98%E0%B8%A3%E0%B8%A3%E0%B8%A1%E0%B8%99%E0%B8%B9%E0%B8%8D2489.txt",
|
| 52 |
+
"https://raw.githubusercontent.com/PyThaiNLP/Thai-constitution-corpus/master/data/%E0%B8%A3%E0%B8%B1%E0%B8%90%E0%B8%98%E0%B8%A3%E0%B8%A3%E0%B8%A1%E0%B8%99%E0%B8%B9%E0%B8%8D2492.txt",
|
| 53 |
+
"https://raw.githubusercontent.com/PyThaiNLP/Thai-constitution-corpus/master/data/%E0%B8%A3%E0%B8%B1%E0%B8%90%E0%B8%98%E0%B8%A3%E0%B8%A3%E0%B8%A1%E0%B8%99%E0%B8%B9%E0%B8%8D2511.txt",
|
| 54 |
+
"https://raw.githubusercontent.com/PyThaiNLP/Thai-constitution-corpus/master/data/%E0%B8%A3%E0%B8%B1%E0%B8%90%E0%B8%98%E0%B8%A3%E0%B8%A3%E0%B8%A1%E0%B8%99%E0%B8%B9%E0%B8%8D2517.txt",
|
| 55 |
+
"https://raw.githubusercontent.com/PyThaiNLP/Thai-constitution-corpus/master/data/%E0%B8%A3%E0%B8%B1%E0%B8%90%E0%B8%98%E0%B8%A3%E0%B8%A3%E0%B8%A1%E0%B8%99%E0%B8%B9%E0%B8%8D2519.txt",
|
| 56 |
+
"https://raw.githubusercontent.com/PyThaiNLP/Thai-constitution-corpus/master/data/%E0%B8%A3%E0%B8%B1%E0%B8%90%E0%B8%98%E0%B8%A3%E0%B8%A3%E0%B8%A1%E0%B8%99%E0%B8%B9%E0%B8%8D2521.txt",
|
| 57 |
+
"https://raw.githubusercontent.com/PyThaiNLP/Thai-constitution-corpus/master/data/%E0%B8%A3%E0%B8%B1%E0%B8%90%E0%B8%98%E0%B8%A3%E0%B8%A3%E0%B8%A1%E0%B8%99%E0%B8%B9%E0%B8%8D2534.txt",
|
| 58 |
+
"https://raw.githubusercontent.com/PyThaiNLP/Thai-constitution-corpus/master/data/%E0%B8%A3%E0%B8%B1%E0%B8%90%E0%B8%98%E0%B8%A3%E0%B8%A3%E0%B8%A1%E0%B8%99%E0%B8%B9%E0%B8%8D2540.txt",
|
| 59 |
+
"https://raw.githubusercontent.com/PyThaiNLP/Thai-constitution-corpus/master/data/%E0%B8%A3%E0%B8%B1%E0%B8%90%E0%B8%98%E0%B8%A3%E0%B8%A3%E0%B8%A1%E0%B8%99%E0%B8%B9%E0%B8%8D2550.txt",
|
| 60 |
+
"https://raw.githubusercontent.com/PyThaiNLP/Thai-constitution-corpus/master/data/%E0%B8%A3%E0%B8%B1%E0%B8%90%E0%B8%98%E0%B8%A3%E0%B8%A3%E0%B8%A1%E0%B8%99%E0%B8%B9%E0%B8%8D2560.txt",
|
| 61 |
+
"https://raw.githubusercontent.com/PyThaiNLP/Thai-constitution-corpus/master/data/%E0%B8%A3%E0%B8%B1%E0%B8%90%E0%B8%98%E0%B8%A3%E0%B8%A3%E0%B8%A1%E0%B8%99%E0%B8%B9%E0%B8%8D%E0%B8%89%E0%B8%B0%E0%B8%9A%E0%B8%B1%E0%B8%9A%E0%B8%8A%E0%B8%B1%E0%B9%88%E0%B8%A7%E0%B8%84%E0%B8%A3%E0%B8%B2%E0%B8%A72490.txt",
|
| 62 |
+
"https://raw.githubusercontent.com/PyThaiNLP/Thai-constitution-corpus/master/data/%E0%B8%A3%E0%B8%B1%E0%B8%90%E0%B8%98%E0%B8%A3%E0%B8%A3%E0%B8%A1%E0%B8%99%E0%B8%B9%E0%B8%8D%E0%B9%81%E0%B8%AB%E0%B9%88%E0%B8%87%E0%B8%A3%E0%B8%B2%E0%B8%8A%E0%B8%AD%E0%B8%B2%E0%B8%93%E0%B8%B2%E0%B8%88%E0%B8%B1%E0%B8%81%E0%B8%A3%E0%B9%84%E0%B8%97%E0%B8%A2%20(%E0%B8%89%E0%B8%9A%E0%B8%B1%E0%B8%9A%E0%B8%8A%E0%B8%B1%E0%B9%88%E0%B8%A7%E0%B8%84%E0%B8%A3%E0%B8%B2%E0%B8%A7)%20%E0%B8%9E%E0%B8%B8%E0%B8%97%E0%B8%98%E0%B8%A8%E0%B8%B1%E0%B8%81%E0%B8%A3%E0%B8%B2%E0%B8%8A%202549.txt",
|
| 63 |
+
"https://raw.githubusercontent.com/PyThaiNLP/Thai-constitution-corpus/master/data/%E0%B8%A3%E0%B8%B1%E0%B8%90%E0%B8%98%E0%B8%A3%E0%B8%A3%E0%B8%A1%E0%B8%99%E0%B8%B9%E0%B8%8D%E0%B9%81%E0%B8%AB%E0%B9%88%E0%B8%87%E0%B8%A3%E0%B8%B2%E0%B8%8A%E0%B8%AD%E0%B8%B2%E0%B8%93%E0%B8%B2%E0%B8%88%E0%B8%B1%E0%B8%81%E0%B8%A3%E0%B9%84%E0%B8%97%E0%B8%A2%20(%E0%B8%89%E0%B8%9A%E0%B8%B1%E0%B8%9A%E0%B8%8A%E0%B8%B1%E0%B9%88%E0%B8%A7%E0%B8%84%E0%B8%A3%E0%B8%B2%E0%B8%A7)%20%E0%B8%9E%E0%B8%B8%E0%B8%97%E0%B8%98%E0%B8%A8%E0%B8%B1%E0%B8%81%E0%B8%A3%E0%B8%B2%E0%B8%8A%202557.txt",
|
| 64 |
+
"https://raw.githubusercontent.com/PyThaiNLP/Thai-constitution-corpus/master/data/%E0%B8%A3%E0%B8%B1%E0%B8%90%E0%B8%98%E0%B8%A3%E0%B8%A3%E0%B8%A1%E0%B8%99%E0%B8%B9%E0%B8%8D%E0%B9%81%E0%B8%AB%E0%B9%88%E0%B8%87%E0%B8%A3%E0%B8%B2%E0%B8%8A%E0%B8%AD%E0%B8%B2%E0%B8%93%E0%B8%B2%E0%B8%88%E0%B8%B1%E0%B8%81%E0%B8%A3%E0%B9%84%E0%B8%97%E0%B8%A2%20%E0%B8%9E%E0%B8%B8%E0%B8%97%E0%B8%98%E0%B8%A8%E0%B8%B1%E0%B8%81%E0%B8%A3%E0%B8%B2%E0%B8%8A%202475%20%E0%B9%81%E0%B8%81%E0%B9%89%E0%B9%84%E0%B8%82%E0%B9%80%E0%B8%9E%E0%B8%B4%E0%B9%88%E0%B8%A1%E0%B9%80%E0%B8%95%E0%B8%B4%E0%B8%A1%20%E0%B8%9E%E0%B8%B8%E0%B8%97%E0%B8%98%E0%B8%A8%E0%B8%B1%E0%B8%81%E0%B8%A3%E0%B8%B2%E0%B8%8A%202495.txt"
|
| 65 |
+
]
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
class ThaiConstitutionDataset(datasets.GeneratorBasedBuilder):
|
| 69 |
+
"""Thailand's constitutional archive since 1932"""
|
| 70 |
+
|
| 71 |
+
BUILDER_CONFIGS = [
|
| 72 |
+
SEACrowdConfig(
|
| 73 |
+
name="thai_constitution_source",
|
| 74 |
+
version=datasets.Version(_SOURCE_VERSION),
|
| 75 |
+
description="Thai constitution source schema",
|
| 76 |
+
schema="source",
|
| 77 |
+
subset_id="thai_constitution",
|
| 78 |
+
),
|
| 79 |
+
SEACrowdConfig(
|
| 80 |
+
name=f"thai_constitution_seacrowd_{_SEACROWD_SCHEMA_NAME}",
|
| 81 |
+
version=datasets.Version(_SEACROWD_VERSION),
|
| 82 |
+
description="Thai constitution SEACrowd schema",
|
| 83 |
+
schema=f"seacrowd_{_SEACROWD_SCHEMA_NAME}",
|
| 84 |
+
subset_id="thai_constitution",
|
| 85 |
+
),
|
| 86 |
+
]
|
| 87 |
+
|
| 88 |
+
def _info(self) -> datasets.DatasetInfo:
|
| 89 |
+
if self.config.schema == "source":
|
| 90 |
+
features = datasets.Features(
|
| 91 |
+
{
|
| 92 |
+
"id": datasets.Value("string"),
|
| 93 |
+
"text": datasets.Value("string"),
|
| 94 |
+
}
|
| 95 |
+
)
|
| 96 |
+
elif self.config.schema == f"seacrowd_{_SEACROWD_SCHEMA_NAME}":
|
| 97 |
+
features = schemas.self_supervised_pretraining.features
|
| 98 |
+
else:
|
| 99 |
+
raise ValueError(f"Invalid config schema: {self.config.schema}")
|
| 100 |
+
|
| 101 |
+
return datasets.DatasetInfo(
|
| 102 |
+
description=_DESCRIPTION,
|
| 103 |
+
features=features,
|
| 104 |
+
homepage=_HOMEPAGE,
|
| 105 |
+
license=_LICENSE,
|
| 106 |
+
citation=_CITATION,
|
| 107 |
+
)
|
| 108 |
+
|
| 109 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
| 110 |
+
filepaths = [Path(dl_manager.download(url)) for url in _URLS]
|
| 111 |
+
|
| 112 |
+
return [
|
| 113 |
+
datasets.SplitGenerator(
|
| 114 |
+
name=datasets.Split.TRAIN,
|
| 115 |
+
gen_kwargs={"filepaths": filepaths},
|
| 116 |
+
),
|
| 117 |
+
]
|
| 118 |
+
|
| 119 |
+
def _generate_examples(self, filepaths: List[Path]) -> Tuple[int, Dict]:
|
| 120 |
+
counter = 0
|
| 121 |
+
for path in filepaths:
|
| 122 |
+
with open(path, encoding="utf-8") as f:
|
| 123 |
+
for line in f.readlines():
|
| 124 |
+
if line.strip() == "":
|
| 125 |
+
continue
|
| 126 |
+
|
| 127 |
+
if self.config.schema == "source":
|
| 128 |
+
yield (
|
| 129 |
+
counter,
|
| 130 |
+
{
|
| 131 |
+
"id": str(counter),
|
| 132 |
+
"text": line.strip(),
|
| 133 |
+
},
|
| 134 |
+
)
|
| 135 |
+
elif self.config.schema == f"seacrowd_{_SEACROWD_SCHEMA_NAME}":
|
| 136 |
+
yield (
|
| 137 |
+
counter,
|
| 138 |
+
{
|
| 139 |
+
"id": str(counter),
|
| 140 |
+
"text": line.strip(),
|
| 141 |
+
},
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
counter += 1
|