ManZzup commited on
Commit
eeeaeec
·
1 Parent(s): 4c1edc6

feat: initial commit with the frequency parser

Browse files
Files changed (1) hide show
  1. gpt.py +67 -0
gpt.py ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ from text2digits import text2digits
3
+ t2d = text2digits.Text2Digits()
4
+
5
+ def extract_amount_and_frequency(directions):
6
+ # Remove punctuation and parentheses
7
+ clean_directions = re.sub(r'[()\[\]{}.,;!?]', '', directions.lower())
8
+
9
+ # Convert word numbers to int
10
+ clean_directions = t2d.convert(clean_directions)
11
+
12
+ # Define patterns to extract amounts and frequencies
13
+ amount_pattern = r'(\d+(?:\s+\d+/\d+)?)(?:\s*(?:tablets|capsules|pills|mg|ml|g))?'
14
+ frequency_pattern = r'(\d+(?:\.\d+)?)\s*(?:times|doses)?(?:\s*every)?\s*(?:day|week|month|hour|hours)?'
15
+ # Search for amounts and frequencies in the cleaned directions
16
+ amount_match = re.search(amount_pattern, clean_directions, re.I)
17
+ frequency_match = re.search(frequency_pattern, clean_directions, re.I)
18
+
19
+ # Extract and format the results
20
+ amount = amount_match.group(1) if amount_match else None
21
+ frequency = frequency_match.group(1) if frequency_match else None
22
+ if amount and frequency:
23
+ # Convert amount to a numerical value
24
+ amount_value = eval(amount.replace(' ', '+'))
25
+ # Extract common frequency expressions
26
+ frequency_phrases = re.findall(r'twice daily|daily|weekly|monthly|hourly|every \d+ hours', clean_directions, re.I)
27
+ # Calculate the correct frequency
28
+ frequency_value = float(frequency)
29
+ for phrase in frequency_phrases:
30
+ print(phrase)
31
+
32
+ if 'twice daily' in phrase:
33
+ frequency_value *= 2
34
+ elif 'daily' in phrase:
35
+ frequency_value *= 1
36
+ elif 'weekly' in phrase:
37
+ frequency_value /= 7
38
+ elif 'monthly' in phrase:
39
+ frequency_value /= 30
40
+ elif 'hourly' in phrase:
41
+ frequency_value *= 24
42
+ elif 'every' in phrase:
43
+ hours = int(re.search(r'\d+', phrase).group(0))
44
+ frequency_value = 24 / hours
45
+
46
+ day_supply = round(amount_value * frequency_value, 3)
47
+
48
+ return amount_value, frequency_value, day_supply
49
+
50
+ return None, None, None
51
+
52
+ """
53
+ TODO: Fix for these cases
54
+ 1. Administer 1 spray into each nostril in the morning and 1 spray before bedtime (Complex sentences)
55
+ 2. Take 1 tablet (550 mg total) by mouth 2 (two) times a day. (times a day)
56
+ 3. Take 1 tablet 2 days by oral route at bedtime for 30 days.
57
+ """
58
+
59
+ if __name__ == "__main__":
60
+ directions = input("Enter prescription directions: ")
61
+ amount, frequency, day = extract_amount_and_frequency(directions)
62
+ if amount is not None and frequency is not None:
63
+ print(f"Amount: {amount}")
64
+ print(f"Frequency: {frequency}")
65
+ print(f"Daily Supply: {day}")
66
+ else:
67
+ print("No amount or frequency found in the directions.")