Datasets:

Languages:
English
ArXiv:
License:
holylovenia commited on
Commit
ca9e599
1 Parent(s): c75b661

Upload cosem.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. cosem.py +173 -0
cosem.py ADDED
@@ -0,0 +1,173 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2022 The HuggingFace Datasets Authors and the current dataset script contributor.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ import os
17
+ import re
18
+ from typing import Dict, List, Tuple
19
+
20
+ import datasets
21
+
22
+ from seacrowd.utils import schemas
23
+ from seacrowd.utils.configs import SEACrowdConfig
24
+ from seacrowd.utils.constants import TASK_TO_SCHEMA, Licenses, Tasks
25
+
26
+ _CITATION = """\
27
+ @article{gonzales_corpus_2021,
28
+ title = {The {Corpus} of {Singapore} {English} {Messages} ({CoSEM})},
29
+ issn = {0883-2919, 1467-971X},
30
+ url = {https://onlinelibrary.wiley.com/doi/10.1111/weng.12534},
31
+ doi = {10.1111/weng.12534},
32
+ language = {en},
33
+ urldate = {2022-02-19},
34
+ journal = {World Englishes},
35
+ author = {Gonzales, Wilkinson Daniel Wong and Hiramoto, Mie and R. E. Leimgruber, Jakob and Lim, Jun Jie},
36
+ month = feb,
37
+ year = {2021},
38
+ }
39
+ """
40
+
41
+ _DATASETNAME = "cosem"
42
+
43
+ _DESCRIPTION = """\
44
+ The CoSEM dataset consists of over 900,000 lines of online messages from the messaging platform WhatsApp collected from personal chat
45
+ logs of students enrolled in an advanced sociolinguistics class from the National University of Singapore. Messages collected were
46
+ from 2016 to 2019. The dataset is in .txt format, where each line of utterance is tagged with a unique identifier that includes its
47
+ metadata such as line number, year message was sent, and age and nationality of sender.
48
+ """
49
+
50
+ _HOMEPAGE = "https://github.com/wdwgonzales/CoSEM/blob/main/Corpus/COSEM_v4_publicrelease_SEP172023.zip"
51
+
52
+ _LANGUAGES = ["eng"] # We follow ISO639-3 language code (https://iso639-3.sil.org/code_tables/639/data)
53
+
54
+ _LICENSE = Licenses.CC0_1_0.value
55
+
56
+ _LOCAL = False
57
+
58
+ _URLS = {_DATASETNAME: "https://github.com/wdwgonzales/CoSEM/raw/main/Corpus/COSEM_v4_publicrelease_SEP172023.zip"}
59
+
60
+ _SUPPORTED_TASKS = [Tasks.SELF_SUPERVISED_PRETRAINING]
61
+ _SUPPORTED_SCHEMA_STRINGS = [f"seacrowd_{str(TASK_TO_SCHEMA[task]).lower()}" for task in _SUPPORTED_TASKS]
62
+
63
+ _SOURCE_VERSION = "1.0.0"
64
+
65
+ _SEACROWD_VERSION = "2024.06.20"
66
+
67
+
68
+ class CoSEMDataset(datasets.GeneratorBasedBuilder):
69
+ """The CoSEM dataset consists of over 900,000 lines of online messages from the messaging platform WhatsApp collected from
70
+ personal chat logs of students enrolled in an advanced sociolinguistics class from the National University of Singapore."""
71
+
72
+ SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
73
+ SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION)
74
+
75
+ subset_id = _DATASETNAME
76
+
77
+ BUILDER_CONFIGS = [
78
+ SEACrowdConfig(
79
+ name=f"{subset_id}_source",
80
+ version=SOURCE_VERSION,
81
+ description=f"{_DATASETNAME} source schema",
82
+ schema="source",
83
+ subset_id=subset_id,
84
+ )
85
+ ]
86
+
87
+ seacrowd_schema_config: list[SEACrowdConfig] = []
88
+
89
+ for seacrowd_schema in _SUPPORTED_SCHEMA_STRINGS:
90
+
91
+ seacrowd_schema_config.append(
92
+ SEACrowdConfig(
93
+ name=f"{subset_id}_{seacrowd_schema}",
94
+ version=SEACROWD_VERSION,
95
+ description=f"{_DATASETNAME} {seacrowd_schema} schema",
96
+ schema=f"{seacrowd_schema}",
97
+ subset_id=subset_id,
98
+ )
99
+ )
100
+
101
+ BUILDER_CONFIGS.extend(seacrowd_schema_config)
102
+
103
+ DEFAULT_CONFIG_NAME = f"{_DATASETNAME}_source"
104
+
105
+ def _info(self) -> datasets.DatasetInfo:
106
+
107
+ if self.config.schema == "source":
108
+ features = datasets.Features(
109
+ {
110
+ "id": datasets.Value("string"),
111
+ "text": datasets.Value("string"),
112
+ }
113
+ )
114
+
115
+ elif self.config.schema == f"seacrowd_{str(TASK_TO_SCHEMA[Tasks.SELF_SUPERVISED_PRETRAINING]).lower()}":
116
+ features = schemas.ssp_features
117
+
118
+ else:
119
+ raise ValueError(f"Invalid config: {self.config.name}")
120
+
121
+ return datasets.DatasetInfo(
122
+ description=_DESCRIPTION,
123
+ features=features,
124
+ homepage=_HOMEPAGE,
125
+ license=_LICENSE,
126
+ citation=_CITATION,
127
+ )
128
+
129
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
130
+ """Returns SplitGenerators."""
131
+
132
+ split_generators = []
133
+
134
+ path = dl_manager.download_and_extract(_URLS[_DATASETNAME])
135
+
136
+ split_generators.append(
137
+ datasets.SplitGenerator(
138
+ name=datasets.Split.TRAIN,
139
+ gen_kwargs={
140
+ "path": os.path.join(path, "COSEM_v4_publicrelease_SEP172023"),
141
+ },
142
+ )
143
+ )
144
+
145
+ return split_generators
146
+
147
+ def _generate_examples(self, path: str) -> Tuple[int, Dict]:
148
+ """Yields examples as (key, example) tuples."""
149
+
150
+ files = os.listdir(path)
151
+ file_paths = [os.path.join(path, file) for file in files]
152
+ pattern = r"<(COSEM:.*?)>(.*?)(?=<COSEM:|$)"
153
+
154
+ s = {}
155
+
156
+ for file_path in file_paths:
157
+ with open(file_path, "r", encoding="utf-8") as file:
158
+ text = file.read()
159
+
160
+ matches = re.findall(pattern, text, re.DOTALL)
161
+ for match in matches:
162
+ key = match[0].strip()
163
+ value = match[1].strip()
164
+
165
+ if key in s:
166
+ continue
167
+ s[key] = value
168
+
169
+ if self.config.schema == "source" or self.config.schema == f"seacrowd_{str(TASK_TO_SCHEMA[Tasks.SELF_SUPERVISED_PRETRAINING]).lower()}":
170
+ yield key, {"id": key, "text": value}
171
+
172
+ else:
173
+ raise ValueError(f"Invalid config: {self.config.name}")