Debugging roof and lighting welsh translations

This commit is contained in:
Khalim Conn-Kowlessar 2023-09-07 15:50:30 +03:00
parent 1a1cf80d7e
commit 4edf4ceedb
3 changed files with 25 additions and 5 deletions

View file

@ -5,7 +5,8 @@ from model_data.utils import correct_spelling
class LightingAttributes: class LightingAttributes:
WELSH_TEXT = { WELSH_TEXT = {
"goleuadau ynni-isel ym mhob un ogçör mannau gosod": "low energy lighting in all fixed outlets" "goleuadau ynni-isel ym mhob un ogçör mannau gosod": "low energy lighting in all fixed outlets",
"dim goleuadau ynni-isel": "no low energy lighting"
} }
def __init__(self, description, averages): def __init__(self, description, averages):

View file

@ -22,16 +22,34 @@ class RoofAttributes(Definitions):
self.description: str = description.lower() self.description: str = description.lower()
self.nodata = not description or description in self.DATA_ANOMALY_MATCHES self.nodata = not description or description in self.DATA_ANOMALY_MATCHES
translation = self.WELSH_TEXT.get(self.description) self.welsh_translation_search()
if translation:
self.nodata = False
self.description = translation
if not self.nodata and not any( if not self.nodata and not any(
rt in self.description for rt in self.ROOF_TYPES + self.DWELLING_ABOVE + ["average thermal transmittance"] rt in self.description for rt in self.ROOF_TYPES + self.DWELLING_ABOVE + ["average thermal transmittance"]
): ):
raise ValueError('Invalid description') raise ValueError('Invalid description')
def welsh_translation_search(self):
"""
For some descriptions,
we want to translate, however they have a consistent structure, where the only change
is the thickness of insulation. Instead of manually adding a record for each translation, we
search for regular expressions and translate
"""
insulation_thickness_match = re.search(r"(\d+ mm) o inswleiddio yn y llofft", self.description)
# Step 2: Generalized translation with placeholder
if insulation_thickness_match:
insulation_thickness = insulation_thickness_match.group(1)
self.description = self.description.replace(insulation_thickness_match.group(0), "")
self.description = f"pitched, {insulation_thickness} loft insulation"
else:
translation = self.WELSH_TEXT.get(self.description)
if translation:
self.nodata = False
self.description = translation
def process(self) -> Dict[str, Union[float, str, bool, None]]: def process(self) -> Dict[str, Union[float, str, bool, None]]:
result: Dict[str, Union[float, str, bool, None]] = {} result: Dict[str, Union[float, str, bool, None]] = {}

View file

@ -32,4 +32,5 @@ test_cases = [
{'original_description': 'No Low energy lighting', 'low_energy_proportion': 0}, {'original_description': 'No Low energy lighting', 'low_energy_proportion': 0},
{'original_description': 'Goleuadau ynni-isel mewn 60% oGÇÖr mannau gosod', 'low_energy_proportion': 0.6}, {'original_description': 'Goleuadau ynni-isel mewn 60% oGÇÖr mannau gosod', 'low_energy_proportion': 0.6},
{'original_description': 'Goleuadau ynni-isel ym mhob un oGÇÖr mannau gosod', 'low_energy_proportion': 1}, {'original_description': 'Goleuadau ynni-isel ym mhob un oGÇÖr mannau gosod', 'low_energy_proportion': 1},
{'original_description': 'Dim goleuadau ynni-isel', 'low_energy_proportion': 0},
] ]