forked from MycroftAI/lingua-franca
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat/utterance_plugin companion to OpenVoiceOS/ovos-core#163 * rm comment * rm try except
- Loading branch information
Showing
2 changed files
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,29 @@ | ||
from typing import Optional, List | ||
|
||
from ovos_plugin_manager.templates.transformers import UtteranceTransformer | ||
from ovos_utils.log import LOG | ||
|
||
from lingua_franca.parse import normalize | ||
|
||
|
||
class UtteranceNormalizer(UtteranceTransformer): | ||
|
||
def __init__(self, name="ovos-lf-utterance-normalizer", priority=5): | ||
super().__init__(name, priority) | ||
|
||
@staticmethod | ||
def strip_punctuation(utterance: str): | ||
return utterance.rstrip('.').rstrip('?').rstrip('!').rstrip(',').rstrip(';') | ||
|
||
def transform(self, utterances: List[str], | ||
context: Optional[dict] = None) -> (list, dict): | ||
context = context or {} | ||
norm = [self.strip_punctuation(u) for u in utterances] + utterances | ||
try: | ||
lang = context.get("lang") or self.config.get("lang", "en-us") | ||
norm += [normalize(u, lang=lang, remove_articles=False) for u in norm] + \ | ||
[normalize(u, lang=lang, remove_articles=True) for u in norm] | ||
except: | ||
LOG.exception("lingua_franca utterance normalization failed") | ||
|
||
return list(set(norm)), context |
This file contains 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