Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Project Gutenberg Initialisms
#!/usr/bin/env python3
# an experiment in using a large corpus of phrases to generate initialisms from
import re
import zipfile
from nltk import tokenize
phrases = {}
shorts = {}
with zipfile.ZipFile("") as z:
for f in sorted(z.filelist, key=lambda x: x.filename):
print(f.filename, len(shorts), len(phrases))
for s in tokenize.sent_tokenize('utf8')):
phrase = re.sub(r'[\s]+', ' ', re.sub(r'[^-a-z \s]', '', s.lower()).strip())
short = ''.join(x[0] for x in phrase.split())
if 4 <= len(short) <= 12:
shorts.setdefault(short, []).append((phrase, f.filename))
for _ in range(3):
s = secrets.choice(shorts)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment