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