-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlettergen2
More file actions
executable file
·48 lines (37 loc) · 1.16 KB
/
Copy pathlettergen2
File metadata and controls
executable file
·48 lines (37 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python3
from collections import defaultdict
import sys
import re
args = sys.argv[1:]
NTILES = int(args.pop(0))
RE_VALID_WORD = re.compile(r'^([a-z][a-z]){1,%s}$' % NTILES)
RE_REPEATED_PAIR = re.compile(r'''
^(..)*
(?P<letter1>.)(?P<letter2>.)
(?P=letter1)(?P=letter2)
''', re.X)
freqs = defaultdict(lambda: 0)
words = []
for word in open(args.pop(0)) if args else sys.stdin:
word = word.rstrip('\n')
# discard words with odd length, capitals, apostrophes.
if not RE_VALID_WORD.match(word):
continue
# split the word into letter pairs.
pairs = re.findall(r'.{2}', word)
# discard words with repeated pairs.
if RE_REPEATED_PAIR.match(''.join(sorted(pairs))):
continue
# add to valid word list. update letter pair statistics.
words.append(word)
for pair in pairs:
freqs[pair] += 1
# our tileset is the top N most frequently occurring pairs.
metric = lambda pair: freqs[pair]
tileset = set(sorted(freqs.keys(), key=metric)[-NTILES:])
# report the tileset.
print(','.join(sorted(tileset)))
# report all formable dictionary words.
for word in words:
if not set(re.findall(r'.{2}', word)).difference(tileset):
print(word)