Download demo_core/features.py from simon-clmtd/morphological-inflection: direct link, hf CLI and curl.
- Browser
- Download file 831 Bytes
-
https://huggingface.co/spaces/simon-clmtd/morphological-inflection/resolve/main/demo_core/features.py
- Command line
-
hf download hf://spaces/simon-clmtd/morphological-inflection/demo_core/features.py
-
curl -L -o features.py https://huggingface.co/spaces/simon-clmtd/morphological-inflection/resolve/main/demo_core/features.py
831 Bytes
| """ | |
| Feature parsing, validation, and OOV checking against vocabulary. | |
| """ | |
| from typing import List, Set, Tuple | |
| from trans import vocabulary | |
| def parse_feature_string(features_str: str) -> List[str]: | |
| """Splits a semicolon-delimited UniMorph tag string into ordered feature tokens.""" | |
| return [f.strip() for f in features_str.split(";") if f.strip()] | |
| def validate_features(features_str: str, vocab: vocabulary.FeatureVocabularies) -> Tuple[List[str], List[str]]: | |
| """ | |
| Validates features against the bundle's vocabulary. | |
| Returns: | |
| (known_tags, unknown_tags) | |
| """ | |
| tags = parse_feature_string(features_str) | |
| known = [] | |
| unknown = [] | |
| for tag in tags: | |
| if tag in vocab.features.w2i: | |
| known.append(tag) | |
| else: | |
| unknown.append(tag) | |
| return known, unknown | |