forked from miurahr/pykakasi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kakasi
executable file
·98 lines (87 loc) · 2.46 KB
/
kakasi
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import getopt
import pykakasi
_VERSION = '0.23'
def show_version():
print (os.path.basename(sys.argv[0]) + " v" + _VERSION)
def usage():
print ("Python implementation of kakasi\n")
def do_wakati(mode, infile, outfile):
kakasi = pykakasi.wakati()
do_it(kakasi, mode, infile, outfile)
def do_kakasi(mode, infile, outfile):
kakasi = pykakasi.kakasi()
do_it(kakasi, mode, infile, outfile)
def do_it(kakasi, mode, infile, outfile):
try:
for k,v in mode.iteritems():
kakasi.setMode(k,v)
except:
for k,v in mode.items():
kakasi.setMode(k,v)
converter = kakasi.getConverter()
if infile:
infp = open(infile,'r')
else:
infp = sys.stdin
if outfile:
outfp = open(outfile,'w')
else:
outfp = sys.stdout
for line in infp:
outfp.write(converter.do(line))
def main():
try:
opts, args = getopt.getopt(sys.argv[1:],
"hO:I:vwH:K:J:r:sCE:S:",
["help","input=","output=","separator=","space","wakati","version"])
except getopt.GetoptError as err:
print (err)
usage()
sys.exit(2)
outfile = None
infile = None
wakati = False
mode = {
'H':None, 'K':None, 'J':None, 'r':"Hepburn", 'E':None,
's':False, 'C':False
}
values = ["a","E","H","K",None]
romanvals = {"h":"Hepburn", "k":"Kunrei", "p":"Passport"}
for o, a in opts:
if o in ["-v","--version"]:
show_version()
sys.exit()
elif o in ("-h", "--help"):
show_version()
usage()
sys.exit()
elif o in ("-I", "--input"):
infile = a
elif o in ("-O", "--output"):
outfile = a
elif o in ("-w", "--wakati"):
wakati = True
elif o in ["-H","-K","-J","-E"]:
if a in values:
mode[o[-1:]] = a
elif o in ["-s","-C"]:
mode[o[-1:]] = True
elif o == "-r":
if a in romanvals.keys():
mode['r'] = romanvals[a]
elif o == "--space":
mode["s"] = True
elif o in ["-S","--separator"]:
mode["S"] = a
else:
assert False, "unhandled option"
if wakati:
do_wakati(infile, outfile)
else:
do_kakasi(mode, infile, outfile)
if __name__ == "__main__":
sys.exit(main())