/
bull.py
117 lines (87 loc) · 4.5 KB
/
bull.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
# -*- coding: utf-8 -*-
import os
import sys
import traceback
from morphomon.algorithm.mmem_disambig import MMEMAlgorithm
from morphomon.eval import M_strict_mathcher, P_no_garbage, calculate_dir_precision
from morphomon.utils import create_dir, load_object, remove_directory_content, remove_ambiguity_file_list, N_rnc_pos, N_rnc_modified_pos
FOLDS_DIRNAME = 'folds'
TEST_FILENAME = 'test.txt'
RUNPY_FILENAME = 'run'
TEST_DIR = 'test'
TRAIN_DIR = 'train'
TRAIN_FILENAME = 'train.txt'
from maxent import MaxentModel
def bull(action, experiment_name, experiment_path, fold, gold = None, morph_analysis = None):
experiment_dir = os.path.join( experiment_path, experiment_name )
sys.path.append( os.path.abspath(experiment_path) )
mod_run_py = '.'.join( [experiment_name, RUNPY_FILENAME ] )
try:
# Import the module
mod = __import__(mod_run_py, globals(), locals(), ['*'])
except ImportError:
# Log error
print >>sys.stderr, "Failed to run.py module from experiment {0}. {1}".format(experiment_name,traceback.format_exc())
print "Loaded run.py for experiment {0}".format( experiment_name )
params = {
'action' : action,
'experiment_name' : experiment_name,
'experiment_dir' : experiment_dir,
'fold' : fold
}
if action == 'train':
fold_dir = os.path.join(experiment_dir, FOLDS_DIRNAME, fold )
train_files_source_dir = os.path.join(experiment_dir, TRAIN_DIR)
fold_train_file = os.path.join( fold_dir, TRAIN_FILENAME )
params['train_file_list'] = [os.path.abspath(os.path.join(train_files_source_dir,line.strip())) for line in open(fold_train_file, 'r').readlines()]
if morph_analysis:
params['ambiguity_dir'] = morph_analysis
params['action'] = 'train'
mod.runner( **params )
elif action == 'test':
fold_dir = os.path.join(experiment_dir, FOLDS_DIRNAME, fold )
test_files_source_dir = os.path.join(experiment_dir, TEST_DIR)
fold_test_file = os.path.join( fold_dir, TEST_FILENAME )
test_files= [os.path.abspath(os.path.join(test_files_source_dir,line.strip())) for line in open(fold_test_file, 'r').readlines()]
result_dir = os.path.join( fold_dir, 'test_result' )
create_dir( result_dir )
params['action'] = 'load_model'
algo = mod.runner( **params )
remove_directory_content( result_dir )
remove_ambiguity_file_list( ambig_filelist = test_files, output_dir=result_dir, algo=algo)
print "Finished removing ambiguity for experiment {0} for fold {1}".format(experiment_name, fold )
elif action == 'eval':
#результат работы алгоритма в конкретном фолде
eval_funcs = mod.runner( **params )
fold_dir = os.path.join(experiment_dir, 'folds', fold )
errors_filename = os.path.join( fold_dir, 'errors.txt' )
algo_dir = os.path.join( fold_dir, 'test_result' )
return calculate_dir_precision( algo_dir = algo_dir, gold_dir = gold,ambi_dir=morph_analysis, M=eval_funcs['M'],
N=eval_funcs['N'], P = eval_funcs['P'], errors_context_filename=errors_filename)
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-a', '--action', help="test/train/eval")
parser.add_argument('-e', '--experiment_name', help="Name of the experiment")
parser.add_argument('-p', '--experiment_path', help="Path to the experiment")
parser.add_argument('-f', '--fold', help="fold number")
parser.add_argument('-g', '--gold', help="gold directory")
parser.add_argument('-m', '--ambiguity_dir', help="morph analysis output directory")
args = parser.parse_args()
if not args.action:
print >>sys.stderr, "You did not specify action you want to make!"
exit(-1)
if not args.experiment_name:
print >>sys.stderr, "You did not specify experiment name"
exit()
if not args.experiment_path:
print >>sys.stderr, "You did not specify experiment path"
exit()
if (args.action in ['train','test']) and not args.fold:
print >>sys.stderr, "You did not specify fold for {0} action".format( args.action )
exit()
if args.action == 'eval' and not args.gold:
print >>sys.stderr, "You did not specify gold directory"
exit()
bull(action=args.action, experiment_name=args.experiment_name,
experiment_path=args.experiment_path, fold = args.fold, gold = args.gold, morph_analysis = args.ambiguity_dir)