'''
This code checks for the rhyme pattern in each verse
checking the rhyme scheme for every sentence picking
the last 3/2/1 character and plotting the total against 
the verse count
'''
#!python2
#coding: utf8

import codecs
import re
import pylab as pl

PATH = "output.txt"
epochs = []
extracted_verses =  []
syllables = {}
rhyme_lines = []
x_axis = []
verse_count = 0
SEPARATOR = 'Epoch'
VERSE_SEPARATOR = "**********#"
H_VERSE_SEPARATOR = "%"
LINE_SEPARATOR = "$"

def check_rythm(hv, n):
    c = 0
    global syllables
    line_data = hv.split(LINE_SEPARATOR)
    for l in line_data:
        l = l.strip()
        if not l == '':
            syllable = l[-n:]
            if syllable in syllables:
                c += 1
            else:
                syllables[syllable] = None
    
    return c

def extract_verse(epoch):
    global syllables, rhyme_lines, x_axis, verse_count
    verse_data = epoch.split(VERSE_SEPARATOR)
    
    for v_data in verse_data:
        if not v_data == '':
            verse_count += 1
            halfverse_data = v_data.split(H_VERSE_SEPARATOR)
            syllables.clear()
            count1 = 0
            count2 = 0
            count3 = 0
            for hv_data in halfverse_data:
                if not hv_data == '':
                    ''' check for rhyme in last 3 characters '''
                    
                    count = check_rythm(hv_data, 3)
                    
                    if count == 0:
                        ''' check for rhyme in last 2 characters '''
                        
                        count = check_rythm(hv_data, 2)
                    
                        if count == 0:
                            ''' check for rhyme in last 1 character '''
                            
                            count = check_rythm(hv_data, 1)
                            count1 += count
                        else:
                            count2 += count
                    else:
                        count3 += count
            
            total = count3 + count2 + count1
            if total > 3:
                rhyme_lines.append(total)
                x_axis.append(verse_count)
            
def main():
    global epochs, rhyme_lines, x_axis
    
    '''Read from the output log generated by the RNN'''
    
    with codecs.open(PATH, "r", 'utf-8-sig') as input:
        data = input.read()
        data = re.sub('//', ' ', data)
        data = re.sub('/', ' ', data)
        lines = data.split("----")
        for line in lines:
            if not line=='' and not len(line.split()) == 0  and not line.split()[0] == SEPARATOR:
                epochs.append(line)
        
        for epoch in epochs:
            extract_verse(epoch)
        
        '''Plot the graph for the number of rhyming sentences verses the poem generated'''
        pl.plot(x_axis, rhyme_lines, '-r')
        pl.xlabel('Verse count')
        pl.ylabel('Count of rhyming sentences')
        pl.show()
        
if __name__=="__main__":
    main()
