示例#1
0
    def __init__(self):
        """Creates suffix tree in O(n) for n number of string for constant-sized alphabets, O(1) for insertion in
        hashmap """
        self.tree = st.SuffixTree(True, [])
        self.children = defaultdict(list)

        with open(self.DATA_FILEPATH, 'r', encoding='utf8') as file:
            column_names = clean_input_line(next(file))
            raw_inputs = [dict(zip(column_names, clean_input_line(line))) for line in file]

        for child in raw_inputs:
            self.tree.addStrings([normalize_input(child[self.COLUMN_ID])])
            self.children[normalize_input(child[self.COLUMN_ID])].append(child)
        self.tree = self.tree.createQueryTree()
        self.tree.cacheNodes()
示例#2
0
    def suggestions():
        q = request.args.get(Q_PARAMETER) if request.args.get(
            Q_PARAMETER) else ""

        try:
            latitude = float(
                request.args.get(LATITUDE_PARAMETER)) if request.args.get(
                    LATITUDE_PARAMETER) else None
            longitude = float(
                request.args.get(LONGITUDE_PARAMETER)) if request.args.get(
                    LONGITUDE_PARAMETER) else None
            city_query = CityQuery(suffix_tree)

        except ValueError:
            return abort(HTTPStatus.BAD_REQUEST)

        response = Response(json.dumps(JSONSerializer.serialize(
            city_query.get(normalize_input(q), latitude, longitude)),
                                       ensure_ascii=False,
                                       sort_keys=False,
                                       indent=2),
                            mimetype='application/json',
                            content_type="application/json; charset=utf-8")
        response.status_code = HTTPStatus.OK
        return response
示例#3
0
def test_levenshtein_ratio():
    expected_ratio = ratio(normalize_input(TORONTO.name), KEY)
    actual_ratio = levenshtein._ratio(TORONTO.name, KEY)

    assert expected_ratio == actual_ratio
示例#4
0
def _ratio(word1: str, word2: str):
    """Returns levenshtein distance between 2 normalized string parameters"""
    return ratio(normalize_input(word1), word2)
示例#5
0
def test_get_cities_response_with_invalid_longitude_returns_type_error(client):
    with pytest.raises(TypeError) as e:
        client.get(normalize_input("Toronto"), 43.70011, "-79.4163")
    assert e.type is TypeError
示例#6
0
def test_get_cities_response_with_empty_query_returns_type_error(client):
    response = client.get(normalize_input(""), 43.70011, -79.4163)
    assert len(response["suggestions"]) == 0
示例#7
0
def test_get_cities_response_with_valid_parameters(client):
    response = client.get(normalize_input("Toronto"), 43.70011, -79.4163)
    cities = response["suggestions"]
    assert any(city.name == "Toronto, CA" for city in cities)
    assert any(city.latitude == "43.70011" for city in cities)
    assert any(city.longitude == "-79.4163" for city in cities)