-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram.py
67 lines (46 loc) · 2.03 KB
/
program.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
import re
def main():
try:
print('Exit with Ctrl-C\n')
while True:
print('Please enter your article:')
input_text = input()
# Remove pronounciation, language origin, date of birth, etc.
simplified_text = re.sub('[\(\[].*?[\)\]]', '', input_text)
# Split the term and it's definition around 'is' or 'was'
is_split = simplified_text.partition(' is ')
was_split = simplified_text.partition(' was ')
text_before_is = is_split[0]
text_before_was = was_split[0]
# Found the keyword 'is' first
if len(text_before_is) < len(text_before_was) and is_split[1]:
# Strip surrounding whitespace from the term and it's definition
term = text_before_is.strip()
definition = is_split[2].partition('.')[0].strip()
# Process the completed term and definition
handle_phrase(term + ': ' + definition + '.')
# Found the keyword 'was' first
elif len(text_before_is) > len(text_before_was) and was_split[1]:
# Strip surrounding whitespace from the term and it's definition
term = text_before_was.strip()
definition = was_split[2].partition('.')[0].strip()
# Process the completed term and definition
handle_phrase(term + ': ' + definition + '.')
# Handle incomplete entry
else:
print("\nA definition was not created because an 'is' or 'was' could not be found.\n")
# Allow loop to end gracefully
except KeyboardInterrupt:
pass
# For debugging
except Exception as e:
print(e)
# Process the final phrase
def handle_phrase(full_phrase):
print('\nDefinition created:')
print(full_phrase + '\n')
with open('definitions.txt', 'a') as text_file:
text_file.write(full_phrase + '\n\n')
# Run this code if being executed directly
if __name__ == '__main__':
main()