Skip to content
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

update asr eval #8045

Merged
merged 1 commit into from
Dec 18, 2023
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
36 changes: 31 additions & 5 deletions nemo/collections/asr/parts/utils/eval_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,23 @@
# limitations under the License.

import json
from typing import Tuple
from typing import Optional, Tuple, Union

from nemo.collections.asr.metrics.wer import word_error_rate_detail
from nemo.utils import logging
from nemo.utils.nemo_logging import LogMode


def remove_punctuations(text: str, punctuations: Optional[Union[list, str]] = None) -> str:
"""
Remove punctuations from a string
"""
if not punctuations:
punctuations = [char for char in '!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~']

for punctuation in punctuations:
text = text.replace(punctuation, '')
return text


def clean_label(_str: str, num_to_words: bool = True, langid="en") -> str:
Expand All @@ -38,8 +51,9 @@ def clean_label(_str: str, num_to_words: bool = True, langid="en") -> str:
if langid == "en":
_str = convert_num_to_words(_str, langid="en")
else:
logging.info(
"Currently support basic num_to_words in English only. Please use Text Normalization to convert other languages! Skipping!"
logging.warning(
"Currently support basic num_to_words in English only. Please use Text Normalization to convert other languages! Skipping!",
mode=LogMode.ONCE,
)

ret = " ".join(_str.split())
Expand Down Expand Up @@ -75,8 +89,9 @@ def convert_num_to_words(_str: str, langid: str = "en") -> str:
out_str += word + " "
out_str = out_str.strip()
else:
raise ValueError(
"Currently support basic num_to_words in English only. Please use Text Normalization to convert other languages!"
logging.warning(
"Currently support basic num_to_words in English only. Please use Text Normalization to convert other languages!",
mode=LogMode.ONCE,
)
return out_str

Expand All @@ -88,6 +103,9 @@ def cal_write_wer(
langid: str = 'en',
use_cer: bool = False,
output_filename: str = None,
ignore_capitalization: bool = False,
ignore_punctuation: bool = False,
punctuations: Optional[list] = None,
) -> Tuple[str, dict, str]:
"""
Calculate wer, inserion, deletion and substitution rate based on groundtruth text and pred_text_attr_name (pred_text)
Expand All @@ -114,6 +132,14 @@ def cal_write_wer(
if clean_groundtruth_text:
ref = clean_label(ref, langid=langid)

if ignore_punctuation:
ref = remove_punctuations(ref, punctuations=punctuations)
hyp = remove_punctuations(hyp, punctuations=punctuations)

if ignore_capitalization:
ref = ref.lower()
hyp = hyp.lower()

wer, tokens, ins_rate, del_rate, sub_rate = word_error_rate_detail(
hypotheses=[hyp], references=[ref], use_cer=use_cer
)
Expand Down
3 changes: 3 additions & 0 deletions tools/asr_evaluator/asr_evaluator.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ def main(cfg):
langid=cfg.analyst.metric_calculator.langid,
use_cer=cfg.analyst.metric_calculator.use_cer,
output_filename=cfg.analyst.metric_calculator.output_filename,
ignore_capitalization=cfg.analyst.metric_calculator.get("ignore_capitalization", False),
ignore_punctuation=cfg.analyst.metric_calculator.get("ignore_punctuation", False),
punctuations=cfg.analyst.metric_calculator.get("punctuations", None),
)
with open_dict(cfg):
cfg.analyst.metric_calculator.output_filename = output_manifest_w_wer
Expand Down
3 changes: 3 additions & 0 deletions tools/asr_evaluator/conf/eval.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ analyst:
langid: "en" # speciify language to clean text. Note use text normalization in NeMo for better performancce
output_filename: null
use_cer: False
ignore_capitalization: False
ignore_punctuation: False
punctuations: null # a string of punctuations to remove when ignore_punctuation=True. if not set, default to '!"#$%&()*+,-./:;<=>?@[\\]^_`{|}~'

metadata:
duration:
Expand Down
Loading