Exemplo n.º 1
0
def normalize(relation: Relation, house_numbers: str, street_name: str,
              normalizers: Dict[str, ranges.Ranges]) -> List[util.HouseNumber]:
    """Strips down string input to bare minimum that can be interpreted as an
    actual number. Think about a/b, a-b, and so on."""
    comment = ""
    if "\t" in house_numbers:
        house_numbers, comment = house_numbers.split("\t")
    if ';' in house_numbers:
        separator = ';'
    elif ',' in house_numbers:
        separator = ','
    else:
        separator = '-'

    # Determine suffix which is not normalized away.
    suffix = ""
    if house_numbers.endswith("*"):
        suffix = house_numbers[-1]

    normalizer = util.get_normalizer(street_name, normalizers)

    ret_numbers, ret_numbers_nofilter = util.split_house_number_by_separator(
        house_numbers, separator, normalizer)

    street_is_even_odd = relation.get_config().get_street_is_even_odd(
        street_name)
    if separator == "-" and util.should_expand_range(ret_numbers_nofilter,
                                                     street_is_even_odd):
        start = ret_numbers_nofilter[0]
        stop = ret_numbers_nofilter[1]
        if stop == 0:
            ret_numbers = [
                number for number in [start] if number in normalizer
            ]
        elif street_is_even_odd:
            # Assume that e.g. 2-6 actually means 2, 4 and 6, not only 2 and 4.
            # Closed interval, even only or odd only case.
            ret_numbers = [
                number for number in range(start, stop + 2, 2)
                if number in normalizer
            ]
        else:
            # Closed interval, but mixed even and odd.
            ret_numbers = [
                number for number in range(start, stop + 1, 1)
                if number in normalizer
            ]

    check_housenumber_letters = len(ret_numbers) == 1 and relation.get_config(
    ).should_check_housenumber_letters()
    if check_housenumber_letters and util.HouseNumber.has_letter_suffix(
            house_numbers, suffix):
        return normalize_housenumber_letters(relation, house_numbers, suffix,
                                             comment)
    return [
        util.HouseNumber(str(number) + suffix, house_numbers, comment)
        for number in ret_numbers
    ]
Exemplo n.º 2
0
def normalize(relation: Relation, house_numbers: str, street_name: str,
              normalizers: Dict[str, ranges.Ranges]) -> List[util.HouseNumber]:
    """Strips down string input to bare minimum that can be interpreted as an
    actual number. Think about a/b, a-b, and so on."""
    ret_numbers = []
    # Same as ret_numbers, but if the range is 2-6 and we filter for 2-4, then 6 would be lost, so
    # in-range 4 would not be detected, so this one does not drop 6.
    ret_numbers_nofilter = []
    if ';' in house_numbers:
        separator = ';'
    else:
        separator = '-'

    # Determine suffix which is not normalized away.
    suffix = ""
    if house_numbers.endswith("*"):
        suffix = house_numbers[-1]

    if street_name in normalizers.keys():
        # Have a custom filter.
        normalizer = normalizers[street_name]
    else:
        # Default sanity checks.
        default = [ranges.Range(1, 999), ranges.Range(2, 998)]
        normalizer = ranges.Ranges(default)

    for house_number in house_numbers.split(separator):
        try:
            number = int(re.sub(r"([0-9]+).*", r"\1", house_number))
        except ValueError:
            continue

        ret_numbers_nofilter.append(number)

        if number not in normalizer:
            continue

        ret_numbers.append(number)

    street_is_even_odd = relation.get_config().get_street_is_even_odd(
        street_name)
    if separator == "-" and util.should_expand_range(ret_numbers_nofilter,
                                                     street_is_even_odd):
        start = ret_numbers_nofilter[0]
        stop = ret_numbers_nofilter[1]
        if stop == 0:
            ret_numbers = [
                number for number in [start] if number in normalizer
            ]
        elif street_is_even_odd:
            # Assume that e.g. 2-6 actually means 2, 4 and 6, not only 2 and 4.
            # Closed interval, even only or odd only case.
            ret_numbers = [
                number for number in range(start, stop + 2, 2)
                if number in normalizer
            ]
        else:
            # Closed interval, but mixed even and odd.
            ret_numbers = [
                number for number in range(start, stop + 1, 1)
                if number in normalizer
            ]

    return [
        util.HouseNumber(str(number) + suffix, house_numbers)
        for number in ret_numbers
    ]