File size: 3,389 Bytes
d596074
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright    2022  Xiaomi Corp.        (authors: Mingshuang Luo)
#
# See ../../../../LICENSE for clarification regarding multiple authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


"""
This script takes as input "text_full", which includes three transcript files
(train_S, train_M and train_L) for AISHELL4:
    - text_full
and generates the output file text_normalize which is implemented
to normalize text:
    - text
"""


import argparse

from tqdm import tqdm


def get_parser():
    parser = argparse.ArgumentParser(
        description="Normalizing for text",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    )
    parser.add_argument(
        "--input",
        default="data/lang_char/text_full",
        type=str,
        help="the input text files for AISHELL4",
    )
    parser.add_argument(
        "--output",
        default="data/lang_char/text",
        type=str,
        help="the text implemented with normalizer for AISHELL4",
    )

    return parser


def text_normalize(str_line: str):
    line = str_line.strip().rstrip("\n")
    line = line.replace(" ", "")
    line = line.replace("<sil>", "")
    line = line.replace("<%>", "")
    line = line.replace("<->", "")
    line = line.replace("<$>", "")
    line = line.replace("<#>", "")
    line = line.replace("<_>", "")
    line = line.replace("<space>", "")
    line = line.replace("`", "")
    line = line.replace("&", "")
    line = line.replace(",", "")
    line = line.replace("A", "")
    line = line.replace("a", "A")
    line = line.replace("b", "B")
    line = line.replace("c", "C")
    line = line.replace("k", "K")
    line = line.replace("t", "T")
    line = line.replace(",", "")
    line = line.replace("丶", "")
    line = line.replace("。", "")
    line = line.replace("、", "")
    line = line.replace("?", "")
    line = line.replace("·", "")
    line = line.replace("*", "")
    line = line.replace("!", "")
    line = line.replace("$", "")
    line = line.replace("+", "")
    line = line.replace("-", "")
    line = line.replace("\\", "")
    line = line.replace("?", "")
    line = line.replace("¥", "")
    line = line.replace("%", "")
    line = line.replace(".", "")
    line = line.replace("<", "")
    line = line.replace("&", "")
    line = line.upper()

    return line


def main():
    parser = get_parser()
    args = parser.parse_args()

    input_file = args.input
    output_file = args.output

    f = open(input_file, "r", encoding="utf-8")
    lines = f.readlines()
    new_lines = []
    for i in tqdm(range(len(lines))):
        new_line = text_normalize(lines[i])
        new_lines.append(new_line)

    f_new = open(output_file, "w", encoding="utf-8")
    for line in new_lines:
        f_new.write(line)
        f_new.write("\n")


if __name__ == "__main__":
    main()