-
Notifications
You must be signed in to change notification settings - Fork 18
/
__init__.py
142 lines (117 loc) · 5.03 KB
/
__init__.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
import sys
import yaml
import os
import imp
import logging
from src import annotation_dispatcher, annotation_worker
from src import generation_dispatcher, generation_worker
from src import classification_dispatcher, classification_worker
__name__ = "bayzee"
def __loadConfig(configFilePath):
config = None
if not os.path.exists(configFilePath):
print "Config file does not exist"
sys.exit(1)
try:
dataStream = open(configFilePath, "r")
config = yaml.load(dataStream)
except:
error = sys.exc_info()
print "Failed to load configuration from file", error
sys.exit(1)
else:
return config
def __loadProcessors(configFilePath, config):
processorInstances = []
for module in config["processor"]["modules"]:
modulePath = os.path.abspath(os.path.join(os.path.dirname(configFilePath), module["path"]))
processorInstances.append(imp.load_source(module["name"], modulePath))
config["processor_instances"] = processorInstances
def __initLogger(configFilePath, config):
logsDir = os.path.abspath(os.path.join(os.path.dirname(configFilePath), config["logger"]["logsDir"]))
if not os.path.exists(logsDir):
os.makedirs(logsDir)
logger = logging.getLogger("bayzee")
fh = logging.FileHandler(logsDir + "/bayzee.log")
ch = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger.setLevel(logging.DEBUG)
fh.setLevel(logging.DEBUG)
ch.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
ch.setFormatter(formatter)
logger.addHandler(fh)
logger.addHandler(ch)
config["logger"] = logger
logger = logging.getLogger("elasticsearch")
fh = logging.FileHandler(logsDir + "/elasticsearch.log")
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger.setLevel(logging.DEBUG)
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
logger.addHandler(fh)
logger = logging.getLogger("elasticsearch.trace")
fh = logging.FileHandler(logsDir + "/elasticsearch.trace")
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger.setLevel(logging.DEBUG)
fh.setLevel(logging.DEBUG)
fh.setFormatter(formatter)
logger.addHandler(fh)
def dispatchToAnnotate(configFilePath, processingStartIndex, processingEndIndex):
config = __loadConfig(configFilePath)
__loadProcessors(configFilePath, config)
__initLogger(configFilePath, config)
ann = annotation_dispatcher.AnnotationDispatcher(config, processingStartIndex, processingEndIndex)
ann.dispatchToAnnotate()
def annotate(configFilePath):
config = __loadConfig(configFilePath)
__loadProcessors(configFilePath, config)
__initLogger(configFilePath, config)
ann = annotation_worker.AnnotationWorker(config)
ann.annotate()
def dispatchToGenerate(configFilePath, processingStartIndex, processingEndIndex):
config = __loadConfig(configFilePath)
__loadProcessors(configFilePath, config)
__initLogger(configFilePath, config)
trainingFilePath = os.path.abspath(os.path.join(os.path.dirname(configFilePath), config["generator"]["trainingPhrasesFilePath"]))
holdOutFilePath = os.path.abspath(os.path.join(os.path.dirname(configFilePath), config["generator"]["holdOutPhrasesFilePath"]))
trainingFile = open(trainingFilePath, "r")
holdOutFile = open(holdOutFilePath, "r")
trainingDataset = {}
for row in trainingFile.readlines()[1:]:
values = row.split(",")
trainingDataset[values[0]] = values[1]
holdOutDataset = {}
for row in holdOutFile.readlines()[1:]:
values = row.split(",")
holdOutDataset[values[0]] = values[1]
gen = generation_dispatcher.GenerationDispatcher(config, trainingDataset, holdOutDataset, processingStartIndex, processingEndIndex)
gen.dispatchToGenerate()
def generate(configFilePath):
config = __loadConfig(configFilePath)
__loadProcessors(configFilePath, config)
__initLogger(configFilePath, config)
trainingFilePath = os.path.abspath(os.path.join(os.path.dirname(configFilePath), config["generator"]["trainingPhrasesFilePath"]))
holdOutFilePath = os.path.abspath(os.path.join(os.path.dirname(configFilePath), config["generator"]["holdOutPhrasesFilePath"]))
trainingFile = open(trainingFilePath, "r")
holdOutFile = open(holdOutFilePath, "r")
trainingDataset = {}
for row in trainingFile.readlines()[1:]:
values = row.split(",")
trainingDataset[values[0]] = values[1]
holdOutDataset = {}
for row in holdOutFile.readlines()[1:]:
values = row.split(",")
holdOutDataset[values[0]] = values[1]
gen = generation_worker.GenerationWorker(config, trainingDataset, holdOutDataset)
gen.generate()
def dispatchToClassify(configFilePath, processingStartIndex, processingEndIndex):
config = __loadConfig(configFilePath)
__initLogger(configFilePath, config)
cls = classification_dispatcher.ClassificationDispatcher(config, processingStartIndex, processingEndIndex)
cls.dispatchToClassify()
def classify(configFilePath):
config = __loadConfig(configFilePath)
__initLogger(configFilePath, config)
cls = classification_worker.ClassificationWorker(config)
cls.classify()