-
Notifications
You must be signed in to change notification settings - Fork 177
Ar TN/TN #454
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
ealbasiri
wants to merge
13
commits into
NVIDIA:staging/ar_tn_v2
Choose a base branch
from
ealbasiri:ar-tn-date
base: staging/ar_tn_v2
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.
+2,056
−233
Open
Ar TN/TN #454
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d717f37
add measure coverage for TN and ITN
ealbasiri 25e485f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f4c378d
Merge branch 'NVIDIA:main' into main
ealbasiri 4717919
Remove unused imports
anand-nv f684fca
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] fe435fb
Remove unused imports
anand-nv 4f6f38c
Remove unused imports
anand-nv 59afc44
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] d7007ae
Update measure.py
anand-nv bc46f66
feat(ar): expand cardinal to millions, fix orthography, agreement, an…
ealbasiri 65395ba
feat(ar): add time normalization and inverse normalization (TN + ITN)
ealbasiri 8192b0d
feat(ar): add date normalization and inverse normalization (TN + ITN)…
ealbasiri 9563b57
chore(ar): add __init__.py to time/date data folders (time, ordinal, …
ealbasiri 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
Some comments aren't visible on the classic Files Changed page.
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
113 changes: 113 additions & 0 deletions
113
nemo_text_processing/inverse_text_normalization/ar/taggers/date.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,113 @@ | ||
| # Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import pynini | ||
| from nemo_text_processing.text_normalization.ar.graph_utils import ( | ||
| NEMO_DIGIT, | ||
| NEMO_NOT_QUOTE, | ||
| NEMO_SIGMA, | ||
| GraphFst, | ||
| convert_space, | ||
| ) | ||
| from nemo_text_processing.text_normalization.ar.utils import get_abs_path, load_labels | ||
| from pynini.lib import pynutil | ||
|
|
||
|
|
||
| class DateFst(GraphFst): | ||
| """ | ||
| Finite state transducer for classifying spoken Arabic date into a digit form, | ||
| by inverting the text-normalization date tagger and verbalizer, e.g. | ||
| الأول من شهر نوفمبر لعام ألفين وعشرة -> tokens { name: "01/11/2010" } | ||
| الأول من شهر نوفمبر -> tokens { name: "1 نوفمبر" } | ||
| الأول من شهر ربيع الثاني لعام ... هجري -> tokens { name: "01/04/1445 هـ" } | ||
|
|
||
| Args: | ||
| itn_cardinal_tagger: ITN cardinal tagger | ||
| tn_date_tagger: TN date tagger | ||
| tn_date_verbalizer: TN date verbalizer | ||
| deterministic: if True will provide a single transduction option, | ||
| for False multiple transduction are generated (used for audio-based normalization) | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| itn_cardinal_tagger: GraphFst, | ||
| tn_date_tagger: GraphFst, | ||
| tn_date_verbalizer: GraphFst, | ||
| deterministic: bool = True, | ||
| ): | ||
| super().__init__(name="date", kind="classify", deterministic=deterministic) | ||
|
|
||
| add_leading_zero_to_double_digit = (NEMO_DIGIT + NEMO_DIGIT) | (pynutil.insert("0") + NEMO_DIGIT) | ||
| optional_delete_space = pynini.closure(NEMO_SIGMA | pynutil.delete(" ", weight=0.0001)) | ||
| ordinals = pynini.string_file(get_abs_path("data/ordinal/ordinals_date.tsv")).invert().optimize() | ||
| hijri_suffixes = pynini.string_file(get_abs_path("data/months/hijri_suffixes_inverse.tsv")).optimize() | ||
| months_names = [x[0] for x in load_labels(get_abs_path("data/months/months_name.tsv"))] | ||
| months_names = pynini.union(*months_names) | ||
| month_to_number = tn_date_tagger.number_to_month.invert().optimize() | ||
| month_to_number_hijri = tn_date_tagger.month_hijri.invert().optimize() | ||
| tagger = tn_date_verbalizer.graph.invert().optimize() | ||
|
|
||
| delete_day_marker = ( | ||
| pynutil.delete("day: \"") + pynini.closure(NEMO_NOT_QUOTE, 1) + pynutil.delete("\"") | ||
| ) @ ordinals | ||
|
|
||
| month_as_number = pynutil.delete("month: \"") + itn_cardinal_tagger.graph + pynutil.delete("\"") | ||
| month_as_string = pynutil.delete("month: \"") + months_names + pynutil.delete("\"") | ||
| month_name_to_number = ( | ||
| pynutil.delete("month: \"") + (month_to_number | month_to_number_hijri) + pynutil.delete("\"") | ||
| ) | ||
|
|
||
| convert_year = (tn_date_tagger.year @ optional_delete_space).invert().optimize() | ||
| convert_year |= ( | ||
| (tn_date_tagger.year @ optional_delete_space).invert().optimize() + pynini.accep(" ") + hijri_suffixes | ||
| ) | ||
| delete_year_marker = ( | ||
| pynutil.delete("year: \"") + pynini.closure(NEMO_NOT_QUOTE, 1) + pynutil.delete("\"") | ||
| ) @ convert_year | ||
|
|
||
| # day month as string (year) | ||
| verbalizer = pynutil.add_weight( | ||
| ( | ||
| pynini.closure(delete_day_marker + pynini.accep(" "), 0, 1) | ||
| + month_as_string | ||
| + pynini.closure(pynini.accep(" ") + delete_year_marker, 0, 1) | ||
| ), | ||
| weight=0.0001, | ||
| ) | ||
|
|
||
| # day month as number (year); trailing "/" only when a year is present | ||
| verbalizer |= ( | ||
| delete_day_marker @ add_leading_zero_to_double_digit | ||
| + pynutil.insert("/") | ||
| + pynutil.delete(" ") | ||
| + month_as_number @ add_leading_zero_to_double_digit | ||
| + pynini.closure(pynutil.insert("/") + pynutil.delete(" ") + delete_year_marker, 0, 1) | ||
| ) | ||
| # 02/03/2022 هـ | ||
| verbalizer |= ( | ||
| delete_day_marker @ add_leading_zero_to_double_digit | ||
| + pynutil.insert("/") | ||
| + pynutil.delete(" ") | ||
| + month_name_to_number @ add_leading_zero_to_double_digit | ||
| + pynini.closure(pynutil.insert("/") + pynutil.delete(" ") + delete_year_marker, 0, 1) | ||
| ) | ||
|
|
||
| # year | ||
| verbalizer |= delete_year_marker | ||
|
|
||
| final_graph = tagger @ verbalizer | ||
|
|
||
| graph = pynutil.insert("name: \"") + convert_space(final_graph) + pynutil.insert("\"") | ||
| self.fst = graph.optimize() | ||
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
83 changes: 83 additions & 0 deletions
83
nemo_text_processing/inverse_text_normalization/ar/taggers/measure.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,83 @@ | ||
| # Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import pynini | ||
| from nemo_text_processing.text_normalization.ar.graph_utils import GraphFst, convert_space, delete_extra_space | ||
| from nemo_text_processing.text_normalization.ar.taggers.measure import unit_singular | ||
| from pynini.lib import pynutil | ||
|
|
||
|
|
||
| class MeasureFst(GraphFst): | ||
| """ | ||
| Finite state transducer for classifying measure. Allows for plural form for unit. | ||
| e.g. "عشرون في المائة" -> measure { cardinal { integer: "20" } units: "%" } | ||
|
|
||
| Args: | ||
| itn_cardinal_tagger: ITN Cardinal tagger | ||
| itn_decimal_tagger: ITN Decimal tagger | ||
| itn_fraction_tagger: ITN Fraction tagger | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| itn_cardinal_tagger: GraphFst, | ||
| itn_decimal_tagger: GraphFst, | ||
| itn_fraction_tagger: GraphFst, | ||
| deterministic: bool = True, | ||
| ): | ||
| super().__init__(name="measure", kind="classify", deterministic=deterministic) | ||
|
|
||
| cardinal_graph = itn_cardinal_tagger.graph | ||
|
|
||
| graph_unit_singular = pynini.invert(unit_singular) | ||
| unit = convert_space(graph_unit_singular) | ||
|
|
||
| optional_graph_negative = pynini.closure( | ||
| pynutil.insert("negative: ") + pynini.cross("سالب", "\"true\"") + delete_extra_space, 0, 1 | ||
| ) | ||
|
|
||
| unit = pynutil.insert("units: \"") + (unit) + pynutil.insert("\"") | ||
|
|
||
| subgraph_decimal = ( | ||
| pynutil.insert("decimal { ") | ||
| + optional_graph_negative | ||
| + itn_decimal_tagger.final_graph_wo_negative | ||
| + pynutil.insert(" }") | ||
| + delete_extra_space | ||
| + unit | ||
| ) | ||
|
|
||
| subgraph_fraction = ( | ||
| pynutil.insert("decimal { ") | ||
| + optional_graph_negative | ||
| + pynutil.insert("integer_part: \"") | ||
| + itn_fraction_tagger.graph | ||
| + pynutil.insert("\" }") | ||
| + delete_extra_space | ||
| + unit | ||
| ) | ||
|
|
||
| subgraph_cardinal = ( | ||
| pynutil.insert("cardinal { ") | ||
| + optional_graph_negative | ||
| + pynutil.insert("integer: \"") | ||
| + cardinal_graph | ||
| + pynutil.insert("\"") | ||
| + pynutil.insert(" }") | ||
| + delete_extra_space | ||
| + unit | ||
| ) | ||
| final_graph = subgraph_cardinal | subgraph_decimal | subgraph_fraction | ||
| final_graph = self.add_tokens(final_graph) | ||
| self.fst = final_graph.optimize() |
41 changes: 41 additions & 0 deletions
41
nemo_text_processing/inverse_text_normalization/ar/taggers/time.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,41 @@ | ||
| # Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
|
|
||
| import pynini | ||
| from nemo_text_processing.text_normalization.ar.graph_utils import NEMO_SIGMA, GraphFst | ||
| from pynini.lib import pynutil | ||
|
|
||
|
|
||
| class TimeFst(GraphFst): | ||
| """ | ||
| Finite state transducer for classifying spoken Arabic time by inverting the | ||
| text-normalization time verbalizer, e.g. | ||
| الثالثة وخمس عشرة دقيقة -> time { hours: "3" minutes: "15" } | ||
| الخامسة وعشر دقائق وثانيتان -> time { hours: "5" minutes: "10" seconds: "2" } | ||
| التاسعة صباحًا -> time { hours: "9" suffix: "صباحًا" } | ||
|
|
||
| Args: | ||
| tn_time_verbalizer: TN time verbalizer, whose .graph maps the tagged fields | ||
| (hours/minutes/seconds/suffix/zone) to their spoken Arabic form. | ||
| deterministic: if True will provide a single transduction option, | ||
| for False multiple transduction are generated (used for audio-based normalization) | ||
| """ | ||
|
|
||
| def __init__(self, tn_time_verbalizer: GraphFst, deterministic: bool = True): | ||
| super().__init__(name="time", kind="classify", deterministic=deterministic) | ||
| # allow the spoken form to be matched with flexible spacing | ||
| optional_delete_space = pynini.closure(NEMO_SIGMA | pynutil.delete(" ", weight=0.0001)) | ||
| graph = (tn_time_verbalizer.graph @ optional_delete_space).invert().optimize() | ||
| self.fst = self.add_tokens(graph).optimize() |
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
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.
Copyright (c) 2026 for any new file