From 450b81cb146bb0fafbe2f71f8b7b3a44232c5405 Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Wed, 14 Jun 2023 11:17:59 +0100 Subject: [PATCH] windows class working nicely --- epc_data/attributes/WindowAttributes.py | 47 ++++++++++++++----- .../test_data/test_window_attributes_cases.py | 39 +++++++++++++++ 2 files changed, 73 insertions(+), 13 deletions(-) create mode 100644 epc_data/tests/test_data/test_window_attributes_cases.py diff --git a/epc_data/attributes/WindowAttributes.py b/epc_data/attributes/WindowAttributes.py index 87df696e..e3177ce8 100644 --- a/epc_data/attributes/WindowAttributes.py +++ b/epc_data/attributes/WindowAttributes.py @@ -1,11 +1,20 @@ from typing import Dict, Union -from epc_data.attributes.attribute_utils import clean_description, process_part +from epc_data.attributes.attribute_utils import clean_description class WindowAttributes: GLAZING_KEYWORDS = ["glazing", "glazed", "glaze"] - GLAZING_COVERAGE = ["fully", "mostly", "partial", "some"] - GLAZING_TYPES = ["double", "triple", "secondary", "multiple", "high performance"] + GLAZING_COVERAGE = ["fully", "mostly", "partial", "some", "full", "thoughout"] + GLAZING_TYPES = ["double", "triple", "secondary", "multiple", "high performance", "single"] + + coverage_map = { + "full": "full", + "fully": "full", + "mostly": "most", + "partial": "partial", + "some": "partial", + "throughout": "full" + } def __init__(self, description: str): self.description: str = clean_description(description.lower()) @@ -23,22 +32,34 @@ class WindowAttributes: def process(self) -> Dict[str, Union[str, bool]]: result: Dict[str, Union[str, bool]] = { - f'has_{wt.replace(" ", "_")}': False for wt in self.GLAZING_KEYWORDS + "has_glazing": False, + "glazing_coverage": None, + "glazing_type": None, + "no_data": self.nodata } - result.update({f'is_{gc.replace(" ", "_")}': False for gc in self.GLAZING_COVERAGE}) - result.update({f'is_{gt.replace(" ", "_")}': False for gt in self.GLAZING_TYPES}) - result["no_data"] = self.nodata if self.nodata: return result - description = self.description.split(',') + # We consolidate GLAZING_KEYWORDS into a single attribute + result["has_glazing"] = any(keyword in self.description for keyword in self.GLAZING_KEYWORDS) - # Process each part separately - for part in description: + # For coverage and type, we will only store the first one we find + for part in self.description.split(','): part = part.strip() # remove leading/trailing white spaces - process_part(result, part, self.GLAZING_KEYWORDS, 'has_') - process_part(result, part, self.GLAZING_COVERAGE, 'is_') - process_part(result, part, self.GLAZING_TYPES, 'is_') + if not result["glazing_coverage"]: + for coverage in self.GLAZING_COVERAGE: + if coverage in part: + result["glazing_coverage"] = self.coverage_map[coverage] + break + if not result["glazing_type"]: + for glazing_type in self.GLAZING_TYPES: + if glazing_type in part: + result["glazing_type"] = glazing_type + break + + # If we didn't find any coverage or type, we assume full coverage + if not result["glazing_coverage"]: + result["glazing_coverage"] = "full" return result diff --git a/epc_data/tests/test_data/test_window_attributes_cases.py b/epc_data/tests/test_data/test_window_attributes_cases.py new file mode 100644 index 00000000..c3b944fd --- /dev/null +++ b/epc_data/tests/test_data/test_window_attributes_cases.py @@ -0,0 +1,39 @@ +windows_cases = [ + {'original_description': '', 'has_glazing': False, 'glazing_coverage': None, 'glazing_type': None, 'no_data': True}, + {'original_description': 'Full secondary glazing', 'has_glazing': True, 'glazing_coverage': 'full', + 'glazing_type': 'secondary', 'no_data': False}, + {'original_description': 'Fully double glazed', 'has_glazing': True, 'glazing_coverage': 'full', + 'glazing_type': 'double', 'no_data': False}, + {'original_description': 'Fully triple glazed', 'has_glazing': True, 'glazing_coverage': 'full', + 'glazing_type': 'triple', 'no_data': False}, + {'original_description': 'High performance glazing', 'has_glazing': True, 'glazing_coverage': 'full', + 'glazing_type': 'high performance', 'no_data': False}, + {'original_description': 'Mostly double glazing', 'has_glazing': True, 'glazing_coverage': 'most', + 'glazing_type': 'double', 'no_data': False}, + {'original_description': 'Mostly multiple glazing', 'has_glazing': True, 'glazing_coverage': 'most', + 'glazing_type': 'multiple', 'no_data': False}, + {'original_description': 'Mostly secondary glazing', 'has_glazing': True, 'glazing_coverage': 'most', + 'glazing_type': 'secondary', 'no_data': False}, + {'original_description': 'Mostly triple glazing', 'has_glazing': True, 'glazing_coverage': 'most', + 'glazing_type': 'triple', 'no_data': False}, + {'original_description': 'Multiple glazing throughout', 'has_glazing': True, 'glazing_coverage': 'full', + 'glazing_type': 'multiple', 'no_data': False}, + {'original_description': 'Partial double glazing', 'has_glazing': True, 'glazing_coverage': 'partial', + 'glazing_type': 'double', 'no_data': False}, + {'original_description': 'Partial multiple glazing', 'has_glazing': True, 'glazing_coverage': 'partial', + 'glazing_type': 'multiple', 'no_data': False}, + {'original_description': 'Partial secondary glazing', 'has_glazing': True, 'glazing_coverage': 'partial', + 'glazing_type': 'secondary', 'no_data': False}, + {'original_description': 'Partial triple glazing', 'has_glazing': True, 'glazing_coverage': 'partial', + 'glazing_type': 'triple', 'no_data': False}, + {'original_description': 'Single glazed', 'has_glazing': True, 'glazing_coverage': 'full', 'glazing_type': 'single', + 'no_data': False}, + {'original_description': 'Some double glazing', 'has_glazing': True, 'glazing_coverage': 'partial', + 'glazing_type': 'double', 'no_data': False}, + {'original_description': 'Some multiple glazing', 'has_glazing': True, 'glazing_coverage': 'partial', + 'glazing_type': 'multiple', 'no_data': False}, + {'original_description': 'Some secondary glazing', 'has_glazing': True, 'glazing_coverage': 'partial', + 'glazing_type': 'secondary', 'no_data': False}, + {'original_description': 'Some triple glazing', 'has_glazing': True, 'glazing_coverage': 'partial', + 'glazing_type': 'triple', 'no_data': False} +]