-
Notifications
You must be signed in to change notification settings - Fork 3
feat: improve french text normalization with number conversion and contraction expansion #13
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
Open
egenthon-cmd
wants to merge
9
commits into
main
Choose a base branch
from
feat/french-normalizer
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b5f2c90
fix: integrating metrics-api process
egenthon-cmd 919ebb5
refactor(french): separate sentence replacements into a new module an…
Karamouche 8db75ab
feat(french): enhance contraction expansion logic and add unit tests
Karamouche 79bb7de
feat: added pourcentage fr
egenthon-cmd 6735b41
test: added french csv tests
egenthon-cmd 0ee638a
test(french): correct spacing in French contractions in CSV test data
Karamouche 6ae7563
Merge branch 'main' into feat/french-normalizer
Karamouche 8453b56
fix(french): update vowel list in FrenchOperators to include 'h' for …
Karamouche ffb1cfe
chore: updated uv.lock
Karamouche File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| """French number normalizer using text2num's alpha2digit. | ||
|
|
||
| Converts spelled-out numbers to digits (e.g. vingt trois → 23) and handles | ||
| mixed digit+word forms (e.g. 3 milliards → trois milliards) before conversion | ||
| so alpha2digit does not misinterpret them. | ||
| """ | ||
|
|
||
| import re | ||
|
|
||
| try: | ||
| from text_to_num import alpha2digit | ||
| except ImportError: | ||
| alpha2digit = None | ||
|
|
||
|
|
||
| # Digit-to-French word mapping for normalizing "3 milliards" → "trois milliards". | ||
| _DIGIT_TO_FRENCH: dict[str, str] = { | ||
| "0": "zéro", | ||
| "1": "un", | ||
| "2": "deux", | ||
| "3": "trois", | ||
| "4": "quatre", | ||
| "5": "cinq", | ||
| "6": "six", | ||
| "7": "sept", | ||
| "8": "huit", | ||
| "9": "neuf", | ||
| } | ||
|
|
||
| # Pattern: digit(s) followed by millions/milliards (French) or billions/trillions. | ||
| _RE_MIXED_NUMBER = re.compile( | ||
| r"\b(\d+)\s+(millions?|milliards?|billions?|trillions?)\b", | ||
| re.IGNORECASE, | ||
| ) | ||
|
|
||
|
|
||
| def _normalize_mixed_numbers(text: str) -> str: | ||
| """Convert '3 milliards' → 'trois milliards' so alpha2digit yields 3e9, not 31e9. | ||
|
|
||
| alpha2digit may concatenate a lone digit with the following word; converting | ||
| the digit to a word avoids that (e.g. 'trois milliards' → 3000000000). | ||
| """ | ||
|
|
||
| def replace(match: re.Match) -> str: | ||
| number = match.group(1) | ||
| multiplier = match.group(2) | ||
| if len(number) == 1 and number in _DIGIT_TO_FRENCH: | ||
| return f"{_DIGIT_TO_FRENCH[number]} {multiplier}" | ||
| # Multi-digit: keep as-is; alpha2digit will handle or leave unchanged | ||
| return match.group(0) | ||
|
|
||
| return _RE_MIXED_NUMBER.sub(replace, text) | ||
|
|
||
|
|
||
| class FrenchNumberNormalizer: | ||
| """Convert French spelled-out numbers to digits via text2num.alpha2digit. | ||
|
|
||
| Applies a pre-pass to normalize mixed digit+word forms (e.g. 3 milliards) | ||
| before calling alpha2digit. | ||
| """ | ||
|
|
||
| def __init__(self) -> None: | ||
| if alpha2digit is None: | ||
| raise ImportError( | ||
| "French number normalization requires the text2num package. " | ||
| "Install it with: uv add text2num" | ||
| ) | ||
| self._alpha2digit = alpha2digit | ||
|
|
||
| def __call__(self, text: str) -> str: | ||
| text = _normalize_mixed_numbers(text) | ||
| return self._alpha2digit(text, "fr") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,28 @@ | ||
| FRENCH_REPLACEMENTS: dict[str, str] = {} | ||
| FRENCH_REPLACEMENTS = { | ||
| # contractions in titles/prefixes | ||
| "mme": "madame", | ||
| "mlle": "mademoiselle", | ||
| "mr": "monsieur", | ||
| "st": "saint", | ||
| "dr": "docteur", | ||
| "prof": "professeur", | ||
| "pr": "professeur", | ||
| # sports | ||
| "volley-ball": "volleyball", | ||
| "basket-ball": "basketball", | ||
| "water-polo": "waterpolo", | ||
| "ping-pong": "pingpong", | ||
| "hand-ball": "handball", | ||
| # Tech / quotidien | ||
| "wi-fi": "wifi", | ||
| "cd-rom": "cdrom", | ||
| "t-shirt": "tshirt", | ||
| "chat-bot": "chatbot", | ||
| "blogue": "blog", | ||
| "e-mail": "email", | ||
| "week-end": "weekend", | ||
| "week-ends": "weekends", | ||
| "porte-monnaie": "portemonnaie", | ||
| "porte-feuille": "portefeuille", | ||
| "extra-terrestre": "extraterrestre", | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| FRENCH_SENTENCE_REPLACEMENTS: dict[str, str] = { | ||
| "super predateur": "superprédateur", | ||
| "ping pong": "pingpong", | ||
| "hand ball": "handball", | ||
| "water polo": "waterpolo", | ||
| "basket ball": "basketball", | ||
| "volley ball": "volleyball", | ||
| "wi fi": "wifi", | ||
| "cd rom": "cdrom", | ||
| "t shirt": "tshirt", | ||
| "pour 100": "pourcent", | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
tests/unit/steps/text/apply_sentence_level_replacements_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| from normalization.languages.french import FrenchOperators | ||
| from normalization.steps.text.apply_sentence_level_replacements import ( | ||
| ApplySentenceLevelReplacementsStep, | ||
| ) | ||
|
|
||
| from .conftest import assert_text_step_registered | ||
|
|
||
|
|
||
| def test_step_is_registered(): | ||
| assert_text_step_registered(ApplySentenceLevelReplacementsStep) | ||
|
|
||
|
|
||
| def test_apply_sentence_level_replacements_step_french_pour_100( | ||
| french_operators: FrenchOperators, | ||
| ): | ||
| text = "pour 100 de réduction" | ||
| formatted_text = ApplySentenceLevelReplacementsStep()(text, french_operators) | ||
| assert formatted_text == "pourcent de réduction" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| from normalization.languages.english import EnglishOperators | ||
| from normalization.languages.french import FrenchOperators | ||
| from normalization.steps.text.expand_contractions import ExpandContractionsStep | ||
|
|
||
| from .conftest import assert_text_step_registered | ||
|
|
||
|
|
||
| def test_step_is_registered(): | ||
| assert_text_step_registered(ExpandContractionsStep) | ||
|
|
||
|
|
||
| def test_expand_contractions_step_english(english_operators: EnglishOperators): | ||
| text = "he ain't gonna" | ||
| formatted_text = ExpandContractionsStep()(text, english_operators) | ||
| assert formatted_text == "he is not going to" | ||
|
|
||
|
|
||
| def test_expand_contractions_step_french(french_operators: FrenchOperators): | ||
| text = "j'ai dit c'est bien" | ||
| formatted_text = ExpandContractionsStep()(text, french_operators) | ||
| assert formatted_text == "j'ai dit c'est bien" |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
number_wordsis missing common standard French forms.This list is used for number-word detection, but ordinary spellings like
dix-sept,dix-huit,dix-neuf,soixante-dix,quatre-vingt,quatre-vingts, andquatre-vingt-dixare absent while rarer regional variants are present. Those phrases will be skipped by config-driven steps.💡 At minimum, add the standard hyphenated forms
number_words=[ "quinze", "seize", + "dix-sept", + "dix-huit", + "dix-neuf", "vingt", "trente", "quarante", "cinquante", "soixante", + "soixante-dix", "septante", "octante", "huitante", "nonante", + "quatre-vingt", + "quatre-vingts", + "quatre-vingt-dix", "cent",🤖 Prompt for AI Agents