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