-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_shared_functions.py
173 lines (138 loc) · 5.45 KB
/
main_shared_functions.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import pyautogui
import pytesseract
import re
from dateutil.parser import parse
from dateutil.relativedelta import relativedelta
from datetime import datetime
def extract_dates_custom(input_text):
months = {
"January": 1,
"February": 2,
"March": 3,
"April": 4,
"May": 5,
"June": 6,
"July": 7,
"August": 8,
"September": 9,
"October": 10,
"November": 11,
"December": 12,
}
day_month_year_result = day_month_year_only(input_text)
if day_month_year_result is not None:
return day_month_year_result
month_year_result = month_year_only(input_text, months)
if month_year_result is not None:
return month_year_result
year_only_result = year_only(input_text)
if year_only_result is not None:
return year_only_result
# Regular expression pattern to match different date formats.
date_pattern = r"\b(\d{1,2})[ /-](\d{4})\b|\b([A-Za-z]+)[ /-](\d{4})\b"
matches = re.findall(date_pattern, input_text)
if matches:
for match in matches:
if match[0]: # Matched day and year (e.g., 10/2023 or 10-2023)
month, year = match[0], match[1]
return f"{month}/{year}"
elif match[2] in months: # Check if the matched text is a valid month name
month, year = months[match[2]], match[3]
return f"{month}/{year}"
# Handle the case of "month YYYY"
month_year_pattern = r"\b([A-Za-z]+) (\d{4})\b"
month_year_match = re.search(month_year_pattern, input_text)
if month_year_match:
month, year = months[month_year_match.group(1)], month_year_match.group(2)
return f"{month:02d}/{year}"
# Handle special cases for phrases like "last month," "last year," and "this year."
return special_cases(input_text)
def remove_numbers_greater_than_current_year(input_text):
current_year = datetime.now().year
# Regular expression pattern to match numbers in the input text
number_pattern = r"\b\d+\b"
def remove_numbers(match):
number = int(match.group(0))
if number <= current_year:
return str(number)
else:
return ""
return re.sub(number_pattern, remove_numbers, input_text)
def remove_phone_numbers(text):
phone_pattern = (
r"\b(\d{3}[-.\s]?\d{3}[-.\s]?\d{4}|\(\d{3}\)[-.\s]?\d{3}[-.\s]?\d{4})\b"
)
return re.sub(phone_pattern, "", text)
def remove_digits_next_to_letters(input_string):
return re.sub(r"([a-zA-Z])\d+|\d+([a-zA-Z])", r"\1\2", input_string)
def day_month_year_only(input_text):
date_pattern = r"\b(\d{1,2})[ /-](\d{1,2})[ /-](\d{4})\b"
matches = re.findall(date_pattern, input_text)
if matches:
for match in matches:
month, year = match[0], match[2]
if month.startswith("0"):
month = month[1] # Remove leading zero
return f"{month}/{year}"
return None
def year_only(input_text):
year_pattern = r"\b(\d{4})\b"
year_match = re.search(year_pattern, input_text)
if year_match:
year = year_match.group(1)
return f"1/{year}"
return None
def month_year_only(input_text, months):
month_year_pattern = r"\b([A-Za-z]+)[ /-](\d{4})\b"
month_year_match = re.search(month_year_pattern, input_text)
if month_year_match:
month = months.get(month_year_match.group(1))
year = month_year_match.group(2)
if month:
return f"{month}/{year}"
return None
def special_cases(input_text):
CURRENT_DATE = datetime.now()
formatted_month = str(CURRENT_DATE.month)
formatted_year = str(CURRENT_DATE.year)
if "last month" in input_text:
last_month = int(formatted_month) - 1
return f"{last_month}/{formatted_year}"
if "last year" in input_text:
last_year = int(formatted_year) - 1
return f"1/{last_year}"
if "this year" in input_text:
return f"1/{formatted_year}"
if "this month" in input_text:
return f"{formatted_month}/{formatted_year}"
return "1/"
def extract_text_from_coordinates(x1, y1, x2, y2):
# Path to the Tesseract executable, may need to use path to the .exe file depending on the operating system
# path_to_tesseract = "tesseract.exe"
# pytesseract.tesseract_cmd = path_to_tesseract
# Capture the current screen as a screenshot
screenshot = pyautogui.screenshot()
# Crop the screenshot to the specified coordinates
textbox_image = screenshot.crop((x1, y1, x2, y2))
# Use Tesseract to extract text from the cropped image
extracted_text = pytesseract.image_to_string(textbox_image)
# Return the extracted text after stripping any leading/trailing whitespace
return extracted_text.strip()
def cord_click(coordinates):
# Move the mouse pointer to the specified coordinates
pyautogui.moveTo(coordinates[0], coordinates[1])
# Perform a mouse click at the current position
pyautogui.click()
def tab_command(number_of_interactions):
# Simulate pressing the "Tab" key a specified number of times
for _ in range(0, number_of_interactions):
pyautogui.press("tab")
def extract_digits_from_text(text):
# Filter and join only the digits from the input text
return "".join(filter(str.isdigit, text))
def is_text_empty(text):
# Check if the text is None or contains only whitespace
if text is None or len(text.strip()) == 0:
return True
else:
return False