8000 [fix] fix normalize and clean transforms config management by francoishernandez · Pull Request #87 · eole-nlp/eole · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[fix] fix normalize and clean transforms config management #87

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

Merged
merged 2 commits into from
Aug 23, 2024
Merged
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
19 changes: 19 additions & 0 deletions eole/config/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,29 @@ class Dataset(Config):
)
path_align: str | None = None
# optional stuff for some transforms
# TODO: define a better mechanism to support such settings
src_prefix: str | None = None
tgt_prefix: str | None = None
src_suffix: str | None = None
tgt_suffix: str | None = None
# normalize
src_lang: str | None = None
tgt_lang: str | None = None
penn: bool | None = True
norm_quote_commas: bool | None = True
norm_numbers: bool | None = True
pre_replace_unicode_punct: bool | None = False
post_remove_control_chars: bool | None = False
# clean
src_eq_tgt: bool | None = True
same_char: bool | None = True
same_word: bool | None = True
scripts_ok: List[str] | None = ["Latin", "Common"]
scripts_nok: List[str] | None = []
src_tgt_ratio: float | None = 2
avg_tok_min: float | None = 3
avg_tok_max: float | None = 20
lang_id: List[str] | None = ["en", "fr"]


# add all opts from all transforms (like in eole.opts._add_transform_opt)
Expand Down
34 changes: 23 additions & 11 deletions eole/transforms/clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class CleanConfig(TransformConfig):
)
scripts_ok: List[str] | None = Field(
default=["Latin", "Common"],
description="list of unicodata scripts accepted",
)
scripts_nok: List[str] | None = Field(
default=[],
description="list of unicodata scripts not accepted",
)
src_tgt_ratio: float | None = Field(
Expand Down Expand Up @@ -63,8 +67,8 @@ def _parse_config(self):
@staticmethod
def _get_param(corpus, param, def_val):
"""Get param string of a `corpus`."""
if "clean" in corpus["transforms"]:
value = corpus.get(param, def_val)
if "clean" in getattr(corpus, "transforms", []):
value = getattr(corpus, param, def_val)
clean = value
else:
clean = None
Expand All @@ -88,17 +92,25 @@ def warm_up(self, vocabs=None):
super().warm_up(None)
import fasttext

self.src_eq_tgt_dict = self.get_config_dict(self.config, "src_eq_tgt", True)
self.same_char_dict = self.get_config_dict(self.config, "same_char", True)
self.same_word_dict = self.get_config_dict(self.config, "same_word", True)
self.src_eq_tgt_dict = self.get_config_dict(
self.full_config, "src_eq_tgt", True
)
self.same_char_dict = self.get_config_dict(self.full_config, "same_char", True)
self.same_word_dict = self.get_config_dict(self.full_config, "same_word", True)
self.scripts_ok_dict = self.get_config_dict(
self.config, "scripts_ok", ["Latin", "Common"]
self.full_config, "scripts_ok", ["Latin", "Common"]
)
self.scripts_nok_dict = self.get_config_dict(
self.full_config, "scripts_nok", []
)
self.src_tgt_ratio_dict = self.get_config_dict(
self.full_config, "src_tgt_ratio", 2
)
self.avg_tok_min_dict = self.get_config_dict(self.full_config, "avg_tok_min", 3)
self.avg_tok_max_dict = self.get_config_dict(
self.full_config, "avg_tok_max", 20
)
self.scripts_nok_dict = self.get_config_dict(self.config, "scripts_nok", [])
self.src_tgt_ratio_dict = self.get_config_dict(self.config, "src_tgt_ratio", 2)
self.avg_tok_min_dict = self.get_config_dict(self.config, "avg_tok_min", 3)
self.avg_tok_max_dict = self.get_config_dict(self.config, "avg_tok_max", 20)
self.langid_dict = self.get_config_dict(self.config, "langid", [])
self.langid_dict = self.get_config_dict(self.full_config, "langid", [])
fasttext_loc = f"{os.path.dirname(os.path.abspath(__file__))}/lid.176.ftz"

if not os.path.exists(fasttext_loc):
Expand Down
20 changes: 11 additions & 9 deletions eole/transforms/normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,8 @@ def _parse_config(self):
@staticmethod
def _get_param(corpus, param, def_val):
"""Get opt string of a `corpus`."""
if "normalize" in corpus["transforms"]:
value = corpus.get(param, def_val)
if "normalize" in getattr(corpus, "transforms", []):
value = getattr(corpus, param, def_val)
normalize = value
else:
normalize = None
Expand All @@ -271,18 +271,20 @@ def get_config_dict(cls, config, param, def_val):
def warm_up(self, vocabs=None):
"""Set options for each dataset."""
super().warm_up(None)
self.src_lang_dict = self.get_config_dict(self.config, "src_lang", "")
self.tgt_lang_dict = self.get_config_dict(self.config, "tgt_lang", "")
self.penn_dict = self.get_config_dict(self.config, "penn", True)
self.src_lang_dict = self.get_config_dict(self.full_config, "src_lang", "")
self.tgt_lang_dict = self.get_config_dict(self.full_config, "tgt_lang", "")
self.penn_dict = self.get_config_dict(self.full_config, "penn", True)
self.norm_quote_commas_dict = self.get_config_dict(
self.config, "norm_quote_commas", True
self.full_config, "norm_quote_commas", True
)
self.norm_numbers_dict = self.get_config_dict(
self.full_config, "norm_numbers", True
)
self.norm_numbers_dict = self.get_config_dict(self.config, "norm_numbers", True)
self.pre_dict = self.get_config_dict(
self.config, "pre_replace_unicode_punct", False
self.full_config, "pre_replace_unicode_punct", False
)
self.post_dict = self.get_config_dict(
self.config, "post_remove_control_chars", False
self.full_config, "post_remove_control_chars", False
)
self.src_lang_dict["infer"] = self.src_lang
self.tgt_lang_dict["infer"] = self.tgt_lang
Expand Down
Loading
0