Model/model_data/utils.py
2023-08-18 16:49:03 +01:00

80 lines
1.9 KiB
Python

import re
from textblob import TextBlob
# Pre-compile the regular expression
PERCENTAGE_PATTERN = re.compile(r'^\d+%?$')
def is_percentage_or_number(s):
# re.match returns None if the string does not match the pattern
return PERCENTAGE_PATTERN.match(s) is not None
def correct_spelling(text):
words = text.split()
corrected_words = []
for word in words:
if is_percentage_or_number(word):
corrected_words.append(word)
else:
blob = TextBlob(word) # create a TextBlob object
corrected_word = blob.correct() # use the correct method to correct spelling
corrected_words.append(str(corrected_word)) # convert corrected word back to string
corrected_text = ' '.join(corrected_words)
return corrected_text
def sap_to_epc(sap_points: int):
"""
Simple utility function to convert SAP points to EPC rating.
:param sapPoints: numerical value of SAP points, typically between 0 and 100
:return:
"""
if sap_points <= 0 or sap_points > 100:
raise ValueError("SAP points should be between 1 and 100.")
if sap_points > 91:
return "A"
elif sap_points > 80:
return "B"
elif sap_points > 69:
return "C"
elif sap_points > 55:
return "D"
elif sap_points > 39:
return "E"
elif sap_points > 21:
return "F"
else:
return "G"
def epc_to_sap_lower_bound(epc: str):
"""
Given an EPC rating, returns the lower bound SAP score required
to hit that EPC rating
:param epc: EPC rating, between A and G
:return:
"""
if epc == "A":
return 92
elif epc == "B":
return 81
elif epc == "C":
return 70
elif epc == "D":
return 56
elif epc == "E":
return 40
elif epc == "F":
return 22
elif epc == "G":
return 1
else:
raise ValueError("EPC rating should be between A and G")