|
| 1 | +# Copyright (c) 2022 Xingchen Song (sxc19@tsinghua.org.cn) |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import argparse |
| 16 | + |
| 17 | +# TODO(xcsong): multi-language support |
| 18 | +from itn.chinese.inverse_normalizer import InverseNormalizer |
| 19 | + |
| 20 | +def str2bool(s, default=False): |
| 21 | + s = s.lower() |
| 22 | + if s == 'true': |
| 23 | + return True |
| 24 | + elif s == 'false': |
| 25 | + return False |
| 26 | + else: |
| 27 | + return default |
| 28 | + |
| 29 | +def main(): |
| 30 | + parser = argparse.ArgumentParser() |
| 31 | + parser.add_argument('--text', help='input string') |
| 32 | + parser.add_argument('--file', help='input file path') |
| 33 | + parser.add_argument('--overwrite_cache', action='store_true', |
| 34 | + help='rebuild *.fst') |
| 35 | + parser.add_argument('--enable_standalone_number', type=str, |
| 36 | + default='True', |
| 37 | + help='enable standalone number') |
| 38 | + parser.add_argument('--enable_0_to_9', type=str, |
| 39 | + default='True', |
| 40 | + help='enable convert number 0 to 9') |
| 41 | + args = parser.parse_args() |
| 42 | + |
| 43 | + normalizer = InverseNormalizer( |
| 44 | + cache_dir='itn', overwrite_cache=args.overwrite_cache, |
| 45 | + enable_standalone_number=str2bool(args.enable_standalone_number), |
| 46 | + enable_0_to_9=str2bool(args.enable_0_to_9)) |
| 47 | + |
| 48 | + if args.text: |
| 49 | + print(normalizer.tag(args.text)) |
| 50 | + print(normalizer.normalize(args.text)) |
| 51 | + elif args.file: |
| 52 | + with open(args.file) as fin: |
| 53 | + for line in fin: |
| 54 | + print(normalizer.tag(line.strip())) |
| 55 | + print(normalizer.normalize(line.strip())) |
| 56 | + |
| 57 | + |
| 58 | +if __name__ == '__main__': |
| 59 | + main() |
0 commit comments