-
Notifications
You must be signed in to change notification settings - Fork 4
Add CLI for text normalization with language and preset options #18
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6e105fe
feat: add CLI for text normalization with language and preset options
Karamouche e032c2a
feat: add support for normalizing text files via CLI
Karamouche ca5947d
refactor: rename CLI command to 'gladia-normalization' for consistency
Karamouche f5288f8
docs: update examples in README to avoid using $
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
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,3 @@ | ||
| from normalization.cli import main | ||
|
|
||
| main() |
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,88 @@ | ||
| import argparse | ||
| import json | ||
| import sys | ||
|
|
||
| from normalization import load_pipeline | ||
| from normalization.languages import get_language_registry | ||
| from normalization.pipeline.loader import _PRESETS_DIR | ||
|
|
||
|
|
||
| def _available_languages() -> list[str]: | ||
| import normalization.languages # noqa: F401 — triggers @register_language decorators | ||
|
|
||
| return sorted(get_language_registry().keys()) | ||
|
|
||
|
|
||
| def _available_presets() -> list[str]: | ||
| return sorted(p.stem for p in _PRESETS_DIR.glob("*.yaml")) | ||
|
|
||
|
|
||
| def main() -> None: | ||
| parser = argparse.ArgumentParser( | ||
| prog="normalize", | ||
| description="Normalize STT transcription text for fair WER comparison.", | ||
| ) | ||
| parser.add_argument( | ||
| "text", | ||
| nargs="?", | ||
| help="Text to normalize. Reads from stdin if omitted.", | ||
| ) | ||
| parser.add_argument( | ||
| "--language", | ||
| "-l", | ||
| default="en", | ||
| metavar="CODE", | ||
| help="Language code (default: en). Available: %(choices)s.", | ||
| choices=_available_languages(), | ||
| ) | ||
|
Karamouche marked this conversation as resolved.
|
||
| parser.add_argument( | ||
| "--preset", | ||
| "-p", | ||
| default="gladia-3", | ||
| metavar="PRESET", | ||
| help="Built-in preset name or path to a YAML file (default: gladia-3).", | ||
| ) | ||
| parser.add_argument( | ||
| "--file", | ||
| "-f", | ||
| metavar="PATH", | ||
| help="Path to a text file to normalize. Mutually exclusive with positional text.", | ||
| ) | ||
| parser.add_argument( | ||
| "--describe", | ||
| action="store_true", | ||
| help="Print the pipeline description as JSON and exit.", | ||
| ) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| if args.text is not None and args.file is not None: | ||
| parser.error("Provide either positional text or --file, not both.") | ||
|
|
||
| try: | ||
| pipeline = load_pipeline(args.preset, args.language) | ||
| except FileNotFoundError as exc: | ||
| parser.error(str(exc)) | ||
|
|
||
|
Karamouche marked this conversation as resolved.
|
||
| if args.describe: | ||
| print(json.dumps(pipeline.describe(), indent=2)) | ||
| return | ||
|
|
||
| if args.file is not None: | ||
| try: | ||
| with open(args.file) as fh: | ||
| text = fh.read().strip() | ||
| except OSError as exc: | ||
| parser.error(str(exc)) | ||
| elif args.text is not None: | ||
| text = args.text | ||
| elif not sys.stdin.isatty(): | ||
| text = sys.stdin.read().strip() | ||
| else: | ||
| parser.error("Provide text as an argument, --file, or pipe it via stdin.") | ||
|
|
||
| print(pipeline.normalize(text)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.