-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest_file.py
66 lines (56 loc) · 1.71 KB
/
test_file.py
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
#!/usr/bin/env python
# -*- encoding:utf-8 -*-
from __future__ import unicode_literals
from argparse import ArgumentParser
from g2p.g2p import G2PTranscriber
import os
import codecs
def is_valid_file(parser, arg):
if not os.path.exists(arg):
parser.error('the file "%s" does not exist!' % arg)
elif not arg.endswith(".txt"):
parser.error('"%s" is not a text file!' % arg)
else:
return codecs.open(arg, "r", "utf-8") # return an open file handle
if __name__ == "__main__":
# Initialize ArgumentParser class
parser = ArgumentParser()
# Parse command line arguments
parser.add_argument(
"-s",
"--separator",
dest="separator",
required=True,
type=str,
choices=["silva", "ceci"],
help="Select the separator/syllabification algorithm",
)
parser.add_argument(
"-f",
"--file",
dest="file",
required=True,
help="Text file",
type=lambda x: is_valid_file(parser, x),
)
args = parser.parse_args()
# Open output file
f = codecs.open("output.txt", "w", "utf-8")
# Iterate input file
for line in args.file.readlines():
# Get input word
word = line.strip().lower()
# Initialize g2p transcriber
g2p = G2PTranscriber(word, algorithm=args.separator)
# Write file
f.write(
"{0} -> [{1}] | {2} | {3}\r\n".format(
word,
g2p.transcriber(),
g2p.get_syllables_with_hyphen(),
g2p.get_syllables_with_stress_boundaries(),
)
)
# Close output file
f.close()
print('\nSuccess!!! Open the "output.txt" file to see the result.\n')