aseifert commited on
Commit
d528187
1 Parent(s): da9c45e

add notebook that was used to load the data

Browse files
Files changed (1) hide show
  1. load_dataset_merlin.ipynb +142 -0
load_dataset_merlin.ipynb ADDED
@@ -0,0 +1,142 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": null,
6
+ "id": "7301572a-4803-4a16-b262-74e41e25803e",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "from pathlib import Path\n",
11
+ "\n",
12
+ "import pandas as pd\n",
13
+ "from datasets import Dataset, DatasetDict, load_dataset\n",
14
+ "from huggingface_hub import Repository, create_repo\n",
15
+ "from selectolax.parser import HTMLParser"
16
+ ]
17
+ },
18
+ {
19
+ "cell_type": "code",
20
+ "execution_count": null,
21
+ "id": "a898baed-3640-4ca8-9d3d-b88f6c85a428",
22
+ "metadata": {},
23
+ "outputs": [],
24
+ "source": [
25
+ "def _parse_start_end(node):\n",
26
+ " return int(node.attrs[\"start\"][1:]), int(node.attrs[\"end\"][1:])\n",
27
+ "\n",
28
+ "\n",
29
+ "def get_original_text(sent_toks) -> str:\n",
30
+ " empty_tokens = [i for i, t in enumerate(sent_toks) if not t.text().strip()]\n",
31
+ " org_sent_toks = [t.text() for i, t in enumerate(sent_toks) if not i in empty_tokens]\n",
32
+ " return \" \".join(org_sent_toks)\n",
33
+ "\n",
34
+ "\n",
35
+ "def get_corrected_text(toks_cor, last_end, sent_end) -> str:\n",
36
+ " cor_toks = []\n",
37
+ " for tok in toks_cor:\n",
38
+ " tok_start, tok_end = _parse_start_end(tok)\n",
39
+ " if tok_start >= last_end and tok_end <= sent_end:\n",
40
+ " cor_toks.append(tok.text())\n",
41
+ " last_end = tok_end\n",
42
+ " return last_end, \" \".join(cor_toks)\n",
43
+ "\n",
44
+ "\n",
45
+ "def process_doc(doc, path):\n",
46
+ " toks = doc.select('tier[category=\"tok\"] event').matches\n",
47
+ " toks_cor = doc.select('tier[category=\"TH1\"] event').matches\n",
48
+ " sents = doc.select('tier[category=\"sentence\"] event').matches\n",
49
+ "\n",
50
+ " last_end = 0\n",
51
+ " for sent_no, org_sent in enumerate(sents):\n",
52
+ " sent_start, sent_end = _parse_start_end(org_sent)\n",
53
+ " sent_toks = toks[sent_start:sent_end]\n",
54
+ " original_text = get_original_text(sent_toks)\n",
55
+ " last_end, corrected_text = get_corrected_text(toks_cor, last_end, sent_end)\n",
56
+ "\n",
57
+ " yield (\n",
58
+ " {\n",
59
+ " \"original\": original_text,\n",
60
+ " \"corrected\": corrected_text,\n",
61
+ " \"id\": f\"{path.stem}-{sent_no}\",\n",
62
+ " }\n",
63
+ " )"
64
+ ]
65
+ },
66
+ {
67
+ "cell_type": "code",
68
+ "execution_count": null,
69
+ "id": "191fe2e3-8a4e-47e2-9316-5b6028662c02",
70
+ "metadata": {},
71
+ "outputs": [],
72
+ "source": [
73
+ "DATASET_NAME = \"merlin\"\n",
74
+ "dataset_path = Path.home() / DATASET_NAME\n",
75
+ "if not Path(dataset_path).exists():\n",
76
+ " repo_url = create_repo(name=DATASET_NAME, repo_type=\"dataset\")\n",
77
+ " repo = Repository(local_dir=str(dataset_path), clone_from=repo_url)\n",
78
+ " repo.lfs_track(\"*.jsonl\")"
79
+ ]
80
+ },
81
+ {
82
+ "cell_type": "code",
83
+ "execution_count": null,
84
+ "id": "005d059f-5de4-4f14-bde3-0cc9ff2435c0",
85
+ "metadata": {},
86
+ "outputs": [],
87
+ "source": [
88
+ "MERLN_EXMARALDA_BASE = Path.home() / Path(\n",
89
+ " \"Downloads/MERLIN Written Learner Corpus for Czech, German, Italian 1.1/merlin-exmaralda-v1.1/\"\n",
90
+ ")\n",
91
+ "\n",
92
+ "for lang in (\"german\", \"czech\", \"italian\"):\n",
93
+ " lang_docs = []\n",
94
+ " for path in (MERLN_EXMARALDA_BASE / lang).glob(\"*.exb\"):\n",
95
+ " with open(path) as fp:\n",
96
+ " xml = HTMLParser(fp.read())\n",
97
+ " docs = list(process_doc(xml, path))\n",
98
+ " lang_docs.extend(docs)\n",
99
+ " Dataset.from_dict(pd.DataFrame(lang_docs)).to_json(dataset_path / f\"{lang}.jsonl\")"
100
+ ]
101
+ },
102
+ {
103
+ "cell_type": "code",
104
+ "execution_count": null,
105
+ "id": "f9d396b2-98dc-4c04-950f-0332a3a6d751",
106
+ "metadata": {},
107
+ "outputs": [],
108
+ "source": [
109
+ "repo.push_to_hub()"
110
+ ]
111
+ },
112
+ {
113
+ "cell_type": "code",
114
+ "execution_count": null,
115
+ "id": "91377eaf-ffac-4df0-9c85-4f6ee979f99f",
116
+ "metadata": {},
117
+ "outputs": [],
118
+ "source": []
119
+ }
120
+ ],
121
+ "metadata": {
122
+ "kernelspec": {
123
+ "display_name": "Python 3 (ipykernel)",
124
+ "language": "python",
125
+ "name": "python3"
126
+ },
127
+ "language_info": {
128
+ "codemirror_mode": {
129
+ "name": "ipython",
130
+ "version": 3
131
+ },
132
+ "file_extension": ".py",
133
+ "mimetype": "text/x-python",
134
+ "name": "python",
135
+ "nbconvert_exporter": "python",
136
+ "pygments_lexer": "ipython3",
137
+ "version": "3.9.7"
138
+ }
139
+ },
140
+ "nbformat": 4,
141
+ "nbformat_minor": 5
142
+ }