Skip to content

Instantly share code, notes, and snippets.

@sosi-deadeye
Created March 25, 2020 00:57
Show Gist options
  • Save sosi-deadeye/a35ca5870bdaf1af91068c89c91312e3 to your computer and use it in GitHub Desktop.
Save sosi-deadeye/a35ca5870bdaf1af91068c89c91312e3 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
"""
Das Programm lädt ein Wörterbuch herunter.
Alle Wörter mit 6 Zeichen und der Summe 66 werden ausgegeben
"""
import os
import sys
from argparse import ArgumentParser
from contextlib import contextmanager
from pathlib import Path
from urllib.request import urlopen
from string import ascii_uppercase as uppercase
from zipfile import ZipFile
from io import BytesIO
URL = "http://www.winedt.org/dict/de_neu.zip"
def filter_dict(fd):
return [
word
for word in map(str.strip, fd.read().decode("utf16").upper().splitlines())
if all(c in uppercase for c in word)
]
def sum66(de_dict):
return [
word
for word in de_dict
if len(word) == 6 and sum(uppercase.index(c) + 1 for c in word) == 66
]
def downloader():
archive = Path("de_neu.zip")
if not archive.exists():
urlretrieve(URL)
return BytesIO(archive.read_bytes())
def ram_downloader():
return BytesIO(urlopen(URL).read())
@contextmanager
def archive_opener(local: bool):
if local:
zip_data = downloader()
with ZipFile(zip_data) as zf:
with zf.open("de_neu.dic") as fd:
yield fd
else:
zip_data = ram_downloader()
with ZipFile(zip_data) as zf:
with zf.open("de_neu.dic") as fd:
yield fd
def main(local: bool, custom_words: list = None):
with archive_opener(local) as fd:
de_dict = filter_dict(fd)
if custom_words:
de_dict.extend(map(str.upper, custom_words))
print(f"{len(de_dict)} Wörter im Wörterbuch nach der Filterung", file=sys.stderr)
words_sum66 = sorted(set(sum66(de_dict)))
print(f"{len(words_sum66)} Wörter erfüllen die Kriterien", file=sys.stderr)
return words_sum66
if __name__ == "__main__":
parser = ArgumentParser(description=__doc__)
parser.add_argument(
"-t",
action="store_false",
help="Das Archiv mit dem Wörterbuch nicht auf Festplatte speichern.",
)
parser.add_argument(
"--output",
action="store",
type=Path,
help="Die Ausgabe in einer Textdatei speichern.",
)
parser.add_argument(
"--wörter",
metavar="wort",
nargs="+",
help="Zusätzliche Wörter getrennt durch ein Leerzeichen",
)
args = parser.parse_args()
if args.output:
with args.output.open("w") as fd:
fd.writelines(line + os.linesep for line in main(args.t, args.wörter))
else:
for line in main(args.t, args.wörter):
print(line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment