jimregan commited on
Commit
3a16634
1 Parent(s): 6f9091d

add script

Browse files
Files changed (1) hide show
  1. cngv1.py +193 -0
cngv1.py ADDED
@@ -0,0 +1,193 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from pathlib import Path
3
+
4
+ import datasets
5
+
6
+ from bs4 import BeautifulSoup
7
+
8
+ _CITATION = """\
9
+ @article{ite2003corpas,
10
+ title={Corpas Náisiúnta na Gaeilge/National Corpus of Irish, Volume 1},
11
+ author={Institiúid Teangeolaíochta Éireann},
12
+ journal={Dublin: ITÉ},
13
+ year={2003}
14
+ }
15
+ """
16
+
17
+ _DESCRIPTION = """\
18
+ Corpus of written Irish.
19
+ """
20
+
21
+ _TEXTDIRS = [
22
+ "fiction", "information", "instruction", "non_fiction", "official"
23
+ ]
24
+
25
+ class CNGDataset(datasets.GeneratorBasedBuilder):
26
+ """National Corpus of Irish."""
27
+
28
+ VERSION = datasets.Version("1.1.0")
29
+
30
+ BUILDER_CONFIGS = [
31
+ datasets.BuilderConfig(name="documents", version=VERSION, description="Plain text portion of the corpus: whole documents"),
32
+ datasets.BuilderConfig(name="paragraphs", version=VERSION, description="Plain text portion of the corpus: paragraphs"),
33
+ datasets.BuilderConfig(name="pos", version=VERSION, description="Part-of-speech tagging subset"),
34
+ ]
35
+
36
+ def _info(self):
37
+ if self.config.name == "documents" or self.config.name == "paragraphs":
38
+ features = datasets.Features(
39
+ {
40
+ "title": datasets.Value("string"),
41
+ "doc_id": datasets.Value("string"),
42
+ "author": datasets.Value("string"),
43
+ "date": datasets.Value("string"),
44
+ "text": datasets.Value("string"),
45
+ "classes": datasets.Sequence(datasets.Value("string"))
46
+ }
47
+ )
48
+ else:
49
+ features = datasets.Features(
50
+ {
51
+ "title": datasets.Value("string"),
52
+ "doc_id": datasets.Value("string"),
53
+ "author": datasets.Value("string"),
54
+ "date": datasets.Value("string"),
55
+ "classes": datasets.Sequence(datasets.Value("string")),
56
+ "words": datasets.Sequence(datasets.Value("string")),
57
+ "pos": datasets.Sequence(datasets.Value("string"))
58
+ }
59
+ )
60
+
61
+ return datasets.DatasetInfo(
62
+ description=_DESCRIPTION,
63
+ features=features,
64
+ supervised_keys=None,
65
+ citation=_CITATION,
66
+ )
67
+
68
+ def _split_generators(self, dl_manager):
69
+ """Returns SplitGenerators."""
70
+ manual_dir = os.path.abspath(os.path.expanduser(dl_manager.manual_dir))
71
+
72
+ if not os.path.exists(manual_dir):
73
+ raise FileNotFoundError(
74
+ "{} does not exist. Make sure you insert a manual dir via `datasets.load_dataset('jimregan/ncg', data_dir=...)` with the path to the corpus directory".format(
75
+ manual_dir
76
+ )
77
+ )
78
+
79
+ return [
80
+ datasets.SplitGenerator(
81
+ name=datasets.Split.TRAIN,
82
+ gen_kwargs={
83
+ "data_dir": manual_dir,
84
+ "split": "train",
85
+ },
86
+ ),
87
+ ]
88
+
89
+ def _generate_examples(
90
+ self, data_dir, split
91
+ ):
92
+ """ Yields examples as (key, example) tuples. """
93
+
94
+ if self.config.name == "documents" or self.config.name == "paragraphs":
95
+ dirs = _TEXTDIRS
96
+ else:
97
+ dirs = ["pos"]
98
+
99
+ cng_path = Path(data_dir)
100
+
101
+ _id = 1
102
+ for dir in dirs:
103
+ dir_path = cng_path / dir
104
+ for filepath in dir_path.glob('*.SGM'):
105
+ with open(filepath, encoding="utf-16-le") as f:
106
+ fid = filepath.stem
107
+ content = f.read()
108
+ soup = BeautifulSoup(content, 'html.parser')
109
+ title = _get_title(soup)
110
+ author = _get_author(soup)
111
+ classes = _get_categories(content)
112
+ date = _get_creation(soup)
113
+ if self.config.name == "pos":
114
+ for sent in _get_pos(soup):
115
+ words = [tok["word"] for tok in sent]
116
+ tags = [tok["msd"] for tok in sent]
117
+ yield _id, {
118
+ "title": title,
119
+ "doc_id": fid,
120
+ "author": author,
121
+ "date": date,
122
+ "classes": classes,
123
+ "words": words,
124
+ "pos": tags
125
+ }
126
+ _id += 1
127
+ else:
128
+ text = _get_paragraphs(soup)
129
+ if self.config.name == "documents":
130
+ text = ["\n".join(text)]
131
+ for para in text:
132
+ yield _id, {
133
+ "title": title,
134
+ "doc_id": fid,
135
+ "author": author,
136
+ "date": date,
137
+ "classes": classes,
138
+ "text": para
139
+ }
140
+ _id += 1
141
+
142
+
143
+ def _get_title(soup):
144
+ title = soup.find("title")
145
+ if title.text and title.text.strip() != "":
146
+ return title.text.strip()
147
+
148
+
149
+ def _get_author(soup):
150
+ author = soup.find("author")
151
+ if author.text and author.text.strip() != "":
152
+ return author.text.strip()
153
+
154
+
155
+ def _get_creation(soup):
156
+ creation = soup.find("creation")
157
+ if creation.text and creation.text.strip() != "":
158
+ return creation.text.strip()
159
+
160
+
161
+ def _get_paragraphs(soup):
162
+ import re
163
+ out = []
164
+ body = soup.find('body')
165
+ for p in body.find_all(['p', 'head']):
166
+ text = p.text.strip()
167
+ text = text.replace('\n', ' ')
168
+ text = re.sub(' +', ' ', text)
169
+ if text:
170
+ out.append(text)
171
+ return out
172
+
173
+
174
+ def _get_categories(text):
175
+ import re
176
+ out = []
177
+ for cat in re.findall('<catRef target="([^"]+)">', text):
178
+ out.append(cat)
179
+ return out
180
+
181
+
182
+ def _get_pos(soup):
183
+ out = []
184
+ for sent in soup.find_all('s'):
185
+ words = []
186
+ for word in sent.find_all('w'):
187
+ if word.text:
188
+ text = word.text.strip()
189
+ msd = word.get('msd')
190
+ if msd and text:
191
+ words.append({"msd": msd, "word": text})
192
+ out.append(words)
193
+ return out