1 #! /usr/bin/env python3
3 # This file is under GNU General Public License 3.0
7 from argparse import ArgumentParser
8 from fileinput import FileInput, hook_encoded
9 import re, itertools, sys
15 def log2(x): return log(x) / log(2)
17 word = re.compile(r"(\S*?)(/|\s.*|$)")
18 unwanted = re.compile(r"(^\d|[^']*')")
19 space = re.compile(r'^\s')
21 p = ArgumentParser(description="create dictionary csv out of hunspell data")
22 p.add_argument('--hunspell', '-H', type=str, default="/usr/share/hunspell",
23 help='directory where hunspell dictionary files reside (default: /usr/share/hunspell)')
24 p.add_argument('--lang', '-l', type=str, default="en_US",
25 help='use dictionary for language LANG (default: en_US)')
26 p.add_argument('--encoding', '-e', type=str, default="utf-8",
27 help='file encoding (default: utf-8)')
28 p.add_argument('--full', '-f', action='store_true',
29 help="full list - don't reduce to 65536 words")
34 from icu import UnicodeString, Locale
36 print("warning: PyICU not installed, using fallback", file=sys.stderr)
40 locale = Locale(args.lang)
43 return str(u.toUpper(locale))
46 upper(word.match(line).group(1))
47 for line in FileInput(
48 args.hunspell + "/" + args.lang + ".dic",
49 openhook=hook_encoded(args.encoding)
51 if not space.match(line)
53 _words = [w for w in _all if len(w) > 2 and not unwanted.match(w)]
55 _words = [w for w, g in itertools.groupby(_words)]
58 while len(_words) > 65536 * 2:
61 if len(_words) > 65536:
63 _words = _words[:65536]
64 elif len(_words) < 65536:
66 "warning for {}: only {:.2f} bit in wordlist, that makes {:.2f} bit for 5 words\n".format(
72 _words.extend(_words[:65536-len(_words)])
75 assert len(_words) == 65536, "lenght is {}".format(len(_words))
77 for i, w in enumerate(_words):
78 print("{l},{i},{w},0".format(l=args.lang[:2], i=i, w=w))