-
Notifications
You must be signed in to change notification settings - Fork 5
/
generate.py
executable file
·161 lines (136 loc) · 5.71 KB
/
generate.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#!/usr/bin/python3
import os
import os.path
import sys
from subprocess import Popen, PIPE
from argparse import ArgumentParser
import utils
from myconfig import MyConfig
config = MyConfig()
#change cwd to the libpinyin utils/training directory
libpinyin_dir = config.getToolsDir()
libpinyin_sub_dir = os.path.join(libpinyin_dir, 'utils', 'training')
os.chdir(libpinyin_sub_dir)
#chdir done
def handleError(error):
sys.exit(error)
#Note: all file passed here should be trained.
def generateOneText(infile, modelfile, reportfile):
infilestatuspath = infile + config.getStatusPostfix()
infilestatus = utils.load_status(infilestatuspath)
if not utils.check_epoch(infilestatus, 'Segment'):
raise utils.EpochError('Please segment first.\n')
if utils.check_epoch(infilestatus, 'Generate'):
return
#begin processing
cmdline = ['./gen_k_mixture_model', '--maximum-occurs-allowed', \
str(config.getMaximumOccursAllowed()), \
'--maximum-increase-rates-allowed', \
str(config.getMaximumIncreaseRatesAllowed()), \
'--k-mixture-model-file', \
modelfile, infile + \
config.getSegmentPostfix()]
subprocess = Popen(cmdline, shell=False, stderr=PIPE, \
close_fds=True)
lines = subprocess.stderr.readlines()
if lines:
print('found error report')
with open(reportfile, 'ab') as f:
f.writelines(lines)
f.close()
os.waitpid(subprocess.pid, 0)
#end processing
utils.sign_epoch(infilestatus, 'Generate')
utils.store_status(infilestatuspath, infilestatus)
#Note: should check the corpus file size, and skip the too small text file.
def handleOneIndex(indexpath, subdir, indexname):
print(indexpath, subdir, indexname)
indexstatuspath = indexpath + config.getStatusPostfix()
indexstatus = utils.load_status(indexstatuspath)
if not utils.check_epoch(indexstatus, 'Segment'):
raise utils.EpochError('Please segment first.\n')
if utils.check_epoch(indexstatus, 'Generate'):
return
#continue generating
textnum, modelnum, aggmodelsize = 0, 0, 0
if 'GenerateTextEnd' in indexstatus:
textnum = indexstatus['GenerateTextEnd']
if 'GenerateModelEnd' in indexstatus:
modelnum = indexstatus['GenerateModelEnd']
#clean up previous file
modeldir = os.path.join(config.getModelDir(), subdir, indexname)
modelfile = os.path.join( \
modeldir, config.getCandidateModelName(modelnum))
reportfile = modelfile + config.getReportPostfix()
if os.access(modelfile, os.F_OK):
os.unlink(modelfile)
if os.access(reportfile, os.F_OK):
os.unlink(reportfile)
#begin processing
indexfile = open(indexpath, 'r')
for i, oneline in enumerate(indexfile.readlines()):
#continue last generating
if i < textnum:
continue
#remove trailing '\n'
oneline = oneline.rstrip(os.linesep)
(title, textpath) = oneline.split('#')
infile = config.getTextDir() + textpath
infilesize = utils.get_file_length(infile + config.getSegmentPostfix())
if infilesize < config.getMinimumFileSize():
print("Skipping " + title + '#' + textpath)
continue
aggmodelsize += infilesize
modeldir = os.path.join(config.getModelDir(), subdir, indexname)
os.makedirs(modeldir, exist_ok=True)
modelfile = os.path.join(modeldir, \
config.getCandidateModelName(modelnum))
reportfile = modelfile + config.getReportPostfix()
print("Proccessing " + title + '#' + textpath)
generateOneText(infile, modelfile, reportfile)
print("Processed " + title + '#' + textpath)
if aggmodelsize > config.getCandidateModelSize():
nexttextnum = i + 1
#store model info in status file
modelstatuspath = modelfile + config.getStatusPostfix()
#create None status
modelstatus = {}
modelstatus['GenerateStart'] = textnum
modelstatus['GenerateEnd'] = nexttextnum
utils.sign_epoch(modelstatus, 'Generate')
utils.store_status(modelstatuspath, modelstatus)
#new model candidate
aggmodelsize = 0
textnum = nexttextnum
modelnum += 1
#save current progress in status file
indexstatus['GenerateTextEnd'] = nexttextnum
indexstatus['GenerateModelEnd'] = modelnum
utils.store_status(indexstatuspath, indexstatus)
pass
indexfile.close()
#end processing
utils.sign_epoch(indexstatus, 'Generate')
utils.store_status(indexstatuspath, indexstatus)
def walkThroughIndex(path):
for root, dirs, files in os.walk(path, topdown=True, onerror=handleError):
for onefile in files:
filepath = os.path.join(root, onefile)
indexpostfix = config.getIndexPostfix()
if onefile.endswith(indexpostfix):
subdir = os.path.relpath(root, path)
indexname = onefile[:-len(indexpostfix)]
handleOneIndex(filepath, subdir, indexname)
elif onefile.endswith(config.getStatusPostfix()):
pass
else:
print('Unexpected file:' + filepath)
if __name__ == '__main__':
parser = ArgumentParser(description='Generate model candidates.')
parser.add_argument('--indexdir', action='store', \
help='index directory', \
default=os.path.join(config.getTextDir(), 'index'))
args = parser.parse_args()
print(args)
walkThroughIndex(args.indexdir)
print('done')