import re from typing import Dict, Union class RoofAttributes: def __init__(self, description: str): """ :param description: Description of the roof. """ self.description: str = description def process(self) -> Dict[str, Union[float, str, bool, None]]: result: Dict[str, Union[float, str, bool, None]] = {} description = self.description.lower() # thermal transmittance match = re.search(r"average thermal transmittance (-?\d+\.\d+)\s(w/m-¦k)", 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(r"average thermal transmittance (-?\d+\.\d+)\s(w/m-¦k)", "", description) else: result['thermal_transmittance'] = None result['thermal_transmittance_unit'] = None # roof type roof_types = ['pitched', 'roof room', 'loft', 'flat', 'thatched', 'at rafters', 'assumed'] for rt in roof_types: result[f'is_{rt.replace(" ", "_")}'] = rt in description # Remove the roof type from the description description = description.replace(rt, "") result["has_dwelling_above"] = ( "another dwelling above" in description or "other premises above" in description ) for dwelling_above in ["another dwelling above", "other premises above"]: description = description.replace(dwelling_above, "") result["is_valid"] = "invalid" not in description description = description.replace("invalid", "") # insulation thickness thickness_map = { "ceiling insulated": "average", "insulated": "average", "limited": "below average", "no insulation": "none", "limited insulation": "below average", "additional insulation": "above average", } for key, value in thickness_map.items(): if key in description: result['insulation_thickness'] = value # Remove the match from the description # description = description.replace(key, "") break else: # Extract insulation thickness in mm, if present match = re.search(r'(\d+\+?)\s*mm', description) if match: result['insulation_thickness'] = match.group(1) if "insulation_thickness" not in result: result['insulation_thickness'] = None return result