# -*- coding: utf-8 -*-

import os,codecs,tarfile 

def load( file_or_dir ):
	if os.path.exists(file_or_dir):
		if os.path.isdir(file_or_dir):
			return load_dir(file_or_dir)
		elif tarfile.is_tarfile(file_or_dir):
			return load_tar_file(file_or_dir)
		elif os.path.isfile(file_or_dir):
			return load_plain_file(file_or_dir)
		else:
			raise IOError, "Unknown file type" + file_or_dir
	else:
		raise IOError, "File or directory does not exists: " + file_or_dir

def load_plain_file(filename):
	pairs = {} # word -> [words]
	
	for line in codecs.open(filename, 'r', 'utf8'):
		line = line.strip()
		if len(line):
			elems = line.split(';')
			if len(elems) == 2:
				w1 = elems[0].strip().lower()
				w2 = elems[1].strip().lower()
				if w1 in pairs:
					pairs[w1].append(w2)
				else:
					pairs[w1] = [w2]
	return pairs

def load_tar_file(filename):
	pairs = {} # word -> [words]
	tar = tarfile.open(filename, "r")
	for tarinfo in tar:
		if tarinfo.isreg():
			print "Loading", tarinfo.name
			for line in tar.extractfile(tarinfo.name):
				line = line.strip()
				if len(line):
					elems = line.split(';')
					if len(elems) == 2:
						w1 = elems[0].strip().decode('utf8')
						w2 = elems[1].strip().decode('utf8')
						if w1 in pairs:
							pairs[w1].append(w2)
						else:
							pairs[w1] = [w2]
	return pairs

def load_dir(dirname):
	raise NotImplementedError

