# -*- coding: utf-8 -*-
'''
Created on 2009-11-15

@author: Bartosz Broda
'''

import codecs,random,sys
from optparse import OptionParser

def load_features(filename):
	features = []
	first_line = True
	for line in codecs.open(filename,'r', 'utf8'):
		if first_line:
			first_line = False
			continue		
		line = line.strip()
		if len(line):
			label,nz,tf,TF,entropy = line.split(';')			
			features.append((label,int(nz),int(tf),int(TF),float(entropy)))
	return features

def draw_random_words(words, no):
	random.seed()
	random.shuffle(words)
	return sorted(words[:no])

def load_wn_words(filename, flex):
	words = set([])
	
	for line in codecs.open(filename, 'r', 'utf8'):
		line = line.strip()
		if len(line):
			if flex:
				words.add(flex + ':' + line)
			else:
				words.add(line)
	return words

def main(args):
	parser = OptionParser()
	parser.add_option("-f", "--file", dest="features_filename", help="matrix features file")
	parser.add_option("-o", "--out", dest="output_filename", help="output file (default=out.txt) ", default='out.txt' )	
	parser.add_option("-n", "--number", dest="no", type='int', help="number of words to draw(default=100)", default=100)
	
	parser.add_option("-w", "--wordnet-words", dest="wnwords", type='string', help="words in wordnet (only those are cosidered)")
	parser.add_option("-l", "--add-flex", dest="flex", type='string', help="flex to add to wordnet words (default=subst)", default='subst')
	
	parser.add_option("-t", "--min-tf", dest="mintf", type='int', help="min value for term frequency (default=0)", default=0)
	parser.add_option("-T", "--min-TF", dest="minTF", type='int', help="min value for global term frequency (default=0)", default=0)
	parser.add_option("-z", "--min-nz", dest="minnz", type='int', help="min value for number of non zeros (default=0)", default=0)
	
	parser.add_option("-m", "--max-tf", dest="maxtf", type='int', help="max value for term frequency (default=maxint)", default=sys.maxint)
	parser.add_option("-a", "--max-TF", dest="maxTF", type='int', help="max value for global term frequency (default=maxint)", default=sys.maxint)
	parser.add_option("-x", "--max-nz", dest="maxnz", type='int', help="max value for number of non zeros (default=maxint)", default=sys.maxint)
	
	options = parser.parse_args(args)[0]
	
	features = load_features(options.features_filename)
	words = [x[0] for x in features if x[1] > options.minnz 
										and x[1] < options.maxnz
										and x[2] > options.mintf 
										and x[2] < options.maxtf
										and x[3] > options.minTF 
										and x[3] < options.maxTF
			]

	if options.wnwords:
		wnwords = load_wn_words(options.wnwords, options.flex)
		words = sorted( wnwords.intersection(words))
		
	with codecs.open(options.output_filename, 'w', 'utf8') as out:
		for w in draw_random_words(words, options.no):
			out.write('%s\n' % w)
	
if __name__ == '__main__':
	main(sys.argv)
	