-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
278 lines (240 loc) · 12.4 KB
/
main.py
File metadata and controls
278 lines (240 loc) · 12.4 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import sys
import os
# Uncomment to set configure path to your tesseract program
# os.environ["TESSDATA_PREFIX"] = r"C:\\PATH\ TO\ TESSERACT\\tessdata\ FOLDER"
# Add the Source directory to the Python path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), "Source")))
import argparse
import asyncio
import CC_Trie
import CC_Matcher
from CC_Trie import Trie
from scrapers import archive_scraper
from SuffixTrie import SuffixTrie
def run_cc_trie(function, file="Data/elements_table_v20.txt", elements_file=None, element='H', compound="NH4NO3"):
"""
Function to run the CC_Trie script functionality.
"""
trie = Trie()
try:
# Load element data from file
elements_trie = trie.read_in_dictionary(file_name=file, elements_file=elements_file)
compound_formulas = []
# Get all elements from the trie
if (len(trie.root.children)) < 1:
trie = elements_trie
elements = trie.get_words()
else:
compound_formulas = elements_trie
elements = trie.get_words()
if(function == "chart_mass"):
compound_weights = CC_Trie.chart_element_mass(trie, element)
print(compound_weights)
return
elif(function == "create_matrix"):
# Define x, y, z
if (len(trie.root.children)) < 1:
x = elements_trie.get_node(element).data[0].symbol if elements else None # Starting element (e.g., first from the list)
y = element # Example element to sum recursively
z = element # Another example element to sum recursively
elif (function == "create_matrix"):
# Define x, y, z
if (len(trie.root.children)) < 1:
x = elements_trie.get_node(element).data[
0].symbol if elements else None # Starting element (e.g., first from the list)
y = element # Example element to sum recursively
z = element # Another example element to sum recursively
else:
x = trie.get_node(element).data[0].symbol if elements else None # Starting element (e.g., first from the list)
y = element # Example element to sum recursively
z = element # Another example element to sum recursively
if x is None:
print("No valid elements found in the Trie.")
return
# Create summation matrix
matrix = CC_Trie.create_summation_matrix(trie, elements, x, y, z)
# Plot the resulting matrix
CC_Trie.plot_matrix(matrix, elements)
# Compute sum tables x, y, z for element x
CC_Trie.plot_sum_for_single_element_x(elements, x)
elif function == "chart_compound_mass" or "chart_compound_matrix":
if function == "chart_compound_mass" and len(trie.root.children) < 1:
compound_masses = CC_Trie.chart_compound_mass(elements_trie, compound)
elif function == "chart_compound_mass_grid":
compound_masses = CC_Trie.chart_compound_mass_grid(trie, elements, compound)
else:
compound_masses = CC_Trie.chart_compound_mass(trie, compound)
print(compound_masses)
except FileNotFoundError:
print(f"File {file} not found. Please provide a valid file path.")
def run_suffix_trie(function, file_path="Data/elements_table_v20.txt"):
"""
Main function to test the enhanced SuffixTrie with metadata (SuffixTrieData).
"""
# Create an empty Suffix Trie
suffix_trie = SuffixTrie()
# Load content from the file into the trie
suffix_trie.load_from_file(file_path)
# Check the contents of the loaded text
print("Text:", suffix_trie.text)
while True:
print("\nOptions:")
print("1. Check if a pattern exists in the Suffix Trie")
print("2. Get metadata (positions and frequency) for a suffix/pattern")
print("3. Get all suffixes with a given prefix")
print("4. Exit")
choice = int(input("Enter your choice: "))
if choice == 1:
pattern = input("Enter the pattern to search for: ").strip()
if suffix_trie.contains(pattern):
print("Pattern exists in the Suffix Trie.")
else:
print("Pattern does not exist in the Suffix Trie.")
elif choice == 2:
pattern = input("Enter the pattern to get metadata for: ").strip()
metadata = suffix_trie.get_suffix_metadata(pattern)
if metadata:
print(f"Metadata for '{pattern}': {metadata}")
else:
print(f"No metadata found for '{pattern}'.")
elif choice == 3:
prefix = input("Enter the prefix: ").strip()
suffixes = suffix_trie.get_suffixes_with_prefix(prefix)
print(f"Suffixes starting with '{prefix}': ")
i = 0
for suffix in suffixes:
i += 1
print(f"{i}) {suffix}")
elif choice == 4:
print("Exiting...")
break
else:
print("Invalid choice. Try again.")
def cc_matcher(function, file="Data/elements_table_v20.txt", elements_file=None, compounds=["CH4", "CH4", "CH4"]):
trie = Trie()
file_name = file
try:
# Load element data from file
elements_trie, trie = trie.read_in_dictionary(file_name=file, elements_file=elements_file)
# Get all elements from the trie
if (len(trie.root.children)) < 1:
elements = elements_trie.get_words()
else:
elements = trie.get_words()
except FileNotFoundError:
print(f"File {file_name} not found. Please provide a valid file path.")
if function == "get_cc_constant":
compound_d = CC_Matcher.Get_Mass(compounds[0])
compound_i = CC_Matcher.Get_Mass(compounds[1])
compound_h = CC_Matcher.Get_Mass(compounds[2])
print(f"The C Constant value for {compounds} is {CC_Matcher.Get_C_Constant(compound_d, compound_i, compound_h)}")
elif function == "get_mass":
compounds = [compounds]
formula_quantity = CC_Matcher.Get_Mass(compounds[0])
mass = 0
if len(trie.root.children) < 1:
for e, q in formula_quantity.items():
mass += (elements_trie.get(e).data[0].mass * q)
else:
for e, q in formula_quantity.items():
mass += (trie.get(e).data[0].mass * q)
print(f"molecular mass for {compounds} is {mass}")
async def run_archiver(search_term=None):
"""
Function to run the Archiver functionality.
"""
if search_term:
print(f"Running Archiver with search term: {search_term}")
scraper = archive_scraper.Google_Scraper()
driver = scraper.initChromeDriver(False)
await scraper.google_scraper(driver, search_term)
driver.close()
else:
print("missing search term")
# Add logic for Archiver without a search term here
async def main():
# Create the main parser
parser = argparse.ArgumentParser(
description="usage: <mode> [cc_trie cc_trie_matcher compound_trie_suffix archiver] --<mode-options>")
subparsers = parser.add_subparsers(dest="mode", required=True, help="Mode to run the script.")
# Subparser for cc_trie
cc_trie_parser = subparsers.add_parser("cc_trie", help="Run the CC_Trie functionality.")
cc_trie_parser.add_argument("--function", type=str, required=True, help="functions [chart_compound_mass, get_elements, create_matrix]")
cc_trie_parser.add_argument("--compound", type=str, required=False,
help="from functions chart_compound_mass")
cc_trie_parser.add_argument("--element", type=str, required=False,
help="from functions chart_element")
cc_trie_parser.add_argument("--elements_file", type=str, required=False, help="Path to the elements file.")
cc_trie_parser.add_argument("--file", type=str, required=False, help="Path to the elements compounds file.")
# Subparser for cc_trie_matcher
cc_matcher_parser = subparsers.add_parser("cc_matcher", help="--file <elements_mass_file default: elements_table_v20.txt> --compounds <list 3 formulas in brackets [compound1 compound2 compound3]>")
cc_matcher_parser.add_argument("--function", type=str, required=False,
help="functions [get_cc_constant, cohesion_matrix, constant_matrix]")
cc_matcher_parser.add_argument("--compounds", type=str, required=False,
help="Chemical Formulae of 1 or more seperated by spacing eg. 'CnO2Ca4He CH4 H20'")
cc_matcher_parser.add_argument("--file", type=str, required=False,
help="Path to the elements compounds file (optional).")
# Subparser for compound_trie_suffix
compound_trie_suffix_parser = subparsers.add_parser("compound_trie_suffix",
help="Run the Compound Trie Suffix functionality.")
compound_trie_suffix_parser.add_argument("--function", type=str, required=False,
help="functions [search_suffix, get_metadata, get_data_structure]")
compound_trie_suffix_parser.add_argument("--file", type=str, required=False,
help="Path to the elements compounds file (optional).")
# Subparser for archiver
archiver_parser = subparsers.add_parser("archiver", help="Run the Archiver functionality.")
archiver_parser.add_argument("--search", type=str, required=False,
help="Google search query for the Archiver (use this for normal search mode).")
archiver_parser.add_argument("--search-file", action="store_true",
help="Enable file-driven scraping mode (reads URLs from a file)."
)
archiver_parser.add_argument("--file-name", type=str, required=False,
help="Path to a text file containing URLs (one per line) for file-driven scraping.")
# Parse the arguments
args = parser.parse_args()
# Handle the selected mode
if args.mode == "cc_trie":
print(f"Running trie utility with options --function {args.function} --file {args.file}")
if args.function == "create_matrix":
run_cc_trie(args.function, args.file, args.elements_file, element=args.element)
else:
run_cc_trie(args.function, args.file)
elif args.mode == "compound_trie_suffix":
print(f"Running Trie Suffix utility with options --file {args.file}")
print("Loading data structure from file...")
# Add logic for compound_trie_suffix here
run_suffix_trie(args.function, args.file)
elif args.mode == "cc_matcher":
print("Running Compound Cohesion Constant Matcher...")
compounds = args.compounds.split(" ")
# Add logic for compound_trie_suffix here
cc_matcher(args.function, args.file, compounds=args.compounds)
elif args.mode == "archiver":
scraper = archive_scraper.Google_Scraper()
driver = scraper.initChromeDriver(False)
try:
# File-driven mode: either explicit --search-file or legacy trigger "file-search"
if getattr(args, "search_file", False) or (getattr(args, "search", None) == "file-search"):
if not getattr(args, "file_name", None):
print("Error: --file-name is required when using --search-file or --search file-search.")
return
# Call the file-based scraper
await scraper.scrape_from_file(driver, args.file_name)
else:
# Default: normal Google search mode
if not getattr(args, "search", None):
print("Error: --search is required when not using --search-file.")
return
await scraper.google_scraper(driver, args.search)
finally:
driver.close()
return
if __name__ == "__main__":
if len(sys.argv) == 1:
# If no arguments are provided, show the help message
print("No arguments provided. Showing usage instructions:")
parser = argparse.ArgumentParser(description="usage: <mode> [cc_trie cc_trie_matcher compound_trie_suffix archiver] --<mode-options>")
parser.print_help()
sys.exit(1)
# Run the main function
asyncio.run(main())