def test_get_best_strategy(self):
        address = u'Российская федерация, москва, улица россия, дом 3'
        got = self.splitter.get_best_strategy(address)
        # u',Российская федерация,,москва,улица россия'
        expected = SplitingStrategy(address=address,
                                    index_pos=None,
                                    country_pos=(0, 20),
                                    region_pos=None,
                                    subregion_pos=None,
                                    city_pos=(22, 28),
                                    street_pos=(30, 42),
                                    house_pos=(44, 49))
        self.assertEqual(got, expected)

        # u',россия,,москва,улица россия'
        not_expected = SplitingStrategy(
            address=address,
            index_pos=None,
            country_pos=(36, 42),  # !!!! We do not exect it
            region_pos=None,
            subregion_pos=None,
            city_pos=(22, 28),
            street_pos=(30, 42),
            house_pos=(44, 49))
        self.assertFalse(got == not_expected)

        # Check that we do not calculate strategy second time
        dummy = 'A DUMMY VARIABLE'
        self.splitter._best_strat = dummy
        parts = self.splitter.get_best_strategy(address)
        self.assertEqual(parts, dummy)
    def test_get_space_penalty(self):

        strategy = SplitingStrategy(
            address='ывааааааываырпрарапрапрараорпопопропрпропоппропdddddddр',
            index_pos=None,
            country_pos=None,
            region_pos=None,
            subregion_pos=(15, 31),
            city_pos=(5, 13),
            street_pos=(50, 55),
            house_pos=None,
            poi_pos=None
        )
        self.assertEqual(strategy._get_space_penalty(), 54 - 5)

        strategy = SplitingStrategy(
            address='ывdddddddааааааываырпрапрарапрапрараорпопопропрпропоппр',
            index_pos=(1, 2),
            country_pos=(2, 3),
            region_pos=(3, 4),
            subregion_pos=(4, 5),
            city_pos=(5, 6),
            street_pos=(6, 10),
            house_pos=(10, 11),
            poi_pos=None
        )
        self.assertEqual(strategy._get_space_penalty(), 10 - 1)

        strategy = SplitingStrategy(
            address='ывааааааdddddddываырпрапрарапрапрараорпопопропрпропоппр',
            index_pos=None,
            country_pos=None,
            region_pos=None,
            subregion_pos=None,
            city_pos=None,
            street_pos=None,
            house_pos=None,
            poi_pos=None
        )
        self.assertEqual(strategy._get_space_penalty(), 0)

        strategy = SplitingStrategy(
            address='ываыааааываырпdddddddрапрарапрапрараорпопопропрпропоппр',
            index_pos=(1, 3),
            country_pos=(5, 10),
            region_pos=(20, 30),
            subregion_pos=None,
            city_pos=None,
            street_pos=(35, 40),
            house_pos=(42, 43),
            poi_pos=None
        )
        self.assertEqual(strategy._get_space_penalty(), 42 - 1)
    def test_init(self):
        strategy = SplitingStrategy(address='0123456789',
                                    index_pos=(0, 10),
                                    country_pos=(0, 10),
                                    region_pos=(0, 10),
                                    subregion_pos=(0, 10),
                                    city_pos=(0, 10),
                                    street_pos=(0, 10),
                                    house_pos=(0, 10),
                                    poi_pos=(0, 10))
        expected_matr = np.ones((8, 10), dtype=np.byte)
        np.testing.assert_array_equal(strategy._score_matrix, expected_matr)

        strategy = SplitingStrategy(address='0123456789',
                                    index_pos=None,
                                    country_pos=None,
                                    region_pos=None,
                                    subregion_pos=None,
                                    city_pos=None,
                                    street_pos=None,
                                    house_pos=None,
                                    poi_pos=None)
        expected_matr = np.zeros((8, 10), dtype=np.byte)
        np.testing.assert_array_equal(strategy._score_matrix, expected_matr)

        strategy = SplitingStrategy(address='0123456789',
                                    index_pos=None,
                                    country_pos=(0, 5),
                                    region_pos=(5, 10),
                                    subregion_pos=None,
                                    city_pos=None,
                                    street_pos=(0, 10),
                                    house_pos=(3, 5),
                                    poi_pos=None)
        expected_matr = np.array(
            [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 1, 1, 1, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
             [0, 0, 0, 1, 1, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],
            dtype=np.byte)
        np.testing.assert_array_equal(strategy._score_matrix, expected_matr)

        # Test for wrong position values
        try:
            strategy = SplitingStrategy(address='0123456789',
                                        index_pos=None,
                                        country_pos=(0, 11),
                                        region_pos=None,
                                        subregion_pos=None,
                                        city_pos=None,
                                        street_pos=None,
                                        house_pos=None,
                                        poi_pos=None)
            raise Exception
        except ValueError:
            pass

        try:
            _ = SplitingStrategy(address='0123456789',
                                 index_pos=None,
                                 country_pos=(-1, 10),
                                 region_pos=None,
                                 subregion_pos=None,
                                 city_pos=None,
                                 street_pos=None,
                                 house_pos=None,
                                 poi_pos=None)
            raise Exception
        except ValueError:
            pass

        try:
            _ = SplitingStrategy(address='0123456789',
                                 index_pos=None,
                                 country_pos=(5, 3),
                                 region_pos=None,
                                 subregion_pos=None,
                                 city_pos=None,
                                 street_pos=None,
                                 house_pos=None,
                                 poi_pos=None)
            raise Exception
        except ValueError:
            pass
    def test_get_score(self):

        strategy = SplitingStrategy(address='0123456789',
                                    index_pos=None,
                                    country_pos=None,
                                    region_pos=None,
                                    subregion_pos=None,
                                    city_pos=None,
                                    street_pos=None,
                                    house_pos=None,
                                    poi_pos=None)
        self.assertEqual(
            strategy.get_score(), 10 * strategy.blank_penalty +
            strategy.absences_penalty['Index'][1] +
            strategy.absences_penalty['Country'][1] +
            strategy.absences_penalty['Region'][1] +
            strategy.absences_penalty['Subregion'][1] +
            strategy.absences_penalty['City'][1] +
            strategy.absences_penalty['Street'][1] +
            strategy.absences_penalty['House'][1] +
            strategy.absences_penalty['Poi'][1])

        strategy = SplitingStrategy(address='0123456789',
                                    index_pos=None,
                                    country_pos=(0, 5),
                                    region_pos=(5, 10),
                                    subregion_pos=None,
                                    city_pos=None,
                                    street_pos=(0, 10),
                                    house_pos=None,
                                    poi_pos=None)
        # expected_matr = np.array(
        #    [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],   (Index)
        #     [1, 1, 1, 1, 1, 0, 0, 0, 0, 0],   (Country)
        #     [0, 0, 0, 0, 0, 1, 1, 1, 1, 1],   (Region)
        #     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],   (Subregion)
        #     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],   (City)
        #     [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],   (Street)
        #     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],   (House)
        #     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],  (Poi))
        # sum: 2  2  2  2  2  2  2  2  2  2
        self.assertEqual(
            strategy.get_score(), 10 * strategy.overlap_penalty +
            strategy.absences_penalty['Index'][1] +
            strategy.absences_penalty['Subregion'][1] +
            strategy.absences_penalty['City'][1] +
            strategy.absences_penalty['House'][1] +
            strategy.absences_penalty['Poi'][1] + strategy.space_ratio * 9)

        strategy = SplitingStrategy(address='0123456789',
                                    index_pos=None,
                                    country_pos=(2, 5),
                                    region_pos=(5, 8),
                                    subregion_pos=None,
                                    city_pos=None,
                                    street_pos=(3, 5),
                                    house_pos=(8, 9),
                                    poi_pos=None)
        # expected_matr = np.array(
        #    [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        #     [0, 0, 1, 1, 1, 0, 0, 0, 0, 0],
        #     [0, 0, 0, 0, 0, 1, 1, 1, 0, 0],
        #     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        #     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        #     [0, 0, 0, 1, 1, 0, 0, 0, 0, 0],
        #     [0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
        #     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        #                                     ], dtype=np.byte)
        # sum: 0  0  1  2  2  1  1  1  1  0
        self.assertEqual(
            strategy.get_score(), 2 * strategy.overlap_penalty +
            strategy.absences_penalty['Index'][1] +
            strategy.absences_penalty['Subregion'][1] +
            strategy.absences_penalty['City'][1] +
            strategy.absences_penalty['Poi'][1] + 3 * strategy.blank_penalty +
            strategy.space_ratio * 6)

        strategy = SplitingStrategy(address='0123456789',
                                    index_pos=None,
                                    country_pos=(2, 3),
                                    region_pos=(5, 8),
                                    subregion_pos=None,
                                    city_pos=None,
                                    street_pos=(9, 10),
                                    house_pos=None,
                                    poi_pos=None)
        self.assertEqual(
            strategy.get_score(), strategy.absences_penalty['Index'][1] +
            strategy.absences_penalty['Subregion'][1] +
            strategy.absences_penalty['City'][1] +
            strategy.absences_penalty['House'][1] +
            strategy.absences_penalty['Poi'][1] + 5 * strategy.blank_penalty +
            strategy.space_ratio * (9 - 2))

        strategy = SplitingStrategy(address='0123456789',
                                    index_pos=(0, 3),
                                    country_pos=(2, 3),
                                    region_pos=(5, 8),
                                    subregion_pos=None,
                                    city_pos=None,
                                    street_pos=(9, 10),
                                    house_pos=(9, 10),
                                    poi_pos=None)
        self.assertEqual(
            strategy.get_score(), 2 * strategy.overlap_penalty +
            strategy.absences_penalty['Subregion'][1] +
            strategy.absences_penalty['City'][1] +
            strategy.absences_penalty['Poi'][1] + 3 * strategy.blank_penalty +
            strategy.space_ratio * 9)
    def test_get_space_penalty(self):

        strategy = SplitingStrategy(
            address='ывааааааываырпрарапрапрараорпопопропрпропоппропdddddddр',
            index_pos=None,
            country_pos=None,
            region_pos=None,
            subregion_pos=(15, 31),
            city_pos=(5, 13),
            street_pos=(50, 55),
            house_pos=None,
            poi_pos=None)
        self.assertEqual(strategy._get_space_penalty(), 54 - 5)

        strategy = SplitingStrategy(
            address='ывdddddddааааааываырпрапрарапрапрараорпопопропрпропоппр',
            index_pos=(1, 2),
            country_pos=(2, 3),
            region_pos=(3, 4),
            subregion_pos=(4, 5),
            city_pos=(5, 6),
            street_pos=(6, 10),
            house_pos=(10, 11),
            poi_pos=None)
        self.assertEqual(strategy._get_space_penalty(), 10 - 1)

        strategy = SplitingStrategy(
            address='ывааааааdddddddываырпрапрарапрапрараорпопопропрпропоппр',
            index_pos=None,
            country_pos=None,
            region_pos=None,
            subregion_pos=None,
            city_pos=None,
            street_pos=None,
            house_pos=None,
            poi_pos=None)
        self.assertEqual(strategy._get_space_penalty(), 0)

        strategy = SplitingStrategy(
            address='ываыааааываырпdddddddрапрарапрапрараорпопопропрпропоппр',
            index_pos=(1, 3),
            country_pos=(5, 10),
            region_pos=(20, 30),
            subregion_pos=None,
            city_pos=None,
            street_pos=(35, 40),
            house_pos=(42, 43),
            poi_pos=None)
        self.assertEqual(strategy._get_space_penalty(), 42 - 1)
    def test_get_score(self):

        strategy = SplitingStrategy(
            address='0123456789',
            index_pos=None,
            country_pos=None,
            region_pos=None,
            subregion_pos=None,
            city_pos=None,
            street_pos=None,
            house_pos=None,
            poi_pos=None
        )
        self.assertEqual(strategy.get_score(),
                         10 * strategy.blank_penalty +
                         strategy.absences_penalty['Index'][1] +
                         strategy.absences_penalty['Country'][1] +
                         strategy.absences_penalty['Region'][1] +
                         strategy.absences_penalty['Subregion'][1] +
                         strategy.absences_penalty['City'][1] +
                         strategy.absences_penalty['Street'][1] +
                         strategy.absences_penalty['House'][1] +
                         strategy.absences_penalty['Poi'][1])

        strategy = SplitingStrategy(
            address='0123456789',
            index_pos=None,
            country_pos=(0, 5),
            region_pos=(5, 10),
            subregion_pos=None,
            city_pos=None,
            street_pos=(0, 10),
            house_pos=None,
            poi_pos=None
        )
        # expected_matr = np.array(
        #    [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],   (Index)
        #     [1, 1, 1, 1, 1, 0, 0, 0, 0, 0],   (Country)
        #     [0, 0, 0, 0, 0, 1, 1, 1, 1, 1],   (Region)
        #     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],   (Subregion)
        #     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],   (City)
        #     [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],   (Street)
        #     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],   (House)
        #     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]],  (Poi))
        # sum: 2  2  2  2  2  2  2  2  2  2
        self.assertEqual(strategy.get_score(),
                         10 * strategy.overlap_penalty +
                         strategy.absences_penalty['Index'][1] +
                         strategy.absences_penalty['Subregion'][1] +
                         strategy.absences_penalty['City'][1] +
                         strategy.absences_penalty['House'][1] +
                         strategy.absences_penalty['Poi'][1] +
                         strategy.space_ratio * 9)

        strategy = SplitingStrategy(
            address='0123456789',
            index_pos=None,
            country_pos=(2, 5),
            region_pos=(5, 8),
            subregion_pos=None,
            city_pos=None,
            street_pos=(3, 5),
            house_pos=(8, 9),
            poi_pos=None
        )
        # expected_matr = np.array(
        #    [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        #     [0, 0, 1, 1, 1, 0, 0, 0, 0, 0],
        #     [0, 0, 0, 0, 0, 1, 1, 1, 0, 0],
        #     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        #     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        #     [0, 0, 0, 1, 1, 0, 0, 0, 0, 0],
        #     [0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
        #     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        #                                     ], dtype=np.byte)
        # sum: 0  0  1  2  2  1  1  1  1  0
        self.assertEqual(strategy.get_score(),
                         2 * strategy.overlap_penalty +
                         strategy.absences_penalty['Index'][1] +
                         strategy.absences_penalty['Subregion'][1] +
                         strategy.absences_penalty['City'][1] +
                         strategy.absences_penalty['Poi'][1] +
                         3 * strategy.blank_penalty +
                         strategy.space_ratio * 6)

        strategy = SplitingStrategy(
            address='0123456789',
            index_pos=None,
            country_pos=(2, 3),
            region_pos=(5, 8),
            subregion_pos=None,
            city_pos=None,
            street_pos=(9, 10),
            house_pos=None,
            poi_pos=None
        )
        self.assertEqual(strategy.get_score(),
                         strategy.absences_penalty['Index'][1] +
                         strategy.absences_penalty['Subregion'][1] +
                         strategy.absences_penalty['City'][1] +
                         strategy.absences_penalty['House'][1] +
                         strategy.absences_penalty['Poi'][1] +
                         5 * strategy.blank_penalty +
                         strategy.space_ratio * (9 - 2))

        strategy = SplitingStrategy(
            address='0123456789',
            index_pos=(0, 3),
            country_pos=(2, 3),
            region_pos=(5, 8),
            subregion_pos=None,
            city_pos=None,
            street_pos=(9, 10),
            house_pos=(9, 10),
            poi_pos=None
        )
        self.assertEqual(strategy.get_score(),
                         2 * strategy.overlap_penalty +
                         strategy.absences_penalty['Subregion'][1] +
                         strategy.absences_penalty['City'][1] +
                         strategy.absences_penalty['Poi'][1] +
                         3 * strategy.blank_penalty +
                         strategy.space_ratio * 9)