forked from jesstess/oreilly-intermediate-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_sonnet_words.py
More file actions
35 lines (28 loc) · 960 Bytes
/
Copy pathextract_sonnet_words.py
File metadata and controls
35 lines (28 loc) · 960 Bytes
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
import string
sonnets = open("sonnets.txt").readlines()
word_set = set([elt.strip() for elt in open("sowpods.txt")])
sonnet_words = set()
def strip_punctuation(word):
# Remove surrounding punctuation. If there's an apostrophe in the
# middle of the word, skip it.
word.replace("-", " ")
apostrophe_index = word.find("'")
if apostrophe_index != -1:
return None
return word.strip(string.punctuation)
for line in sonnets:
# Split apart hyphenated words.
line_words = line.replace("-", " ").strip().split()
# It's an empty line or a sonnet number; skip it.
if len(line_words) <= 1:
continue
for word in line_words:
stripped = strip_punctuation(word)
if stripped and len(stripped) > 1:
sonnet_words.add(stripped.upper())
sonnet_words = list(sonnet_words)
sonnet_words.sort()
f = open("sonnet_words.txt", "w")
for word in sonnet_words:
f.write(word + "\n")
f.close()