# -*- coding: utf-8 -*-
import codecs

def load_pairs(filename, encoding = 'utf8'):
	pairs = set([])

	for line in codecs.open(filename, 'r', encoding):
		line = line.strip()
		if len(line) == 0: continue
		pair = line.split(';')
		pairs.add( (pair[0].strip(), pair[1].strip() ) )

	return pairs

def write_pairs(filename, pairs, encoding = 'utf8'):
	file = codecs.open(filename, 'w', encoding)
	for p in pairs:
		file.write( "%s;%s\n" % p )
	file.close()

def select_at_least(pairs_collection, n):
	all_pairs = set([])
	for pairs in pairs_collection:
		for p in pairs:
			all_pairs.add(p)
	
	selected = set([])
	for p in all_pairs:
		hits = 0
		for pairs in pairs_collection:
			if p in pairs:
				hits+=1
		if hits >= n:
			selected.add(p)


	return selected


if __name__ == '__main__':
	print "MAIN"
	f_jest = '/home/boban/data/wyniki/klasHip/kipi_knigi_rzepa_opbJestInst.txt'
	f_inne = '/home/boban/data/wyniki/klasHip/kipi_knigi_rzepa_opbMergedIInne.txt'
	f_nom = '/home/boban/data/wyniki/klasHip/kipi_knigi_rzepa_opbNomToNom.txt'

	jest = load_pairs(f_jest)
	inne = load_pairs(f_inne)
	nom = load_pairs(f_nom)

	all = [jest, inne, nom]

	inter = jest.intersection(inne).intersection(nom)

	write_pairs('kipi_kniki_rzepa_przeciecie_JestInst_MergedIInne_NomToNom.txt', inter)

	at_least2 = select_at_least( all, 2 )

	write_pairs('kipi_kniki_rzepa_at_least2_JestInst_MergedIInne_NomToNom.txt', at_least2)
