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

Support back-translation augmentation #1

Merged
merged 9 commits into from
May 27, 2021
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
38 changes: 38 additions & 0 deletions egs/clotho/asr1/local/back_translate_data_dir.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

# Perform back-translation for data dir

. ./path.sh || exit 1

tgt_lang=en
inter_lang=es
edit_distance_threshold=-1

# shellcheck disable=SC1091
. utils/parse_options.sh || exit 1

src_data_dir=$1

if [ $# -ne 1 ]; then
echo "Usage: $0 [options] <data_dir>"
echo "Options:"
echo " --tgt_lang: Target language (default=${tgt_lang})"
echo " --inter_lang: Intermediate language (default=${inter_lang})"
echo " --edit_distance_threshold: Threshold of edit distance (default=${edit_distance_threshold})"
exit 1
fi

set -euo pipefail

utils/copy_data_dir.sh \
--utt-suffix "_${inter_lang}2${tgt_lang}" \
"${src_data_dir}" "${src_data_dir}_${inter_lang}2${tgt_lang}"
local/back_translate_text.py \
--tgt_lang "${tgt_lang}" \
--inter_lang "${inter_lang}" \
--edit_distance_threshold "${edit_distance_threshold}" \
"${src_data_dir}/text" \
"${src_data_dir}_${inter_lang}2${tgt_lang}/text"
utils/fix_data_dir.sh "${src_data_dir}_${inter_lang}2${tgt_lang}"

echo "Sucessfully finished back-tranlation."
57 changes: 57 additions & 0 deletions egs/clotho/asr1/local/back_translate_text.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env python3

"""Back-translate the text."""

import argparse
import logging
import time

import editdistance

from textblob import TextBlob

DELAY_ALPHA = 0.01


def main():
"""Run back-translation."""
parser = argparse.ArgumentParser()
parser.add_argument("--tgt_lang", type=str, default="en")
parser.add_argument("--inter_lang", type=str, default="es")
parser.add_argument("--edit_distance_threshold", type=int, default=-1)
parser.add_argument("--verbose", type=int, default=1)
parser.add_argument("in_text")
parser.add_argument("out_text")
args = parser.parse_args()

if args.verbose > 0:
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s (%(module)s:%(lineno)d) %(levelname)s: %(message)s",
)

with open(args.in_text) as f:
lines = [line.strip() for line in f.readlines()]
text = {line.split()[0]: " ".join(line.split()[1:]) for line in lines}

with open(args.out_text, "w") as f:
for utt_id, txt in text.items():
blob = TextBlob(txt.lower())
# NOTE(kan-bayashi): Sleep to avoid too many request
time.sleep(len(txt) * DELAY_ALPHA)
blob = blob.translate(to=args.inter_lang, from_lang=args.tgt_lang)
# NOTE(kan-bayashi): Sleep to avoid too many request
time.sleep(len(txt) * DELAY_ALPHA)
blob = blob.translate(to=args.tgt_lang, from_lang=args.inter_lang)
new_txt = str(blob).upper()
logging.info(f"original : {txt}")
logging.info(f"translated: {new_txt}")
if args.edit_distance_threshold > 0:
distance = editdistance.eval(txt, new_txt)
if distance > args.edit_distance_threshold:
continue
f.write(f"{utt_id}_{args.inter_lang}2{args.tgt_lang} {new_txt}\n")


if __name__ == "__main__":
main()