Model/epc_data/attributes/MainheatAttributes.py
2023-06-13 23:11:45 +01:00

78 lines
3.3 KiB
Python

from typing import Dict, List, Union
class MainHeatAttributes:
HEAT_SYSTEMS = ["boiler", "air source heat pump", "room heaters", "electric storage heaters", "warm air",
"electric underfloor heating", "electric ceiling heating", "community scheme",
"ground source heat pump", "no system present", "portable electric heaters",
"water source heat pump"]
FUEL_TYPES = ["electric", "mains gas", "wood logs", "LPG", "coal", "oil", "wood pellets", "anthracite",
"dual fuel mineral and wood", "smokeless fuel", "lpg"]
DISTRIBUTION_SYSTEMS = ["radiators", "fan coil units", "pipes in screed above insulation",
"pipes in insulated timber floor", "pipes in concrete slab"]
OTHERS = ["assumed", "electricaire", "assumed for most rooms"]
def __init__(self, description: str):
self.description: str = self._clean_description(description.lower())
# Remove special characters
if not description or not any(
rt in self.description for rt in
self.HEAT_SYSTEMS + self.FUEL_TYPES + self.DISTRIBUTION_SYSTEMS + self.OTHERS
):
raise ValueError('Invalid description')
@staticmethod
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
def process(self) -> Dict[str, Union[str, bool]]:
result: Dict[str, Union[str, bool]] = {f'has_{ds.replace(" ", "_")}': False for ds in self.DISTRIBUTION_SYSTEMS}
result.update({f'has_{hs.replace(" ", "_")}': False for hs in self.HEAT_SYSTEMS})
result.update({f'has_{ft.replace(" ", "_")}': False for ft in self.FUEL_TYPES})
result.update({f'has_{ot.replace(" ", "_")}': False for ot in self.OTHERS})
description = self.description.split(',')
# Process each part separately
for part in description:
part = part.strip() # remove leading/trailing white spaces
# Heating Systems
self._process_part(result, part, self.HEAT_SYSTEMS, 'has_')
# Fuel Types
self._process_part(result, part, self.FUEL_TYPES, 'has_')
# Distribution Systems
self._process_part(result, part, self.DISTRIBUTION_SYSTEMS, 'has_')
# Other attributes
self._process_part(result, part, self.OTHERS, 'has_')
# Check for "underfloor" separately in the entire description
if "underfloor" in self.description:
result['has_underfloor_heating'] = True
return result
@staticmethod
def _process_part(result: Dict[str, Union[str, bool]], part: str, attr_list: List[str], prefix: str):
"""
Process a part of the description with a given list of attributes
and update the result dictionary.
"""
part_words = part.split()
for attr in attr_list:
attr_words = attr.split()
if set(attr_words).issubset(set(part_words)):
result[f'{prefix}{attr.replace(" ", "_")}'] = True
return result