globli-ai / utils /utils.py
Ashad001's picture
project initiated as zip: by Hamza bhai
3384583
Raw
History Blame Contribute Delete
2.19 kB
import os, json
import re
import pandas as pd
def parse_investment_range(investment_range_str):
# If the field is 'N/A', return 0
if investment_range_str == 'N/A':
return 0
# Use regular expressions to find all numbers in the string
numbers = re.findall(r'\d{1,3}(?:,\d{3})*(?:\.\d+)?', investment_range_str)
# Convert the numbers to integers, stripping commas and converting to int
numbers = [int(num.replace(',', '')) for num in numbers]
# Return the maximum value found in the range
return max(numbers)
def safe_convert_to_float(value):
if pd.isna(value) or value == 'N/A':
return None
try:
if isinstance(value, str):
# Remove any non-numeric characters except the decimal point
cleaned_value = re.sub("[^0-9.]", "", value)
# If the cleaned value is empty, return None or a default value
if cleaned_value == '':
return None
return float(cleaned_value)
except ValueError:
return None
# Handle floats and ints that do not need cleaning
if isinstance(value, (int, float)):
return float(value)
return None
def get_range_bounds(range_str):
# Handle the case where the string indicates no savings or investment
if 'no' in range_str.lower():
return (0, 0)
# Check if the range starts with '$'
if range_str.startswith('$'):
numbers = re.findall(r'\d+', range_str.replace(',', ''))
# If not exactly 2 numbers, return (0, 0)
if len(numbers) != 2:
return (0, 0)
# Convert extracted strings to integers
low, high = map(int, numbers)
return low, high
# Handle cases where the amount is specified as being over a certain amount
elif 'over' in range_str.lower():
number = re.findall(r'\d+', range_str.replace(',', ''))
if number:
# As only a lower bound is given, we use a very large number to represent the upper bound
return (int(number[0]), float('inf'))
# Default case if the string does not start with '$' and does not include 'no' or 'over'
return (0, 0)