Model/epc_data/attributes/attribute_utils.py
2023-06-14 10:23:27 +01:00

61 lines
2.3 KiB
Python

import re
from typing import Tuple, Union, Dict
THERMAL_TRANSMITTENCE_STR = r"average thermal transmittance (-?\d+\.\d+)\s(w/m-¦k)"
THERMAL_TRANSMITTANCE_REGEX = re.compile(THERMAL_TRANSMITTENCE_STR)
def extract_thermal_transmittance(result: dict, description: str) -> Tuple[
Dict[str, Union[None, str, float]], str
]:
"""
Extracts thermal transmittance from the description and updates the result dictionary.
:param result: Dictionary to store the result in.
:param description: Lowercase description string.
:return: A tuple containing the updated result dictionary and the description with the thermal transmittance part
removed.
"""
match = THERMAL_TRANSMITTANCE_REGEX.search(description)
if match:
result['thermal_transmittance'] = float(match.group(1))
result['thermal_transmittance_unit'] = match.group(2)
# Remove the match from the description
description = re.sub(THERMAL_TRANSMITTENCE_STR, "", description)
else:
result['thermal_transmittance'] = None
result['thermal_transmittance_unit'] = None
return result, description
def extract_component_types(result: dict, description: str, list_of_components: list) -> Tuple[
Dict[str, Union[None, str, float]], str
]:
"""
Extracts component types from the description, updates the result dictionary, and removes the matched component
types from the description.
:param result: Dictionary to store the results in.
:param description: Lowercase description string.
:param list_of_components: List of component types to extract from the description.
:return: A tuple containing the updated result dictionary and the description with the matched component types
removed.
"""
for component in list_of_components:
result[f'is_{component.replace(" ", "_")}'] = component in description
# Remove the component from the description
description = description.replace(component, "")
return result, description
def clean_description(description: str) -> str:
"""
Clean the description by replacing any special characters with a space.
"""
special_chars = [":", ";", "*", "@", "?", "!", "(", ")"]
for char in special_chars:
description = description.replace(char, " ")
return description