File size: 4,854 Bytes
f006f31
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env python3
import pytest
from langdetect import detect_langs

import mappingservice.dependencies as deps
from mappingservice.config import Settings
from mappingservice.constants import AVAILABLE_LANGUAGES, MODEL_NAMES
from mappingservice.ms.model_loader import ModelLoader
from mappingservice.utils import predict_language

settings = Settings()
ml_model = ModelLoader(settings, MODEL_NAMES)


@pytest.fixture
def classifier():

    for lang in AVAILABLE_LANGUAGES:
        model_pipeline = ml_model.get_model('room_view', lang)
        try:
            deps.mc['room_view'][lang] = model_pipeline
        except KeyError:
            deps.mc['room_view'] = {}
            deps.mc['room_view'][lang] = model_pipeline

    def get_model(language):
        return deps.mc['room_view'][language]

    return get_model


def test_model_predictions(classifier):
    test_data = [
        {'input': 'studio with ruins view', 'expected_response': 'ruins'},  # noqa: E501
        {'input': 'suite with pool view', 'expected_response': 'pool'},
        {'input': 'executive room garden view', 'expected_response': 'garden'},
        {'input': 'studio two bedroom vineyard view 4 guests wine country charm', 'expected_response': 'vineyard'},  # noqa: E501
        {'input': 'superior suite city view', 'expected_response': 'city'},
        {'input': 'room with a balcony and harbour views', 'expected_response': 'harbour'},  # noqa: E501
        {'input': 'junior studio stars views', 'expected_response': 'stars'},
        {'input': 'room park views', 'expected_response': 'park'},
        {'input': 'loft two bedroom and marina view 4 adults coastal luxury', 'expected_response': 'marina'},  # noqa: E501
        {'input': 'house sea view', 'expected_response': 'sea'},
        {'input': 'villa hill views', 'expected_response': 'hill'},
        {'input': 'room park view', 'expected_response': 'park'},
        {'input': 'townhouse one bedroom and park view 2 adults urban charm', 'expected_response': 'park'},  # noqa: E501
        {'input': 'suite garden view', 'expected_response': 'garden'},
        {'input': 'twin room ocean view with balcony', 'expected_response': 'ocean'},
        {'input': 'residencia con vistas al mar', 'expected_response': 'mar'},
        {'input': 'habitacion con vistas a la piscina', 'expected_response': 'piscina'},
        {'input': 'habitacion ejecutiva con vistas al jardin', 'expected_response': 'jardin'},  # noqa: E501
        # {'input': 'habitacion con vistas al oceano', 'expected_response': 'oceano'},  # noqa: E501
        {'input': 'suite superior con vistas a la ciudad', 'expected_response': 'ciudad'},  # noqa: E501
        # {'input': 'habitacion con balcon con vistas a la bahia', 'expected_response': 'bahia'},  # noqa: E501
        {'input': 'estudio junior con vistas a las estrellas', 'expected_response': 'estrellas'},  # noqa: E501
        {'input': 'habitacion con vistas al parque', 'expected_response': 'parque'},
        {'input': 'habitacion con vistas a la ciudad', 'expected_response': 'ciudad'},
        {'input': 'casa con vista al mar', 'expected_response': 'mar'},
        {'input': 'corner suite pool view', 'expected_response': 'pool'},
        {'input': 'estudio con vista al parque', 'expected_response': 'parque'},
        {'input': 'estudio con vista al jardin', 'expected_response': 'jardin'},
        {'input': 'residencia con vista al amazonas', 'expected_response': 'amazonas'},
        {'input': 'habitacion con vista a la montana', 'expected_response': 'montana'},  # noqa: E501
        {'input': 'residencia moderno con vista al park', 'expected_response': 'park'},  # noqa: E501
        {'input': 'dormitorio con vistas al lago', 'expected_response': 'lago'},  # noqa: E501
        # {'input': 'dormitorio con vistas a la bahia', 'expected_response': 'bahia'},
        {'input': 'classic ocean view', 'expected_response': 'ocean'}  # noqa: E501
    ]

    # this test case will always pass untill we solve the mistery with model data
    print("Test data length:", len(test_data))

    for test_case in test_data:
        language = detect_langs(test_case['input'])
        for item in language:
            if item.lang in AVAILABLE_LANGUAGES:
                language = item.lang
                break

        if language not in AVAILABLE_LANGUAGES:
            language = predict_language(test_case['input'])

        cls = classifier(language)
        description = test_case["input"]
        expected_view = test_case["expected_response"]
        processed_results = cls.predict(description)

        predicted_views = [entity["word"] for entity in processed_results]
        assert expected_view in predicted_views, f"Incorrect prediction for '{description}' using '{language}' language: expected '{expected_view}', obtained '{predicted_views}'"  # noqa: E501