-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtrans_luna_to_pinyin.py
More file actions
52 lines (43 loc) · 1.16 KB
/
trans_luna_to_pinyin.py
File metadata and controls
52 lines (43 loc) · 1.16 KB
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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2020 ubuntu <ubuntu@wmsj100>
#
# Distributed under terms of the MIT license.
"""
转换luna的词库
"""
import sys
import os
import json
def deal_line(line):
cur_line = line.strip()
if line.startswith('#'):
return ''
line_list = cur_line.split()
if len(line_list) < 3:
return ''
pin_yin = "'".join(line_list[1:-1])
return ' '.join([line_list[0], pin_yin, line_list[-1]])
def write_to_file(result, file_path):
data = '\n'.join(result)
with open(file_path + '_out', 'w') as file:
file.write(data)
def init(file_name):
file_path = os.path.abspath(file_name)
if os.path.isfile(file_path):
result = set()
with open(file_path, 'r') as file:
for line in file.readlines():
target_line = deal_line(line)
if target_line:
result.add(target_line)
print(result)
write_to_file(result, file_path)
else:
print("file is not exist")
if __name__ == '__main__':
if len(sys.argv) >1:
file_name=sys.argv[1]
init(file_name)