Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Support "pk" in housenumber #12

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions addok_france/utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import re

TYPES = [
'av(enue)?', 'r(ue)?', 'b(oulevar)?d', 'all[ée]es?', 'impasse', 'place',
'chemin', 'rocade', 'route', 'l[ôo]tissement', 'mont[ée]e', 'c[ôo]te',
r'av(enue)?', r'r(ue)?', r'b(oulevar)?d', r'all[ée]es?', 'impasse', 'place',
'chemin', 'rocade', 'route', 'l[ôo]tissement', 'mont[ée]e', r'c[ôo]te',
'clos', 'champ', 'bois', 'taillis', 'boucle', 'passage', 'domaine',
'étang', 'etang', 'quai', 'desserte', 'pré', 'porte', 'square', 'mont',
'r[ée]sidence', 'parc', 'cours?', 'promenade', 'hameau', 'faubourg',
'ilot', 'berges?', 'via', 'cit[ée]', 'sent(e|ier)', 'rond[- ][Pp]oint',
'pas(se)?', 'carrefour', 'traverse', 'giratoire', 'esplanade', 'voie',
'chauss[ée]e',
'ilot', r'berges?', 'via', r'cit[ée]', r'sent(e|ier)', r'rond[- ][Pp]oint',
r'pas(se)?', 'carrefour', 'traverse', 'giratoire', 'esplanade', 'voie',
'chauss[ée]e', r'd\d+'
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"d" pour "départementale": il faut peut-être ajouter le mot entier, et peut-être gérer l'espace optionnel:

d(épartementale|\d+)?

et dans tous les cas ajouter les autres types de routes (nationale, communale…).

]
TYPES_REGEX = '|'.join(
map(lambda x: '[{}{}]{}'.format(x[0], x[0].upper(), x[1:]), TYPES)
Expand All @@ -27,7 +27,7 @@
# example "22 rue des Fleurs 59350 Lille" will be extracted from
# "XYZ Ets bâtiment B 22 rue des Fleurs 59350 Lille Cedex 23").
EXTRACT_ADDRESS_PATTERN = re.compile(
r'(\b\d{1,4}( *(' + ORDINAL_REGEX + '))?,? +(' + TYPES_REGEX + ') .*(\d{5})?).*', # noqa
r'((pk )?\b\d{1,4}( *(' + ORDINAL_REGEX + '))?,? +(' + TYPES_REGEX + ') .*(\d{5})?).*', # noqa
flags=re.IGNORECASE)

# Match "bis", "ter", "b", etc.
Expand All @@ -46,7 +46,7 @@

# Match number once cleaned by glue_ordinal and fold_ordinal (for example
# "6b", "234t"…)
NUMBER_PATTERN = re.compile(r'\b\d{1,4}[a-z]?\b', flags=re.IGNORECASE)
NUMBER_PATTERN = re.compile(r'\b(pk)?\d{1,4}[a-z]?\b', flags=re.IGNORECASE)


def clean_query(q):
Expand Down Expand Up @@ -84,22 +84,27 @@ def neighborhood(iterable, first=None, last=None):


def glue_ordinal(tokens):
previous = None
for _, token, next_ in neighborhood(tokens):
if next_ and token.isdigit():
previous = token
candidate = None
for previous, token, next_ in neighborhood(tokens):
if not candidate and next_ and (token.isdigit() or (token == "pk" and next_.isdigit())):
candidate = token
continue
if previous is not None:
# Yield a token without knowing next token and next x 2.
if candidate is not None:
if candidate == "pk":
raw = 'pk {}'.format(token)
# Space removed to maximize chances to get a hit.
token = token.update(raw.replace(' ', ''), raw=raw)
# Matches "bis" either followed by a type or nothing.
if (ORDINAL_PATTERN.match(token) and
elif (ORDINAL_PATTERN.match(token) and
(not next_ or TYPES_PATTERN.match(next_))):
raw = '{} {}'.format(previous, token)
raw = '{} {}'.format(candidate, token)
# Space removed to maximize chances to get a hit.
token = previous.update(raw.replace(' ', ''), raw=raw)
token = candidate.update(raw.replace(' ', ''), raw=raw)
else:
# False positive.
yield previous
previous = None
yield candidate
candidate = None
yield token


Expand All @@ -124,6 +129,8 @@ def fold_ordinal(s):
else:
s = s.update('{}{}'.format(number,
FOLD.get(ordinal.lower(), ordinal)))
elif s.startswith("pk"):
s = s.update(s.raw.replace(" ", ""), raw=s.raw)
return s


Expand Down
3 changes: 3 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ def test_extract_address(input, expected):
(['241', 'r', 'rue'], ['241r', 'rue']),
(['place', 'des', 'terreaux'], ['place', 'des', 'terreaux']),
(['rue', 'du', 'bis'], ['rue', 'du', 'bis']),
(['pk', '25'], ['pk25']),
(['pk', '25', 'd39'], ['pk25', 'd39']),
])
def test_glue_ordinal(inputs, expected):
tokens = [Token(input_) for input_ in inputs]
Expand All @@ -151,6 +153,7 @@ def test_glue_ordinal(inputs, expected):
(['place', 'des', 'terreaux'], False),
(['rue', 'du', 'bis'], False),
(['9', 'grand', 'rue'], True),
(['pk25', 'd39'], True),
])
def test_flag_housenumber(inputs, expected):
tokens = [Token(input_) for input_ in inputs]
Expand Down