forked from jrlegrand/parserx
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparserx.py
114 lines (102 loc) · 3.44 KB
/
parserx.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
from parsers.sig import *
import sys
import json
class bcolors:
HEADER = "\033[95m"
OKBLUE = "\033[94m"
OKCYAN = "\033[96m"
OKGREEN = "\033[92m"
WARNING = "\033[93m"
FAIL = "\033[91m"
ENDC = "\033[0m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
WHITE = "\033[97m"
def main():
value = get_input()
generate_output(value)
def get_input():
while True:
try:
if len(sys.argv) == 1:
print_usage_instructions()
break
elif sys.argv[1] == "--b":
input_file, output_file = sys.argv[2], sys.argv[3]
return 2
elif sys.argv[1] == "--n":
return 3
elif sys.argv[1] == "--r":
return 4
else:
return 1
except IndexError:
print("Usage: main.py --b input.csv output.csv")
break
def print_usage_instructions():
instructions = [
(
bcolors.BOLD
+ bcolors.WHITE
+ "\n Individual sig usage: "
+ bcolors.ENDC
+ "main.py your sig goes here"
),
(
bcolors.BOLD
+ bcolors.WHITE
+ "\n Individual sig usage with inference: "
+ bcolors.ENDC
+ "main.py --n <NDC> your sig goes here"
),
(
bcolors.BOLD
+ bcolors.WHITE
+ "\n Individual sig usage with inference: "
+ bcolors.ENDC
+ "main.py --r <RxCUI> your sig goes here"
),
(
bcolors.BOLD
+ bcolors.WHITE
+ "\n Bulk sig usage: "
+ bcolors.ENDC
+ " main.py --b input.csv output.csv\n"
),
(
" Bulk sig instructions: \n > Place your input file in the /csv directory.\n"
" > Input files are read from the /csv directory.\n"
" > Output files are written to the /csv/output directory.\n"
" > Enter the input file name (input.csv as default) and output file name (output.csv as default), separated by a space.\n"
),
]
for instruction in instructions:
print(instruction)
def generate_output(n):
if n == 1:
matches = SigParser().parse(" ".join(sys.argv[1:]))
print(json.dumps(matches, indent=4))
elif n == 2:
try:
input_file, output_file = sys.argv[2], sys.argv[3]
if input_file.endswith(".csv") and output_file.endswith(".csv"):
SigParser().parse_sig_csv(input_file, output_file)
print(f"Output written to {output_file}.")
else:
print("Both files must end with .csv. Please try again.")
except ValueError:
print("Invalid. Enter input and output file names separated by a space.")
except FileNotFoundError:
print("Input file not found. Please try again.")
elif n == 3:
results = {}
results['parsed'] = SigParser().parse(" ".join(sys.argv[3:]))
results['inferred'] = SigParser().infer(results['parsed'], ndc=sys.argv[2])
print(json.dumps(results, indent=4))
elif n == 4:
results = {}
results['parsed'] = SigParser().parse(" ".join(sys.argv[3:]))
results['inferred'] = SigParser().infer(results['parsed'], rxcui=sys.argv[2])
print(json.dumps(results, indent=4))
if __name__ == "__main__":
main()