pszemraj commited on
Commit
a605c0c
1 Parent(s): 5b4a05c

Upload fixing_random_newlines.ipynb

Browse files
Files changed (1) hide show
  1. fixing_random_newlines.ipynb +244 -0
fixing_random_newlines.ipynb ADDED
@@ -0,0 +1,244 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "nbformat": 4,
3
+ "nbformat_minor": 0,
4
+ "metadata": {
5
+ "colab": {
6
+ "provenance": []
7
+ },
8
+ "kernelspec": {
9
+ "name": "python3",
10
+ "display_name": "Python 3"
11
+ },
12
+ "language_info": {
13
+ "name": "python"
14
+ }
15
+ },
16
+ "cells": [
17
+ {
18
+ "cell_type": "code",
19
+ "execution_count": 9,
20
+ "metadata": {
21
+ "colab": {
22
+ "base_uri": "https://localhost:8080/"
23
+ },
24
+ "id": "mvIiEQOaFozF",
25
+ "outputId": "e23b2823-f541-4b99-83a2-75c154f6048a"
26
+ },
27
+ "outputs": [
28
+ {
29
+ "output_type": "stream",
30
+ "name": "stdout",
31
+ "text": [
32
+ "the words \"May I\n",
33
+ "come in?\" should be on one line\n"
34
+ ]
35
+ }
36
+ ],
37
+ "source": [
38
+ "import re\n",
39
+ "from tqdm.auto import trange, tqdm\n",
40
+ "\n",
41
+ "def label_lines_refined(text, show_progress=False):\n",
42
+ " lines = text.split(\"\\n\")\n",
43
+ " labeled_lines = []\n",
44
+ " continuation_pattern = re.compile(r'(?<=[a-z0-9,\\'\"])$(?!\\n\\n)', flags=re.MULTILINE)\n",
45
+ " exclusion_pattern = re.compile(r\"(Mr|Mrs|Dr|Ms)\\.$|^\\*|^\\d+\\.\", flags=re.MULTILINE)\n",
46
+ " likely_continuation_pattern = re.compile(r\"(,|and|or|with)$\", flags=re.MULTILINE)\n",
47
+ " incomplete_sentence_pattern = re.compile(r\"(?<=[a-z])\\.$\", flags=re.MULTILINE)\n",
48
+ "\n",
49
+ " for i in trange(len(lines) - 1, disable=not show_progress):\n",
50
+ " current_line = lines[i].strip()\n",
51
+ " next_line = lines[i + 1].strip()\n",
52
+ "\n",
53
+ " if exclusion_pattern.search(current_line) or exclusion_pattern.search(next_line):\n",
54
+ " labeled_lines.append({\"text\": current_line, \"label\": 0})\n",
55
+ " continue\n",
56
+ "\n",
57
+ " if (\n",
58
+ " likely_continuation_pattern.search(current_line)\n",
59
+ " or incomplete_sentence_pattern.search(current_line)\n",
60
+ " or (\n",
61
+ " continuation_pattern.search(current_line)\n",
62
+ " and next_line\n",
63
+ " and (next_line[0].islower() or next_line.split()[0].istitle())\n",
64
+ " )\n",
65
+ " ):\n",
66
+ " labeled_lines.append({\"text\": current_line, \"label\": 1})\n",
67
+ " else:\n",
68
+ " labeled_lines.append({\"text\": current_line, \"label\": 0})\n",
69
+ "\n",
70
+ " labeled_lines.append({\"text\": lines[-1].strip(), \"label\": 0})\n",
71
+ " return labeled_lines\n",
72
+ "\n",
73
+ "def reconstruct_text(labeled_lines):\n",
74
+ " reconstructed_text = \"\"\n",
75
+ " for i in range(len(labeled_lines)):\n",
76
+ " line = labeled_lines[i][\"text\"]\n",
77
+ " label = labeled_lines[i][\"label\"]\n",
78
+ "\n",
79
+ " if label == 1 and i + 1 < len(labeled_lines):\n",
80
+ " # Append the current line without a newline if it's incorrectly split\n",
81
+ " reconstructed_text += line + \" \"\n",
82
+ " else:\n",
83
+ " # Append the current line with a newline\n",
84
+ " reconstructed_text += line + \"\\n\"\n",
85
+ "\n",
86
+ " return reconstructed_text.strip() # Remove any trailing newline characters\n",
87
+ "\n",
88
+ "\n",
89
+ "# Example usage\n",
90
+ "example_text = \"\"\"\n",
91
+ "the words \"May I\n",
92
+ "come in?\" should be on one line\n",
93
+ "\"\"\"\n",
94
+ "\n",
95
+ "labeled_example_refined = label_lines_refined(example_text)\n",
96
+ "reconstructed_text = reconstruct_text(labeled_example_refined)\n",
97
+ "print(reconstructed_text)\n",
98
+ "\n",
99
+ "# def fix_newlines_v2(example, text_column: str = \"text\"):\n",
100
+ "\n",
101
+ "# labeled_text = label_lines_refined(str(example[text_column]))\n",
102
+ "# return {text_column: reconstruct_text(labeled_text)}\n",
103
+ "\n",
104
+ "\n",
105
+ "# ds_fnv2 = dataset.map(fix_newlines_v2, num_proc=6)\n",
106
+ "# ds_fnv2"
107
+ ]
108
+ },
109
+ {
110
+ "cell_type": "markdown",
111
+ "source": [
112
+ "this ^ does not work because it depends on having more text/puncatuation than that short example"
113
+ ],
114
+ "metadata": {
115
+ "id": "TS8Fbfi5PIXU"
116
+ }
117
+ },
118
+ {
119
+ "cell_type": "code",
120
+ "source": [
121
+ "longer_example = \"\"\"[Illustration: THE GIRL'S OWN PAPER\n",
122
+ "\n",
123
+ "VOL. XX.--NO. 990.] DECEMBER 17, 1898. [PRICE ONE PENNY.]\n",
124
+ "\n",
125
+ "\"OUR HERO.\"\n",
126
+ "\n",
127
+ "BY AGNES GIBERNE, Author of \"Sun, Moon and Stars,\" \"The Girl at the\n",
128
+ "Dower House,\" etc.\n",
129
+ "\n",
130
+ "[Illustration: \"THE HORSES IN QUESTION WERE SOMEWHAT SORRY BEASTS.\"]\n",
131
+ "\n",
132
+ "_All rights reserved._]\n",
133
+ "\n",
134
+ "CHAPTER XII.\n",
135
+ "\n",
136
+ "ORDERED TO VERDUN.\n",
137
+ "\n",
138
+ "\"Mamma! Mother!\" cried Roy, bursting into the sitting-room at\n",
139
+ "Fontainebleau one wintry day. \"Ma'am, what do you think?\"\n",
140
+ "\n",
141
+ "Roy had by this time quite recovered from his illness, though his face still bore evidence of the same in the shape of several small red pits, which had not yet had time to lose their prominence. His eyes sparkled with excitement. Mrs. Baron was on the sofa, resting after a walk with her husband, and Colonel Baron sat near, book in hand. Ivor, who happened to be in rear of them both, made a silencing gesture, but Roy was much too eager to attend, or to read his meaning.\n",
142
+ "\n",
143
+ "\"Only think, ma'am. Do but hear! All of us are ordered off from\n",
144
+ "Fontainebleau\"\"\"\n",
145
+ "\n",
146
+ "\n",
147
+ "print(reconstruct_text(label_lines_refined(longer_example)))"
148
+ ],
149
+ "metadata": {
150
+ "colab": {
151
+ "base_uri": "https://localhost:8080/"
152
+ },
153
+ "id": "o_oadp-7LTUa",
154
+ "outputId": "6589be4f-d71c-4432-ce86-b37de6151828"
155
+ },
156
+ "execution_count": 10,
157
+ "outputs": [
158
+ {
159
+ "output_type": "stream",
160
+ "name": "stdout",
161
+ "text": [
162
+ "[Illustration: THE GIRL'S OWN PAPER\n",
163
+ "\n",
164
+ "VOL. XX.--NO. 990.] DECEMBER 17, 1898. [PRICE ONE PENNY.]\n",
165
+ "\n",
166
+ "\"OUR HERO.\"\n",
167
+ "\n",
168
+ "BY AGNES GIBERNE, Author of \"Sun, Moon and Stars,\" \"The Girl at the Dower House,\" etc. \n",
169
+ "[Illustration: \"THE HORSES IN QUESTION WERE SOMEWHAT SORRY BEASTS.\"]\n",
170
+ "\n",
171
+ "_All rights reserved._]\n",
172
+ "\n",
173
+ "CHAPTER XII.\n",
174
+ "\n",
175
+ "ORDERED TO VERDUN.\n",
176
+ "\n",
177
+ "\"Mamma! Mother!\" cried Roy, bursting into the sitting-room at Fontainebleau one wintry day. \"Ma'am, what do you think?\"\n",
178
+ "\n",
179
+ "Roy had by this time quite recovered from his illness, though his face still bore evidence of the same in the shape of several small red pits, which had not yet had time to lose their prominence. His eyes sparkled with excitement. Mrs. Baron was on the sofa, resting after a walk with her husband, and Colonel Baron sat near, book in hand. Ivor, who happened to be in rear of them both, made a silencing gesture, but Roy was much too eager to attend, or to read his meaning. \n",
180
+ "\"Only think, ma'am. Do but hear! All of us are ordered off from Fontainebleau\n"
181
+ ]
182
+ }
183
+ ]
184
+ },
185
+ {
186
+ "cell_type": "code",
187
+ "source": [
188
+ "ex2 = \"\"\"CHAPTER III.\n",
189
+ "\n",
190
+ "\"You sought to prove how I could love,\n",
191
+ "And my disdain is my reply.\"\n",
192
+ "\n",
193
+ "_Tennyson._\n",
194
+ "\n",
195
+ "Linnaea's first waking thoughts carried with them the conviction that life was different--why was it? Ah, she remembered! Last night's scene came back to her with a rush of feeling that brought the warm colour to her face. Then came the colder and more prosaic feelings which so often come with the morning. Gwendoline would soon be like the others--she would go over to the popular opinion, and Linnaea would be thrust upon her own companionship as before. These thoughts were passing through her mind when she heard a tap at the door, and a voice called, \"May I\n",
196
+ "come in?\"\n",
197
+ "\n",
198
+ "Linnaea opened the door, and there stood Gwendoline, her arms full of knick-knacks of all sorts.\n",
199
+ "\n",
200
+ "\"You are only dressing! I have been\n",
201
+ "\n",
202
+ "\"\"\"\n",
203
+ "\n",
204
+ "print(reconstruct_text(label_lines_refined(ex2)))"
205
+ ],
206
+ "metadata": {
207
+ "colab": {
208
+ "base_uri": "https://localhost:8080/"
209
+ },
210
+ "id": "u27WJquGLdVB",
211
+ "outputId": "71f7cadf-ad6c-427a-841a-a097f96bb519"
212
+ },
213
+ "execution_count": 12,
214
+ "outputs": [
215
+ {
216
+ "output_type": "stream",
217
+ "name": "stdout",
218
+ "text": [
219
+ "CHAPTER III.\n",
220
+ "\n",
221
+ "\"You sought to prove how I could love, And my disdain is my reply.\"\n",
222
+ "\n",
223
+ "_Tennyson._\n",
224
+ "\n",
225
+ "Linnaea's first waking thoughts carried with them the conviction that life was different--why was it? Ah, she remembered! Last night's scene came back to her with a rush of feeling that brought the warm colour to her face. Then came the colder and more prosaic feelings which so often come with the morning. Gwendoline would soon be like the others--she would go over to the popular opinion, and Linnaea would be thrust upon her own companionship as before. These thoughts were passing through her mind when she heard a tap at the door, and a voice called, \"May I\n",
226
+ "come in?\"\n",
227
+ "\n",
228
+ "Linnaea opened the door, and there stood Gwendoline, her arms full of knick-knacks of all sorts. \n",
229
+ "\"You are only dressing! I have been\n"
230
+ ]
231
+ }
232
+ ]
233
+ },
234
+ {
235
+ "cell_type": "code",
236
+ "source": [],
237
+ "metadata": {
238
+ "id": "_skV5Y6YNFtI"
239
+ },
240
+ "execution_count": null,
241
+ "outputs": []
242
+ }
243
+ ]
244
+ }