#!/usr/bin/python
#encoding: utf8

'''
python make_models.py \
	-c ../cfg/classification.ini \
	-l /mnt/data/home/pkedzia/work/matrices/other/wsd_dane/lexcsd/window_base_20_20/marek_ann_no_pl/marek_ann_zamek.tar.bz2
'''

import os, sys, argparse
import ltcore.io.matrix as matrixio
from ltlearn.classification.makers import maker

try:
	import argcomplete
except ImportError:
	argcomplete = None

def make_parser():
	desc = 'Makes classifiers models for given classification config file ' \
			'and given matrix learn.'

	parser = argparse.ArgumentParser(description = desc)
	parser.add_argument('-c', '--config-file', 
		dest = 'config_file', required = True)
	parser.add_argument('-l', '--learning-matrix-path', 
		dest = 'learning_matrix_path', required = True)
	
	if argcomplete:
		argcomplete.autocomplete(parser)
	return parser

def make_classifiers(cfg_file):
	"""Makes classifiers based on the config file"""

	mkr = maker.Maker()
	return mkr.make_classifiers_from_config(cfg_file)

def main(argv = None):
	args = make_parser().parse_args(argv)

	classifiers = make_classifiers(args.config_file)
	for classifier in classifiers:
		classifier.set_data_type('symbolic')
		print >> sys.stderr, "Classifier: ", classifier._user_classifier_path 
		print >> sys.stderr, "  Learning...",
		classifier.learn(args.learning_matrix_path)
		print >> sys.stderr, " done"
		
		print >> sys.stderr, "  Serializing...",
		out_mdl = "lexcsd_%s.mdl" % (classifier._user_classifier_path)
		classifier.serialize(out_mdl)
		print >> sys.stderr, " done"

if __name__ == '__main__':
	main()

