def __init__(self, model=None, debug=False, features=False,
candidates=False, error_handling=True):
'''
Initialize the disambiguation model and Solr connection.
'''
if model == 'train':
self.model = models.Model()
elif model == 'svm':
self.model = models.LinearSVM()
elif model == 'nn':
self.model = models.NeuralNet()
elif model == 'bnn':
self.model = models.BranchingNeuralNet()
else:
self.model = models.NeuralNet()
self.debug = debug
self.features = features
self.candidates = candidates
self.error_handling = error_handling
self.solr_connection = solr.SolrConnection(SOLR_URL)
python类Model()的实例源码
def rank(self):
'''
Rank candidates according to trained model.
'''
for c in self.filtered_candidates:
c.set_prob_features()
# Only calculate prob if not in training mode
if self.model.__class__.__name__ != 'Model':
example = []
for j in range(len(self.model.features)):
feature = getattr(c, self.model.features[j])
example.append(float(feature))
c.prob = self.model.predict(example)
self.ranked_candidates = sorted(self.filtered_candidates,
key=attrgetter('prob'), reverse=True)
def __init__(self):
self.NAME = "Alexandria"
self.VERSION = "0.1"
# Model
self.model = models.Model()
# Configuration file
self.conf_file = AlexandriaConfiguration("alexandria.conf")
# Build driver list from configuration file
driver_name_list = self.conf_file.get_drivers()
self.drivers = drivers.DriverCollection()
# Create objects !!!! TO BE CONTINUED !!!!
for driver_name in driver_name_list:
# Get class
driver_class = getattr(sys.modules["drivers"], driver_name.capitalize())
# Create object
driver_object = driver_class()
# Add to driver list
self.drivers.append(driver_object)
index = self.drivers.index(driver_object)
# Set an attribute to the coresponding driver
setattr(self.drivers, driver_name.lower(), self.drivers[index])