""" Script to generate trianing data, testing data, 
and a dictionary of one hot vectors for each word used in the CFG """

import cfg
import numpy as np
import pickle

def create_data(obj, path):
	# Generic function for saving object to a file
	with open(path, 'wb') as file:
		pickle.dump(obj, file)

def create_corpus(num_sentences, path):
	# Generic function to create corpus of sentences using the CFG
	sentences = cfg.create_corpus(num_sentences)
	create_data(sentences, path)	

def create_dictionary():
	# Create vocabulary of words generated by CFG
	sentences = cfg.create_corpus(1000)
	vocab = []
	for sentence in sentences:
		for word in sentence:
			if word not in vocab:
				vocab.append(word)
	# Build dictionary of one hot vectors for each word			
	encodings = {}
	decodings = {}
	num_words = len(vocab)
	for index, word in enumerate(vocab):
		num = np.zeros((num_words,))
		num[index] = 1
		encodings.update({word : num})
		decodings.update({index : word})
	# Save to file
	create_data(encodings, '../data/encodings.data')
	create_data(decodings, '../data/decodings.data')
		
def create_training(num_sentences):
	create_corpus(num_sentences, '../data/training.data')
	
def create_testing(num_sentences):
	create_corpus(num_sentences, '../data/testing.data')
	
create_dictionary()
create_training(50000)
create_testing(5000)