-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
39 lines (35 loc) · 1.47 KB
/
helper.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
import re
import csv
db = "CustomerList.csv"
def split(text):
'''
Company Number --> [Company, Number]
"Company A" Number --> [Company A, Number]
Company A Number --> [Company A, Number]
Company ABC abc Number --> [Company ABC abc, Number]
'''
# Use regex to match a quoted company name and a number or an unquoted company name and a number
if len(text) == 0:
return []
elif " " not in text:
return [text]
match = re.match(r'\"(.+?)\"\s+([\d\.]+)|(.+?)\s+([\d\.]+)', text)
if match:
if match.group(1) and match.group(2):
return [match.group(1), match.group(2)]
else:
return [match.group(3), match.group(4)]
else:
raise ValueError("The input text format is incorrect.")
def get_data_to_prompt():
pre_prompt = ""
with open(db, newline='') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
rate = -1 if int(row["Rates"]) == 0 else float(row["SumRating"]) / int(row["Rates"])
if rate == -1:
pre_prompt += ("Company " + row["Company"] + " does " + row["ServiceType"] + " and does not have any rate yet; ")
else:
pre_prompt += ("Company " + row["Company"] + " does " + row["ServiceType"] + " and its rating is " + str(rate) + " from " + row["Rates"] + " users; ")
pre_prompt += "they offer " + row["Discount"] + f"% discount for Justworks employees.\n"
return pre_prompt