-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.py
320 lines (251 loc) · 9.44 KB
/
config.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# -*- coding: utf-8 -*-
import sys
sys.path.append('pymodules')
import color
#### ----------------------------------- must-do ----------------------------------- ####
mongo_addr = 'doraemon.iis.sinica.edu.tw'
# mongo_addr = 'wolverine.iis.sinica.edu.tw'
db_name = 'LJ40K'
# db_name = 'NTCIR'
# category = 'polarity' ## target to be categorized. e.g., emotion, polarity
category = 'emotion' ## target to be categorized. e.g., emotion, polarity
corpus_root = '/corpus/NTCIR'
#### ----------------------------------- must-do ----------------------------------- ####
#### define program names
ds_name = 'document_scoring'
# ps_name = 'pattern_scoring'
ev_name = 'evaluation'
genSVM_name = 'toSVM'
runSVM_name = 'run_svm'
# -------------------------------------------- paths -------------------------------------------- #
# libsvm abs path
libsvm_path = '/tools/libsvm'
libsvm_program = {
'train':'svm-train',
'test': 'svm-predict',
'check': 'tools/checkdata.py',
}
# relative file pathes for train, test, model and out
svm_file_root = 'tmp'
# -------------------------------------------- mongodb -------------------------------------------- #
## mongo collection setting
keywordFeat_name = 'keyword_feature'
keywordEmotionFeat_name = 'keyword_emotion_feature'
keywordPositionFeat_name = 'keyword_position_feature'
keywordEmotionPositionFeat_name = 'keyword_emotion_position_feature'
patternFeat_name = 'pattern_feature'
patternEmotionFeat_name = 'pattern_emotion_feature'
patternPositionFeat_name = 'pattern_position_feature'
patternEmotionPositionFeat_name = 'pattern_emotion_position_feature'
## mongo collection name
co_emotions_name = 'emotions'
co_category_name = category
co_docs_name = 'docs'
co_sents_name = 'sents'
co_deps_name = 'deps'
co_pats_name = 'pats'
co_lexicon_name = 'lexicon.nested'
co_results_name = 'NewRes'
co_patsearch_name = 'pats_trim'
co_feature_setting_name = 'features.settings'
## total count
co_lexicon_pattern_tc_name = 'lexicon.pattern_total_count'
co_lexicon_keyword_tc_name = 'lexicon.keyword_total_count'
co_svm_out_name = 'svm.out' # (svm) .out.txt
co_svm_gold_name = 'svm.gold' # (svm) .gold
co_svm_eval_name = 'svm.eval' # (svm) evalutation results
## default
co_patscore_prefix = 'patscore'
co_docscore_prefix = 'docscore'
## to be setup
# co_patscore_name = co_patscore_prefix
# co_docscore_name = ''
# ------------------------------------------ (end) mongodb ------------------------------------------ #
## names of functions
ps_function_name = 'ps_function'
ds_function_name = 'ds_function'
sig_function_name = 'sig_function'
smoothing_name = 'smoothing'
limit_name = 'limit'
## default values
ps_function_type = 0
ds_function_type = 0
sig_function_type = 0
smoothing_type = 0
## minimum occurrence of a pattern
min_count = 0
###################################
### document feature extraction ###
begPercentage = 20
midPercentage = 60
endPercentage = 20
countingUnitType = 0
featureValueType = ''
minCount = 4
cutoffPercentage = 100
###################################
### document feature extraction ###
keyword_type = 'basic'
lemma = False
###################################
overwrite = False
verbose = False
debug = False
topk = 1
delta_d = 356.10659375
### latest version: support automatically insert addon option names
### i.e, config.py doesn't care about neither the addon opts nor the description
### e.g., in runSVM_name, addon is a list of length 3, which looks like [ ('--setting', [...]), ('--list', [...]), ('--param', [...]) ]
### and the program will incorporate missing opts and yield: ['-v', '-o', '--setting', '--list', '--param']
opt_fields = {
# ps_name: ['-p','-s','-v', '-o'],
ds_name: ['-p','-d','-s','-g','-l','-v', '-o'],
ev_name: ['-p','-d','-s','-g','-l','-v', '-o'],
keywordFeat_name: ['-k','--lemma','-v'],
keywordEmotionFeat_name: ['-k','--lemma','-v'],
keywordPositionFeat_name: ['-b','-m','-e','-k','--lemma','-v'],
keywordEmotionPositionFeat_name: ['-b','-m','-e','-k','--lemma','-v'],
patternFeat_name: ['-l','-v'],
patternEmotionFeat_name: ['-f','-v'],
patternPositionFeat_name: ['-b','-m','-e','-l','-v'],
patternEmotionPositionFeat_name: ['-b','-m','-e','-f','-v'],
genSVM_name:['-v', '-o'],
runSVM_name:['-v', '-o'],
'default': ['-v', '-o']
}
_abbr = {
'p': 'ps_function',
'd': 'ds_function',
'g': 'sig_function',
's': 'smoothing',
'l': 'limit'
}
## fields="p,d,g,s,l" or fields="-p,-d,-g,-s,-l" or fields=['-p','-s','-v']
def getOpts(fields="-p,-d,-g,-s,-l", key_value='', full=False):
if type(fields) == str:
fields_to_transform = [x.strip().replace('-','') for x in fields.split(',')]
else:
fields_to_transform = [x.strip().replace('-','') for x in fields]
cfgShort, cfgFull = {}, {}
if 'p' in fields_to_transform:
cfgFull[ _abbr['p'] ] = ps_function_type
cfgShort['p'] = ps_function_type
if 'd' in fields_to_transform:
cfgFull[ _abbr['d'] ] = ds_function_type
cfgShort['d'] = ds_function_type
if 'g' in fields_to_transform:
cfgFull[ _abbr['g'] ] = sig_function_type
cfgShort['g'] = sig_function_type
if 's' in fields_to_transform:
cfgFull[ _abbr['s'] ] = smoothing_type
cfgShort['s'] = smoothing_type
if 'l' in fields_to_transform:
cfgFull[ _abbr['l'] ] = min_count
cfgShort['l'] = min_count
cfg = cfgShort if not full else cfgFull
return [str(x)+key_value+str(cfg[x]) for x in sorted(cfg.keys())]
color_for = {
bool:
{
False: 'r',
True: 'g'
},
str:
{
},
list:
{
},
dict:
{
}
}
def print_confirm(confirm_msg, bar=40, halt=True):
for msg in confirm_msg:
msg = list(msg)
if len(msg) > 1:
for i in range(len(msg)-1):
if type(msg[i+1]) == bool:
if msg[i+1] == False:
msg[i+1] = color.render(str(msg[i+1]), color_for[bool][False])
else:
msg[i+1] = color.render(str(msg[i+1]),color_for[bool][True])
if len(msg) == 3 and type(msg[2]) == dict:
print >> sys.stderr, msg[0], ':', msg[1], msg[2][msg[1]]
elif len(msg) == 3 and type(msg[2]) == str:
print >> sys.stderr, msg[0], ':', msg[1], msg[2]
elif len(msg) == 2:
print >> sys.stderr, msg[0], ':', msg[1]
else:
print >> sys.stderr, msg
print >> sys.stderr, '='*bar
if halt:
print >> sys.stderr, 'press any key to start...', raw_input()
def help(program, args=[], addon=[], exit=1):
params = {}
record = ['p','d','g','s','l'] # record the option of ps_function, ds_function, sig_function, smoothing and limit
params['-p'] = [
'-p, --ps_function: pattern scoring function',
' 0: (default) no distribution information, only consider occurrence portion',
' 1: combine occurrence with distribution information using standard deviation scaling [2014.03.18]',
' 2: average occurrence with weight [2014.04.09]']
params['-d'] = [
'-d, --ds_function: document scoring function',
' 0: (default) arithmetic mean',
' 1: geometric mean']
params['-g'] = [
'-g, --sig_function: significance function',
' 0: (default) sf = 1, i.e., remain origin pattern score',
' 1: sf = ( pattern length )',
' 2: sf = ( 1/sentence length )',
' 3: sf = ( pattern length/sentence length )']
params['-s'] = [
'-s, --smoothing: smoothing method',
' 0: (default) no smoothig',
' 1: awesome smoothing (+0.25)']
params['-l'] = [
'-l, --limit: minimum occurrence of a pattern',
' 0: (default) collect all patterns',
' n: at least occurs < n > times for each pattern']
params['-o'] = [
'-o, --overwrite: overwrite the destination file (or mongo database)']
params['-v'] = [
'-v, --verbose: show debug message']
#########################################################################################################
## document feature extraction
params['-b'] = [
'-b, --begPercentage: percentage of beginning section']
params['-m'] = [
'-m, --midPercentage: percentage of middle section']
params['-e'] = [
'-e, --endPercentage: percentage of ending section']
params['-c'] = [
'-c, --countingUnitType: counting unit for document segmentation',
' 0: number of words',
' 1: number of sentences (not implemented yet)']
# params['-f'] = [
# '-f, --featureValueType: feature value computation',
# ' 0: pattern scores (patscore_p2_s0)',
# ' 1: accumulated threshold by 0.68 (1 standard diviation) using pattern scores',
# ' 2: accumulated threshold by 0.68 (1 standard diviation) using pattern occurrence',
# ' 3: same as type 2 but ignore those with total occurrence < 4 (1, 2, 3)']
#########################################################################################################
opts = opt_fields['default'] if program not in opt_fields else opt_fields[program]
## add all self-defined option description
# addon_opt: --setting
# addon_description: ['--setting: specify a setting ID (e.g., 537086fcd4388c7e81676914)', ... ]
for addon_opt, addon_description in addon:
params[ addon_opt ] = addon_description
if addon_opt not in opts:
opts.append( addon_opt )
usage = ['\nusage:']
usage += [program+'.py']
usage += args
usage += ['[-, -- options]\n']
# usage += []
print ' '.join(usage) + '='*50 + '\n[-, -- options]'
# usage = '\nusage: python '+program+' '.join(args)+' [options]\n'
params_str = '\n'.join(['\n'.join(params[opt]) + '\n' for opt in opts])
# print >> sys.stderr, usage
print >> sys.stderr, params_str
if exit: sys.exit(exit)