-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_clue_data.py
81 lines (63 loc) · 2.06 KB
/
parse_clue_data.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
'''
Sam Hage
Script to take data from clue database and put it in the format I want
11/205
'''
import re
SPACE_BUFFER = ' ' # element found on almost line between clue and answer
ORIGINAL_FILE = 'assets/clues.txt'
NEW_FILE = 'assets/clues-good.txt'
def main():
"""
**********************************************************************************************************************
Main code to run on program execution
"""
## commented out so I don't accidentally wipe my data ##
# remove_unnecessary_info()
# sort_answers()
def remove_unnecessary_info():
"""
**********************************************************************************************************************
Preserve only the clue and answer from the data
"""
orig = open( ORIGINAL_FILE, 'r' )
new = open( NEW_FILE, 'w' )
for line in orig:
start_of_garbage = line.rfind( SPACE_BUFFER ) + 4
end_of_garbage = start_of_garbage + 10
new_line = line[ : start_of_garbage ] + line[ end_of_garbage : ]
new.write( new_line )
orig.close()
new.close()
def sort_answers():
"""
**********************************************************************************************************************
Store the clues and answers as tuples, switch their order, then flip them
"""
orig = open( NEW_FILE, 'r' )
unsorted_pairs = []
non_white_space = re.compile( '[^\s]' )
## grab clue answer pair ##
for line in orig:
end_of_answer = line.find( ' ' )
answer = line[ : end_of_answer ]
line = line[ end_of_answer : ]
find_non_white = re.search( non_white_space, line )
if find_non_white:
start_of_clue = find_non_white.start()
clue = line[ start_of_clue : ].strip()
pair = ( clue, answer )
unsorted_pairs.append( pair )
else:
print( line )
orig.close()
## sort the pairs alphabetically by clue and remove duplicates ##
sorted_pairs = sorted( list( set( unsorted_pairs ) ) )
## write the sorted pairs back out ##
new = open( NEW_FILE, 'w' )
for pair in sorted_pairs:
new_line = str( pair ) + '\n'
new.write( new_line )
new.close()
if __name__ == '__main__':
main()