summaryrefslogtreecommitdiff
path: root/lulua/text.py
blob: 8fa9f48f89a80f46e7c01a75cbc35e157ab62be6 (plain)
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# Copyright (c) 2019 lulua contributors
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

"""
Text/corpus handling tools
"""

import sys, argparse, pickle, json, logging, xml.dom.minidom, queue
from io import StringIO, BytesIO
from functools import partial
from itertools import chain
from multiprocessing import Process, Queue, cpu_count, current_process
from subprocess import Popen, PIPE

from tqdm import tqdm
import ebooklib
from ebooklib import epub
import html5lib
from html5lib.filters.base import Filter
import brotli

from .keyboard import defaultKeyboards
from .layout import defaultLayouts
from .writer import Writer

def iterchar (fd):
    batchsize = 1*1024*1024
    while True:
        c = fd.read (batchsize)
        if not c:
            break
        yield from c

class Select (Filter):
    def __init__ (self, source, f):
        Filter.__init__ (self, source)
        self.inside = None
        self.f = f

    def __iter__(self):
        isScript = None
        for token in Filter.__iter__(self):
            ttype = token['type']
            if ttype == 'StartTag':
                tname = token['name']
                tdata = token['data']
                if self.f (token):
                    self.inside = 0
                if tname in {'script', 'style'}:
                    isScript = 0

            if isScript is not None:
                if ttype == 'EndTag':
                    isScript -= 1
                    if isScript <= 0:
                        isScript = None
            elif self.inside is not None:
                if ttype == 'StartTag':
                    self.inside += 1
                if ttype == 'EndTag':
                    self.inside -= 1
                if self.inside <= 0:
                    self.inside = None

                yield token

class HTMLSerializer(object):
    def serialize(self, treewalker):
        for token in treewalker:
            type = token["type"]
            if type == "Doctype":
                pass
            elif type == "Characters":
                yield token['data']
            elif type == "SpaceCharacters":
                yield ' '
            elif type in ("StartTag", "EmptyTag"):
                name = token["name"]
                pass
            elif type == "EndTag":
                name = token["name"]
                if name in ('p', 'div'):
                    yield '\n\n'
            elif type == "Comment":
                pass
            elif type == "Entity":
                name = token["name"]
                key = name + ";"
                if key not in html5lib.constants.entities:
                    self.serializeError("Entity %s not recognized" % name)
                yield entities[key]
            else:
                assert False

class BrotliFile:
    __slots__ = ('decompressor', 'readchunk', 'fd', 'buf')

    def __init__ (self, fd, readchunk=100*1024):
        self.fd = fd
        self.readchunk = readchunk
        self.decompressor = brotli.Decompressor ()
        self.buf = b''

    def __enter__ (self):
        return self

    def __exit__ (self, exc_type, exc_val, exc_tb):
        return True

    def read (self, num=None):
        while not self.decompressor.is_finished ():
            if num is not None and len (self.buf) >= num:
                break
            self.buf += self.decompressor.process (self.fd.read (self.readchunk))
        if num is not None:
            b = self.buf[0:num]
            self.buf = self.buf[num:]
        else:
            b = self.buf
            self.buf = b''
        return b

    def seekable (self):
        return False

    def close (self):
        self.decompressor = None

def filterTar (fd):
    # Python’s tarfile module is painfully slow. We can do better.
    pos = 0
    blocksize = 512
    emptyblock = b'\0'*blocksize

    while True:
        # read header
        header = fd.read (blocksize)
        pos += blocksize
        if header == b'' or header == emptyblock:
            break
        assert header[256:256+8] == b'\0ustar  ', (header[256:256+8])
        size = int (header[124:124+12].rstrip (b'\0'), base=8)

        # read body
        if size > 0:
            yield BytesIO (fd.read (size))
            pos += size

        # align to next 512 byte block
        into = pos%blocksize
        if into != 0:
            pad = blocksize-into
            fd.read (pad)
            pos += pad

def filterBrotli (fd):
    yield BrotliFile (fd)

def filterHtml (selectFunc, fd):
    document = html5lib.parse (fd)
    walker = html5lib.getTreeWalker("etree")
    stream = walker (document)
    s = HTMLSerializer()
    yield ''.join (s.serialize(Select (stream, selectFunc)))

def filterEpub (item):
    """ epub reader """
    book = epub.read_epub (item.rstrip ())
    logging.debug (f'reading ebook {item}')
    for item in book.get_items_of_type (ebooklib.ITEM_DOCUMENT):
        logging.debug (f'got item {item.get_name ()}')
        # XXX: in theory html5lib should be able to detect the encoding of
        # bytes(), but it does not.
        document = html5lib.parse (item.get_content ().decode ('utf-8'))
        walker = html5lib.getTreeWalker("etree")
        stream = walker (document)
        s = HTMLSerializer()
        yield ''.join (s.serialize (stream))

def filterText (fd):
    yield fd.read ().decode ('utf-8')

def filterLines (item):
    """ Read items (i.e. lines) as is """
    yield item

def filterJson (item):
    yield json.loads (item)

def filterFile (item):
    with open (item.rstrip (), 'rb') as fd:
        yield fd

def filterXml (fd):
    try:
        yield xml.dom.minidom.parse (fd)
    except Exception:
        logging.error (f'invalid xml document {fd}')

def getText (nodelist):
    """ Helper to retrieve text from an XML node list """
    rc = []
    for node in nodelist:
        if node.nodeType == node.TEXT_NODE:
            rc.append(node.data)
    return ''.join(rc)

def filterTEI2 (doc):
    """ TEI.2 format used for United Nations parallel corpus """
    out = []
    for text in doc.getElementsByTagName ('text'):
        for body in text.getElementsByTagName ('body'):
            for p in body.getElementsByTagName ('p'):
                for s in p.getElementsByTagName ('s'):
                    out.append (getText (s.childNodes))
                out.append ('')
    yield '\n'.join (out)

def filterOpenSubtitles (doc):
    """
    XML-based format used by the (raw!) OpenSubtitles dump found here:
    http://opus.nlpl.eu/OpenSubtitles-v2018.php
    """

    out = []
    for s in doc.getElementsByTagName ('s'):
        # strip newlines, which are mostly unintentional due to
        # pretty-printed xml structure
        out.append (getText (s.childNodes).strip ())
    yield '\n'.join (out)

def filterMediawikiMarkdown (text):
    """
    Convert mediawiki to markdown
    """
    p = subprocess.Popen (['pandoc', '-f', 'mediawiki', '-t', 'markdown'],
            stdin=subprocess.PIPE, stdout=subprocess.PIPE)
    p.stdin.write (text.encode ('utf-8'))
    p.stdin.close ()
    text = p.stdout.read ().decode ('utf-8')
    ret = p.wait ()
    # make sure we’re not leaking fd’s
    p.stdout.close ()
    del p
    if ret != 0:
        logging.error ('pandoc rejected document')
    else:
        yield text

f = dict(
    aljazeera=lambda x: x['name'] == 'div' and x['data'].get ((None, 'id')) == 'DynamicContentContainer',
    bbcarabic=lambda x: x['name'] == 'div' and x['data'].get ((None, 'property')) == 'articleBody',
    )

filterAvail = dict(
    aljazeera=partial(filterHtml, f['aljazeera']),
    bbcarabic=partial(filterHtml, f['bbcarabic']),
    text=filterText,
    json=filterJson,
    epub=filterEpub,
    tei2=filterTEI2,
    opensubtitles=filterOpenSubtitles,
    lines=filterLines,
    xml=filterXml,
    file=filterFile,
    tar=filterTar,
    mediawikimarkdown=filterMediawikiMarkdown,
    brotli=filterBrotli,
    )

charMap = {
    'ﻻ': 'لا',
    'أ': 'أ',
    'إ': 'إ',
    'ئ': 'ئ',
    'ؤ': 'ؤ',
    ',': '،',
    'آ': 'آ',
    '%': '٪',
    '0': '٠',
    '1': '١',
    '2': '٢',
    '3': '٣',
    '4': '٤',
    '5': '٥',
    '6': '٦',
    '7': '٧',
    '8': '٨',
    '9': '٩',
    '?': '؟',
    ';': '؛',
    'ﻹ': 'لإ',
    'ﻷ': 'لأ',
    # nbsp
    '\u00a0': ' ',
    }

def mapChars (text, m):
    """ For all characters in text, replace if found in map m or keep as-is """
    return ''.join (map (lambda x: m.get (x, x), text))

def apply (fs, items):
    """ Apply the first function fs[0] to all items, flatten the result and repeat """
    if not fs:
        return items
    else:
        return apply (fs[1:], chain.from_iterable (map (fs[0], items)))

from .stats import allStats, makeCombined

def writeWorker (layout, funcs, inq, outq, statusq, benchmark):
    try:
        keyboard = defaultKeyboards['ibmpc105']
        combined = makeCombined (keyboard)
        itemsProcessed = 0

        while True:
            item = inq.get ()
            if item is None:
                break

            # extract (can be multiple texts per item)
            i = 0
            for text in apply (funcs, [item]):
                if benchmark:
                    i += 1
                    continue

                # map chars; make sure we’re using unix line endings, which is
                # only one character
                text = mapChars (text, charMap).replace ('\r\n', '\n')

                logging.debug (text)

                # init a new writer for every item
                w = Writer (layout)
                # stats
                stats = [cls(w) for cls in allStats]
                for match, event in w.type (StringIO (text)):
                    for s in stats:
                        s.process (event)

                for s in stats:
                    combined[s.name].update (s)

                i += 1
            # only update ocasionally, this is an expensive operation
            statusq.put (i)
            itemsProcessed += i
        if itemsProcessed > 0:
            outq.put (combined)
        else:
            outq.put (None)
    except Exception as e:
        # async exceptions
        outq.put (e)

def statusWorker (statusq):
    with tqdm (unit='item', smoothing=0) as bar:
        while True:
            try:
                num = statusq.get (block=True, timeout=1)
                if num is None:
                    break
                bar.update (n=num)
            except queue.Empty:
                bar.update (n=0)

def write ():
    """ Extract corpus source file, convert to plain text, map chars and create stats """

    parser = argparse.ArgumentParser(description='Import text and create stats.')
    parser.add_argument('--benchmark', action='store_true', help='Benchmark filter, no stats')
    parser.add_argument('-j', '--jobs', metavar='NUM',
            default=cpu_count (), type=int, help='Number of parallel jobs')
    parser.add_argument('-k', '--keyboard', metavar='KEYBOARD',
            default='ibmpc105', help='Physical keyboard name')
    parser.add_argument('-v', '--verbose', action='store_true', help='Enable debugging output')
    parser.add_argument('layout', metavar='LAYOUT', help='Keyboard layout name')
    parser.add_argument('filter', metavar='FILTER', choices=filterAvail.keys(), nargs='+', help='Data filter')

    args = parser.parse_args()

    if args.verbose:
        logging.basicConfig (level=logging.DEBUG)
    else:
        logging.basicConfig (level=logging.INFO)

    keyboard = defaultKeyboards[args.keyboard]
    layout = defaultLayouts[args.layout].specialize (keyboard)
    filterSel = [filterAvail[x] for x in args.filter]

    # limit queue sizes to limit memory usage
    inq = Queue (args.jobs*2)
    outq = Queue (args.jobs+1)
    statusq = Queue (args.jobs+1)

    logging.info (f'using {args.jobs} workers')
    workers = []
    for i in range (args.jobs):
        p = Process(target=writeWorker,
                args=(layout, filterSel, inq, outq, statusq, args.benchmark),
                daemon=True,
                name=f'worker-{i}')
        p.start()
        workers.append (p)

    statusp = Process(target=statusWorker,
            args=(statusq,),
            daemon=True,
            name=f'status')
    statusp.start()

    try:
        for l in sys.stdin:
            inq.put (l)

            # something is wrong
            if not outq.empty ():
                break
    except KeyboardInterrupt:
        pass

    # exit workers
    # every one of them will consume exactly one item and write one in return
    for w in workers:
        inq.put (None)
        item = outq.get ()
        if isinstance (item, Exception):
            raise item
        if item is not None:
            pickle.dump (item, sys.stdout.buffer, pickle.HIGHEST_PROTOCOL)
    assert outq.empty ()

    statusq.put (None)
    statusp.join ()

    # and then we can kill them
    for w in workers:
        w.join ()

    return 0

import bz2, sys, json, subprocess
from xml.etree.ElementTree import iterparse
def extractMediawiki ():
    parser = argparse.ArgumentParser(description='Extract raw wikitext from mediawiki dump.')
    parser.add_argument('file', metavar='FILE', help='bzip2-compressed dump')
    args = parser.parse_args()

    with bz2.open (args.file, 'r') as fd:
        for event, elem in iterparse (fd, ['start', 'end']):
            if event == 'end' and elem.tag == '{http://www.mediawiki.org/xml/export-0.10/}text':
                text = ''.join (elem.itertext ())
                json.dump (text, sys.stdout, ensure_ascii=False)
                sys.stdout.write ('\n')