Renamed wordsearch, added example problem
[ou-summer-of-code-2017.git] / 07-word-chains / build-word-lists.ipynb
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 3,
6 "metadata": {
7 "collapsed": true
8 },
9 "outputs": [],
10 "source": [
11 "import unicodedata\n",
12 "import string"
13 ]
14 },
15 {
16 "cell_type": "code",
17 "execution_count": 4,
18 "metadata": {
19 "collapsed": true
20 },
21 "outputs": [],
22 "source": [
23 "unaccent_specials = ''.maketrans({\"’\": \"'\", '“': '\"', '”': '\"'})"
24 ]
25 },
26 {
27 "cell_type": "code",
28 "execution_count": 5,
29 "metadata": {
30 "collapsed": true
31 },
32 "outputs": [],
33 "source": [
34 "def unaccent(text):\n",
35 " translated_text = text.translate(unaccent_specials)\n",
36 " return unicodedata.normalize('NFKD', translated_text).\\\n",
37 " encode('ascii', 'ignore').\\\n",
38 " decode('utf-8')"
39 ]
40 },
41 {
42 "cell_type": "code",
43 "execution_count": 6,
44 "metadata": {
45 "collapsed": true
46 },
47 "outputs": [],
48 "source": [
49 "def only_lower(text):\n",
50 " return all((c in string.ascii_lowercase) for c in text)"
51 ]
52 },
53 {
54 "cell_type": "code",
55 "execution_count": 7,
56 "metadata": {},
57 "outputs": [
58 {
59 "name": "stdout",
60 "output_type": "stream",
61 "text": [
62 "/usr/share/dict/british-english\n",
63 "find: ‘/usr/share/doc/google-chrome-stable’: Permission denied\n",
64 "/usr/share/man/man5/british-english.5.gz\n"
65 ]
66 }
67 ],
68 "source": [
69 "# !find /usr -type f -iname 'british-english*'"
70 ]
71 },
72 {
73 "cell_type": "code",
74 "execution_count": 8,
75 "metadata": {},
76 "outputs": [
77 {
78 "name": "stdout",
79 "output_type": "stream",
80 "text": [
81 "total 2.3M\r\n",
82 "drwxr-xr-x 2 root root 4.0K Dec 29 12:37 .\r\n",
83 "drwxr-xr-x 640 root root 20K Apr 13 17:05 ..\r\n",
84 "-rw-r--r-- 1 root root 917K Oct 23 2011 american-english\r\n",
85 "-rw-r--r-- 1 root root 917K Oct 23 2011 british-english\r\n",
86 "-rw-r--r-- 1 root root 467K Aug 25 2016 cracklib-small\r\n",
87 "-rw-r--r-- 1 root root 199 Aug 29 2016 README.select-wordlist\r\n",
88 "lrwxrwxrwx 1 root root 30 Nov 10 2014 words -> /etc/dictionaries-common/words\r\n",
89 "lrwxrwxrwx 1 root root 16 Jun 18 2014 words.pre-dictionaries-common -> american-english\r\n"
90 ]
91 }
92 ],
93 "source": [
94 "# !ls -lah /usr/share/dict"
95 ]
96 },
97 {
98 "cell_type": "code",
99 "execution_count": 17,
100 "metadata": {
101 "collapsed": true
102 },
103 "outputs": [],
104 "source": [
105 "def rude(word):\n",
106 " return any(w in word \n",
107 " for w in 'piss shit cunt fuck arse crap fart jizz whore bitch'.split())"
108 ]
109 },
110 {
111 "cell_type": "code",
112 "execution_count": 18,
113 "metadata": {},
114 "outputs": [
115 {
116 "data": {
117 "text/plain": [
118 "True"
119 ]
120 },
121 "execution_count": 18,
122 "metadata": {},
123 "output_type": "execute_result"
124 }
125 ],
126 "source": [
127 "rude('fucks')"
128 ]
129 },
130 {
131 "cell_type": "code",
132 "execution_count": 19,
133 "metadata": {
134 "collapsed": true
135 },
136 "outputs": [],
137 "source": [
138 "def words_with_len(n):\n",
139 " return [unaccent(w.strip()) for w in open('/usr/share/dict/british-english').readlines()\n",
140 " if only_lower(unaccent(w.strip()))\n",
141 " if len(unaccent(w.strip())) == n\n",
142 " if not rude(unaccent(w.strip()))]"
143 ]
144 },
145 {
146 "cell_type": "code",
147 "execution_count": 20,
148 "metadata": {},
149 "outputs": [
150 {
151 "name": "stdout",
152 "output_type": "stream",
153 "text": [
154 "2336 4-letter words\n",
155 "4566 5-letter words\n",
156 "7223 6-letter words\n"
157 ]
158 }
159 ],
160 "source": [
161 "dicts = {}\n",
162 "\n",
163 "for n in [4, 5, 6]:\n",
164 " dicts[n] = words_with_len(n)\n",
165 " print('{} {}-letter words'.format(len(dicts[n]), n))\n",
166 " with open('words{}.txt'.format(n), 'w') as f:\n",
167 " f.write('\\n'.join(sorted(set(dicts[n]))))\n"
168 ]
169 },
170 {
171 "cell_type": "code",
172 "execution_count": null,
173 "metadata": {
174 "collapsed": true
175 },
176 "outputs": [],
177 "source": []
178 }
179 ],
180 "metadata": {
181 "kernelspec": {
182 "display_name": "Python 3",
183 "language": "python",
184 "name": "python3"
185 },
186 "language_info": {
187 "codemirror_mode": {
188 "name": "ipython",
189 "version": 3
190 },
191 "file_extension": ".py",
192 "mimetype": "text/x-python",
193 "name": "python",
194 "nbconvert_exporter": "python",
195 "pygments_lexer": "ipython3",
196 "version": "3.5.2+"
197 }
198 },
199 "nbformat": 4,
200 "nbformat_minor": 2
201 }