-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput_take.py
50 lines (41 loc) · 1.68 KB
/
input_take.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
import re
from os import getcwd
def parse_input(input_text):
# Regular expression to extract time in format like "07:30 PM" or "8:00pm"
time_regex = r'(\d{1,2}:\d{2} ?(?:AM|PM|am|pm))'
# Find all matches of time in the input text
times = re.findall(time_regex, input_text)
if times:
# Assuming the first time found is the intended time
time_match = times[0]
# Normalize the time format to "hh:mmAM/PM"
formatted_time = time_match.strip().replace(" ", "").upper()
updated_input_text = input_text.replace(time_match, "").replace("at", "").replace("tell me", "").replace("Tell me", "").replace("to", "").strip()
# Combine formatted time with command
formatted_output = f"{formatted_time} = Sir tis is Your {updated_input_text} time"
return formatted_output, formatted_time
else:
return "No valid time found in input", None
def save_to_file(output_text, time, filename):
try:
with open(filename, 'r') as file:
lines = file.readlines()
except FileNotFoundError:
lines = []
time_found = False
with open(filename, 'w') as file:
for line in lines:
if line.startswith(time):
file.write(output_text + '\n')
time_found = True
else:
file.write(line)
if not time_found:
file.write(output_text + '\n')
# Example usage:
def input_manage(input_text):
output, time = parse_input(input_text)
if output != "No valid time found in input":
save_to_file(output, time, fr'{getcwd()}\schedule.txt')
else:
print(output)