#python
'''Compute rhythmic profile'''

import sys
import os
import numpy
import pylab

from select_paragraphs import misc
from select_paragraphs import properties

TEXTS = '/proj/aesop/Texts_for_recording'




'''
#def comp_prof(v):
    p = []
    i = 0
    while i < len(v) :
	if i+1 < len(v):
	    (a, b) = v[i:i+2]
	else:
	    a = v[i]
	    b = ''
	print i, a, b
	if "'" in a or "'" in b:
	    p.append(1)
	else:
	    p.append(0)
	i += 2
    return p
'''

def comp_prof(v):
    p = []
    print len(v)
    if len(v) < 8:
	v.append('')
    for i in range(0, len(v)):
	if "'" in v[i]:
	    p.append(1)
	else:
	    p.append(0)
    return p
	

def get_profile(f, lang, allpr, n):
    for line in open(f, 'r'):
	phl = line.strip('\n').split( )
	vowels = properties.get('vowels_list', lang, phl)
	pr = comp_prof(vowels)
	allpr = allpr+numpy.array(pr)
	n +=1 
    return (allpr, n)

def plot_prof(data):
    pylab.figure()
    pylab.plot(data)
    pylab.show()
    
def get_data(lang):
    data = []
    prof = numpy.zeros(8)
    n = 0
    for f in [f for f in os.listdir(os.path.join(TEXTS, lang)) if ('Poetry_' in f and 'phones' in f and '~' not in f and f!='Poetry_'+lang+'.phones')]:
	print f
	(prof, n) = get_profile(os.path.join(TEXTS, lang, f), lang, prof, n)
    print prof, n, prof/float(n)
    return prof

if __name__ == '__main__':
    lang = sys.argv[1]
    data = get_data(lang)
    plot_prof(data)
 
   
    
    
    #read all poetry in TFR
    #separate the vowels
    #Check the number of vowels
    #Get the stressed and unstressed ones
    #Draw the profile










