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

Glue words (WIP) #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions addok_france/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
extract_address = yielder(utils.extract_address)
glue_ordinal = utils.glue_ordinal
fold_ordinal = yielder(utils.fold_ordinal)
glue_words = utils.glue_words
flag_housenumber = utils.flag_housenumber
make_labels = utils.make_labels
remove_leading_zeros = yielder(utils.remove_leading_zeros)
10 changes: 10 additions & 0 deletions addok_france/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,16 @@ def fold_ordinal(s):
return s


GLUE_WORDS = ["mont", "val", "le", "la", "l", "champ"]

def glue_words(tokens):
""" glue 'MONT GRIFFON' into 'MONTGRIFFON' """
for _, token, next_ in neighborhood(tokens):
yield token
if token in GLUE_WORDS and next_ and next_.isalpha() and len(next_)>2:
yield token.update(token+next_)


def remove_leading_zeros(s):
"""0003 => 3."""
# Limit digits from 1 to 3 in order to avoid processing postcodes.
Expand Down
14 changes: 13 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from addok.helpers.text import Token
from addok_france.utils import (clean_query, extract_address, flag_housenumber,
fold_ordinal, glue_ordinal, make_labels,
remove_leading_zeros)
remove_leading_zeros, glue_words)


@pytest.mark.parametrize("input,expected", [
Expand Down Expand Up @@ -331,3 +331,15 @@ def test_make_municipality_labels(config):
'59000 Lille',
'Lille 59000',
]


@pytest.mark.parametrize("inputs,expected", [
(['mont', 'griffon'], ['mont', 'montgriffon', 'griffon']),
(['champ', 'vallon'], ['champ', 'champvallon', 'vallon']),
(['val', 'suzon'], ['val', 'valsuzon', 'suzon']),
(['l', 'a', 'peu', 'pres'], ['l', 'a', 'peu', 'pres']),
(['l', 'un', 'des'], ['l', 'un', 'des']),
])
def test_glue_ordinal(inputs, expected):
tokens = [Token(input_) for input_ in inputs]
assert list(glue_words(tokens)) == expected