# -*- coding: utf-8 -*-
#!/usr/bin/env python
import codecs, numpy, pylab
import pyplwn

poses = ['subst', 'verb', 'adj' ]
verb_flex = set(['fin', 'praet', 'impt', 'imps', 'inf'])
bins = [0,50,100,250,500,750,1000]

def load_udict(filename, enc='utf8'):
	udict = {}
	for line in codecs.open(filename, 'r', enc):
		line = line.strip()
		if len(line):
			(flexword,tf) = line.split(':=')
			flexword = flexword.split(':')
			flex = flexword[0]
			if flex in verb_flex:
				flex = 'verb'
			word = ":".join(flexword[1:])
			tf = int(tf)
			if flex in udict:
				dict = udict[flex] 
				if word in dict:
					dict[ word ] =  dict[word] + tf
				else:
					dict[word] = tf
			else:
				udict[flex] = {}
				udict[flex][word] = tf
	return udict


def load_plwn_words():
	words = {}
	db = pyplwn.database.Database('plwn', 'plwn', 'localhost', 'plwn')
	for pos in poses:
		words[pos] = set([])
		db.poses = [pyplwn.database.DBEnums.get_pos(pos)]
		lemmas = db.LexicalUnits.get_all_lemmas()
		for l in lemmas:
			words[pos].add(l)
	return words

def draw_histogram(wn_words, udict, pos, fileout, show = False):
	words = wn_words[pos]
	dict = udict[pos]
	zeros = 0
	vals = numpy.zeros( len(bins) )
	v2 = []
	i = -1
	for w in words:
		i += 1
		if not w in dict:
			zeros += 1
			continue
		tf = dict[w]

		if tf > 1000:
			v2.append(1000)
		else:
			v2.append(tf)

		found = False
		for i in xrange(len(bins)-1):
			curr = bins[i]
			next = bins[i+1]
			if tf >= curr and tf < next:
				found = True
				vals[i] += 1
				break
		if not found:
			vals[-1] += 1
	
	i = 1
	for x in vals:
		if i < len(bins):
			print 'up to ' + str(bins[i]),';',x
		else:
			print 'more then ' + str(bins[-1]) , ';', x
		i += 1
	
	pylab.hist(v2, 9)


	pylab.title( "%s slow w kategorii %s (brak danych dla %s) " % (len(words), pos, zeros) )
	pylab.ylabel("Liczba slow")
	pylab.xlabel(u"Czestosc")
	pylab.savefig(fileout)
	if show:
		pylab.show()
	pylab.close()



def main():
	#udict_file = '/mnt/data/workdir/macierze/supermatrix2/noun/polaczone/kipi_knigi_rzepa_substAdjP_AmISubj_Coord_NGen_pq/udict.txt'
	udict_file = '/mnt/data/workdir/macierze/supermatrix2/noun/polaczone/kipi_subst_AdjP_AmISubj_Coord_NGen_pq_wrong_boundary/udict.txt'
	print "Loading udict"
	udict = load_udict(udict_file)
	print "Loading wordnet words"
	wnwords = load_plwn_words()
	print "Drawing histogram"
	for pos in poses:
		draw_histogram(wnwords, udict, pos , pos + '_kipi.png' )

if __name__ == '__main__':
	main()

