db/dic2csv.py
author Krista Grothoff <krista@pep-project.org>
Tue, 18 Oct 2016 00:33:42 +0200
branchENGINE-109
changeset 1296 9491e990650c
parent 121 dcefb3741425
child 1417 3507e785e538
permissions -rwxr-xr-x
ENGINE-109: closed branch
vb@2
     1
#! /usr/bin/env python3
vb@2
     2
vb@2
     3
from argparse import ArgumentParser
vb@2
     4
from fileinput import FileInput, hook_encoded
vb@2
     5
import re, itertools, sys
vb@2
     6
vb@2
     7
try:
vb@2
     8
    from math import log2
vb@2
     9
except:
vb@2
    10
    from math import log
vb@2
    11
    def log2(x): return log(x) / log(2)
vb@2
    12
vb@2
    13
word = re.compile(r"(\S*?)(/|\s.*|$)")
vb@2
    14
unwanted = re.compile(r"(^\d|[^']*')")
vb@2
    15
space = re.compile(r'^\s')
vb@2
    16
vb@2
    17
p = ArgumentParser(description="create dictionary csv out of hunspell data")
vb@2
    18
p.add_argument('--hunspell', '-H', type=str, default="/usr/share/hunspell",
vb@2
    19
    help='directory where hunspell dictionary files reside (default: /usr/share/hunspell)')
vb@2
    20
p.add_argument('--lang', '-l', type=str, default="en_US",
vb@2
    21
    help='use dictionary for language LANG (default: en_US)')
vb@2
    22
p.add_argument('--encoding', '-e', type=str, default="utf-8",
vb@2
    23
    help='file encoding (default: utf-8)')
vb@121
    24
p.add_argument('--full', '-f', action='store_true',
vb@121
    25
    help="full list - don't reduce to 65536 words")
vb@2
    26
vb@2
    27
args = p.parse_args()
vb@2
    28
vb@120
    29
try:
vb@120
    30
    from icu import UnicodeString, Locale
vb@120
    31
except ImportError:
vb@120
    32
    print("warning: PyICU not installed, using fallback", file=sys.stderr)
vb@120
    33
    def upper(x):
vb@120
    34
        return x.upper();
vb@120
    35
else:
vb@120
    36
    locale = Locale(args.lang)
vb@120
    37
    def upper(x):
vb@120
    38
        u = UnicodeString(x)
vb@120
    39
        return str(u.toUpper(locale))
vb@120
    40
vb@2
    41
_all = (
vb@120
    42
    upper(word.match(line).group(1))
vb@2
    43
        for line in FileInput(
vb@2
    44
                args.hunspell + "/" + args.lang + ".dic",
vb@2
    45
                openhook=hook_encoded(args.encoding)
vb@2
    46
            )
vb@2
    47
        if not space.match(line)
vb@2
    48
)
vb@2
    49
_words = [w for w in _all if len(w) > 2 and not unwanted.match(w)]
vb@2
    50
_words.sort()
vb@2
    51
_words = [w for w, g in itertools.groupby(_words)]
vb@2
    52
vb@121
    53
if not args.full:
vb@121
    54
    while len(_words) > 65536 * 2:
vb@121
    55
        _words = _words[::2]
vb@120
    56
vb@2
    57
if len(_words) > 65536:
vb@121
    58
    if not args.full:
vb@121
    59
        _words = _words[:65536]
vb@2
    60
elif len(_words) < 65536:
vb@2
    61
    sys.stderr.write(
vb@2
    62
            "warning for {}: only {:.2f} bit in wordlist, that makes {:.2f} bit for 5 words\n".format(
vb@2
    63
                    args.lang,
vb@2
    64
                    log2(len(_words)),
vb@2
    65
                    log2(len(_words))*5
vb@2
    66
                )
vb@2
    67
        )
vb@2
    68
    _words.extend(_words[:65536-len(_words)])
vb@2
    69
vb@121
    70
if not args.full:
vb@121
    71
    assert len(_words) == 65536, "lenght is {}".format(len(_words))
vb@2
    72
vb@2
    73
for i, w in enumerate(_words):
Edouard@4
    74
    print("{l},{i},{w},0".format(l=args.lang[:2], i=i, w=w))