Merged in task/unit-tests-investment-and-client (pull request #346)
Task/unit tests investment and client * move io_utils.py * add __init__.py * refactor: move string_funcs and update import paths to use utils namespace * rename string_funcs to string_utils * move and rename table_utils.py * refactor: rename claude_funcs to llm_utils in multiple files * update unit tests * added unit tests for src/string_utils.py * added unit tests for src/table_utils.py * added unit tests for src/llm_utils.py * added unit tests for src/io_utils.py * Merge branch 'main' into task/unit-tests-investment-and-client; TODO: fix unit tests * Unit tests to fix * fix unit tests * fix invalid references Approved-by: Alex Galarce
This commit is contained in:
committed by
Alex Galarce
parent
53b38ea15c
commit
ab71bd8dae
@@ -69,8 +69,20 @@ def extract_text_from_delimiters(
|
||||
|
||||
return matches[match_index]
|
||||
|
||||
def json_parsing_search(response_text: str, field_list: list[str]) -> dict[str, str]:
|
||||
"""
|
||||
Parses a JSON-like string to extract values for fields.
|
||||
|
||||
def json_parsing_search(response_text, field_list):
|
||||
This function attempts to clean and format the input string to resemble a valid JSON structure.
|
||||
It then searches for the specified fields within the string and extracts their corresponding values.
|
||||
|
||||
Parameters:
|
||||
response_text (str): The JSON-like string to parse.
|
||||
field_list (list[str]): A list of field names to search for in the response_text.
|
||||
|
||||
Returns:
|
||||
dict[str, str]: A dictionary where keys are field names and values are the extracted values from the response_text.
|
||||
"""
|
||||
try:
|
||||
if response_text.split("{", 1)[1].strip()[0] == '"':
|
||||
response_text = "{" + response_text.split("{", 1)[1]
|
||||
@@ -86,14 +98,18 @@ def json_parsing_search(response_text, field_list):
|
||||
response_text = response_text.rsplit("}", 1)[0]
|
||||
else:
|
||||
response_text = response_text.rstrip(",") + "}"
|
||||
|
||||
field_l = []
|
||||
answer_l = []
|
||||
position_dict = {}
|
||||
|
||||
for f in field_list:
|
||||
location = response_text.find('"' + f + '"')
|
||||
if location != -1:
|
||||
position_dict[location] = f
|
||||
|
||||
field_list = list(dict(sorted(position_dict.items())).values())
|
||||
|
||||
for f in field_list:
|
||||
if f in response_text:
|
||||
field_l.append(f)
|
||||
@@ -135,7 +151,7 @@ def json_parsing_search(response_text, field_list):
|
||||
return dict(zip(field_l, answer_l))
|
||||
|
||||
|
||||
def secondary_string_to_dict(dict_string, filename):
|
||||
def secondary_string_to_dict(dict_string: str, filename: str) -> dict:
|
||||
"""
|
||||
Converts a string representation of a dictionary into an actual dictionary object, handling potential formatting issues.
|
||||
|
||||
@@ -145,6 +161,7 @@ def secondary_string_to_dict(dict_string, filename):
|
||||
|
||||
Parameters:
|
||||
dict_string (str): The string containing the dictionary-like content, potentially surrounded by extra text or characters.
|
||||
filename (str): The filename associated with these entries, used to tag each resulting dictionary.
|
||||
|
||||
Returns:
|
||||
dict: The dictionary obtained from parsing the cleaned and corrected string.
|
||||
|
||||
Reference in New Issue
Block a user