#!/usr/bin/env python3

'''
    Quinema Kurt Vonnegut
    Analisis de guion o textos basado en la suerte del personaje protagonista
    Copyright (C) 2021  Ernesto Bazzano (bazza)

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.

'''

import os
import sys
import numpy as np
import re
import random

if len(sys.argv) < 1:
	exit("\n  ./simplificar texto puerta tiempo\n")
#---------------------------------------
def savitzky_golay(y, window_size, order, deriv=0, rate=1):
	import numpy as np
	from math import factorial
	window_size = np.abs(np.int32(window_size))
	order = np.abs(np.int32(order))
	if window_size % 2 != 1 or window_size < 1:
		window_size = window_size + 1
	if window_size < order + 2:
		raise TypeError("window_size is too small for the polynomials order")
	order_range = range(order+1)
	half_window = (window_size -1) // 2
	# aca deberia haber un limitea para la puerta si no hay datos
	b = np.mat([[k**i for i in order_range] for k in range(-half_window, half_window+1)])
	m = np.linalg.pinv(b).A[deriv] * rate**deriv * factorial(deriv)
	firstvals = y[0] - np.abs( y[1:half_window+1][::-1] - y[0] )
	lastvals = y[-1] + np.abs(y[-half_window-1:-1][::-1] - y[-1])
	y = np.concatenate((firstvals, y, lastvals))
	return np.convolve( m[::-1], y, mode='valid')
#---------------------------------------
hedonometer = open("hedonometer.csv",'r').read().lower().split("\n")
del hedonometer[0]
regex = re.compile(r'[^a-zA-Z0-9\ áíéúó]') 
guion =regex.sub(r' ', open(sys.argv[1],'r').read().lower()).split()
#---------------------------------------
if (len(guion) < 280):
	tiempo = len(guion)
else:
	tiempo = int(len(guion)/200)
#---------------------------------------
puerta = 50
#if (len(sys.argv) > 1):
#	puerta = int (sys.argv[2])
x = np.array([])
n = np.array([])
#---------------------------------------
for palabra in guion:
	for punto in hedonometer:
		if ( punto.find("\""+palabra+"\"") > 1 ):
			n = np.append(n, (float(punto.split(",\"")[3].split("\"")[0])))
#---------------------------------------
n1 = np.array([])
n2 = np.array([])
w = n

for k in range(int(len(n)/puerta)):
	n1 = np.append(n1, np.mean(n))
	n2 = np.append(n2, n[len(n)-random.randint(1, int(len(n)/20))])
#-------------MAGIA---------------------
n = np.concatenate((n1, n))
n = np.concatenate((n, n2))

for p in {50,35,15}:
	try:
		x = savitzky_golay(n, int(len(w)/p), 1)
	except:
		x = x
	n = x

#---------------------------------------
x = np.delete(x, range(int(len(w)/puerta)))
x = np.delete(x, range(len(x)-(int(len(w)/puerta)), len(x)))
#media = np.mean(x)
#for k in range(len(x)):
#	x[k] = ((x[k] - media) * (puerta/15)) + media
#---------------------------------------
for i in range(0,len(x)):
	t = tiempo/len(x)*i
	print(str("%01.4f" % t) +"\t"+ str("%01.3f" % x[i]))