#!/usr/bin/python

import codecs, random
import pymatrices, pycomparator


mdir = '/mnt/data/workdir/macierze/supermatrix2/verb/polaczone/'
mname= 'kipi_knigi_rzepa_verb_3000_3010_3030_all_imiemodsubst'
#mdir = '/mnt/data/workdir/macierze/supermatrix2/noun/polaczone/'
#mname = 'kipi_knigi_rzepa_substAdjP_AmISubj_Coord_NGen_pq'

#mdir = '/mnt/data/workdir/macierze/supermatrix2/noun/'
#mname = 'kipi_substAdjP_pq'

matrix = pymatrices.LoadSuperMatrix(mdir, mname, False, True)
row_features = matrix.GetRowFeatures()

#cmp = pycomparator.FunctionFactory.CreateFunction('rwf_lin(rank_size=10000, uniform_rank = false, partial_order=true, div_factor=1)', matrix, mdir)

cmp = pycomparator.FunctionFactory.CreateFunction('rwf_zscore(rank_size=10000, rz=2.57, uniform_rank = false)', matrix, mdir)

all_words = set([])
for row_no in xrange( matrix.NumberOfRows() ):
	all_words.add((row_features[row_no].GetLabel().decode('utf8')))

all_words = sorted(all_words)

random.shuffle( all_words )

words = all_words[:100]
next = 100
file = codecs.open('random_verb_klist_sample_zscore_lin.csv', 'w', 'utf8')
file2 = codecs.open('random_verb_klist_sample_zscore_lin.txt', 'w', 'utf8')

no = 1
for w in sorted(words):
	print "Processing %s (%d/%d) " % (w, no, len(words) )
	no += 1
	kbest = pycomparator.get_kbest_val(cmp, w.encode('utf8'), 10)

	while len(kbest) == 0:
		print "No klist for",w,
		file2.write( "Brak listy dla %s\n" % w )
		w = all_words[next]
		next += 1
		print "trying with",w
		kbest = pycomparator.get_kbest_val(cmp, w.encode('utf8'), 10)
	file2.write(w) 
	f = row_features[ matrix.GetRowIndex(w.encode('utf8')) ]
	file2.write( "    nz = %s, tf = %s, TF = %s\n" % (f.GetNZ(), f.GetTermFrequency(), f.GetGlobalTermFrequency()) )
	for (word,sim) in kbest:
		w2 = word.decode('utf8')[5:]
		file.write( '%s; %s; %s\n' % (sim, w[5:], w2) )
		file2.write( '\t%s (%s)\n' % (w2, sim))

file.close()

