<jupyter_start><jupyter_text>CS155 Project 3 Recurrent Neural NetworksIn this notebook, we generate Shakespeare poems using LSTM-RNNs.  Functions<jupyter_code>import re
import pandas as pd
from keras.models import load_model
import WilliamRNN as willr
from nltk.tokenize import SyllableTokenizer

Ts = [1.5,0.75,0.25]
Nunits = [100,150,200]

def LoadRNNs(Nsemi):
    iT = 0
    models = []
    for Nunit in Nunits:
        model_Ts = []
        iT = 0
        for T in Ts:
            model = load_model('models/Nsemi'+str(Nsemi)+'/'+'rnn_iT_'+str(iT)+'_units_'+str(Nunit)+'.h5')
            iT += 1
            model_Ts.append(model)
        models.append(model_Ts)
    return models

def GetSyllablesDict():
    df = pd.read_csv('data/Syllable_dictionary.txt',header=None)
    df_vals = df.values
    syl_dic = {}
    syl_dic_E = {}
    for val in df_vals:
        dics = val[0].split()
        if len(dics) == 2:
            syl_dic[re.sub(r'[^\w]','',dics[0])] = int(dics[1])
        elif len(dics) == 3:
            try:
                syl_dic[re.sub(r'[^\w]','',dics[0])] = int(dics[2])
                eIDX = 1
            except:
                syl_dic[re.sub(r'[^\w]','',dics[0])] = int(dics[1])
                eIDX = 2
            syl_dic_E[re.sub(r'[^\w]','',dics[0])] = int(dics[eIDX].strip('E'))
    return syl_dic,syl_dic_E

def GetPoem(models,Ns):
    SSP = SyllableTokenizer()
    will = willr.William(seq_length=41,Nsemi=Ns)
    syl_dic,syl_dic_E = GetSyllablesDict()
    for i in range(len(Nunits)):
        for j in range(len(Ts)):
            model = models[i][j]
            print('####### LSTM-RNN: semi-redundant sequences with n = '+str(Ns)+' #######')
            print('T = '+str(Ts[j])+' and Nunit = '+str(Nunits[i]))
            input_c = list("shall i compare thee to a summer's day?\n")
            text = will.GenerateAIpoem(input_c,model,1000)
            lines = [line for line in text.split('\n')]
            Nsyl_list = []
            for l in range(len(lines)):
                line = lines[l]
                Nsyl = 0
                line_s = line.split()
                for word in line_s:
                    word = re.sub(r'[^\w]','',word).lower()
                    try:
                        if i != len(line):
                            Nsyl += syl_dic[word]
                        else:
                            Nsyl += syl_dic_E[word]
                    except:
                        if word != '':
                            Nsyl += len(SSP.tokenize(word))
                if Nsyl <= 9:
                    print('(#syllables  '+str(Nsyl)+')',line)
                else:
                    print('(#syllables '+str(Nsyl)+')',line)
                Nsyl_list.append(Nsyl)
                if l == 13:
                    break
            mean = sum(Nsyl_list)/len(Nsyl_list) 
            variance = sum([((x-mean) ** 2) for x in Nsyl_list])/len(Nsyl_list)
            std = variance ** 0.5
            print('Average #syllables: '+str(mean))
            print('Standard deviation #syllables: '+str(std))
            print('#############################################################','\n')
    pass
            
def GetErrors(models,Ns):
    will = willr.William(seq_length=41,Nsemi=Ns)
    for i in range(len(Nunits)):
        for j in range(len(Ts)):
            model = models[i][j]
            print('####### LSTM-RNN: semi-redundant sequences with n = '+str(Ns)+' #######')
            print('T = '+str(Ts[j])+' and Nunit = '+str(Nunits[i]))
            model.fit(will.X,will.y,epochs=1,verbose=2)
            print('#############################################################')
    pass

def GetSinglePoem(model,input_c,Ns):
    SSP = SyllableTokenizer()
    will = willr.William(seq_length=41,Nsemi=Ns)
    syl_dic,syl_dic_E = GetSyllablesDict()
    print('############### My favorite poem ###############')
    text = will.GenerateAIpoem(input_c,model,1000)
    lines = [line for line in text.split('\n')]
    Nsyl_list = []
    for l in range(len(lines)):
        line = lines[l]
        Nsyl = 0
        line_s = line.split()
        for word in line_s:
            word = re.sub(r'[^\w]','',word).lower()
            try:
                if i != len(line):
                            Nsyl += syl_dic[word]
                else:
                            Nsyl += syl_dic_E[word]
            except:
                if word != '':
                    Nsyl += len(SSP.tokenize(word))
        if Nsyl <= 9:
            print('(#syllables  '+str(Nsyl)+')',line)
        else:
            print('(#syllables '+str(Nsyl)+')',line)
        Nsyl_list.append(Nsyl)
        if l == 13:
            break
    mean = sum(Nsyl_list)/len(Nsyl_list) 
    variance = sum([((x-mean) ** 2) for x in Nsyl_list])/len(Nsyl_list)
    std = variance ** 0.5
    print('Average #syllables: '+str(mean))
    print('Standard deviation #syllables: '+str(std))
    print('#################################################','\n')
    pass<jupyter_output><empty_output><jupyter_text>Load LSTM-RNNs (trained with semi-redundant sequences with n = 5, 10)<jupyter_code>models1 = LoadRNNs(1)
models5 = LoadRNNs(5)
models10 = LoadRNNs(10)<jupyter_output><empty_output><jupyter_text>Generate poems using LSTM-RNNs<jupyter_code>GetPoem(models1,1)
GetPoem(models5,5)
GetPoem(models10,10)<jupyter_output>####### LSTM-RNN: semi-redundant sequences with n = 10 #######
T = 1.5 and Nunit = 100
(#syllables 10) shall i compare thee to a summer's day?
(#syllables 10) whan thou art buthtesull be tough mery,
(#syllables 14) no fouldy premen eabtedo not fan rome tous groegh,
(#syllables 14) for with mumy lo seepich they reme to tee sume,
(#syllables 10) and your nee focke tress a vill noth live,
(#syllables  3)   leabey look,
(#syllables 15) ond not tat in i ne rind, and seeft suclift cate sime,
(#syllables  3) and your nee,
(#syllables 12) soise sace and suemen enouchich then frown,
(#syllables 10) when well fay folln or mand and ther foren,
(#syllables  8) that which stool deather ene lays,
(#syllables 13) and searing thit woyns tide's ove ar time is fore.
(#syllables  5)   in me sel sine,
(#syllables 10) soresting thou do spilens in ton end,
Average #syllables: 9.785714285714286
Standard deviation #syllables: 3.7450647796909515
############################################################# 

#[...]<jupyter_text>Obtain accuracies of each LSTM-RNN<jupyter_code>GetErrors(models1,1)
GetErrors(models5,5)
GetErrors(models10,10)<jupyter_output>####### LSTM-RNN: semi-redundant sequences with n = 10 #######
T = 1.5 and Nunit = 100
Epoch 1/1
 - 19s - loss: 0.1596 - accuracy: 0.9660
#############################################################
####### LSTM-RNN: semi-redundant sequences with n = 10 #######
T = 0.75 and Nunit = 100
Epoch 1/1
 - 18s - loss: 0.0339 - accuracy: 0.9999
#############################################################
####### LSTM-RNN: semi-redundant sequences with n = 10 #######
T = 0.25 and Nunit = 100
Epoch 1/1
 - 18s - loss: 0.0039 - accuracy: 1.0000
#############################################################
####### LSTM-RNN: semi-redundant sequences with n = 10 #######
T = 1.5 and Nunit = 150
Epoch 1/1
 - 23s - loss: 0.0167 - accuracy: 1.0000
#############################################################
####### LSTM-RNN: semi-redundant sequences with n = 10 #######
T = 0.75 and Nunit = 150
Epoch 1/1
 - 23s - loss: 0.0053 - accuracy: 1.0000
###########################################################[...]<jupyter_text>My favorite poem<jupyter_code>GetSinglePoem(models1[2][1],list("i can't stop this feeling, deep inside of me,\n"),1)<jupyter_output>############### My favorite poem ###############
(#syllables 12) i can't stop this feeling, deep inside of me,
(#syllables 13) liquing in thine eye bate my gross do bent,
(#syllables 11) by advich controw it so forgot a lick,
(#syllables 14) i may begues, that thou mayst have those their dear deem,
(#syllables 12) but what of thee, and for thy self art pleasure,
(#syllables 12)   but daves more blest thee vanticing crossed,
(#syllables 12) the poor of this proud come his rick) compite!
(#syllables  9) hoon, thou the sweetest broods on thee it;
(#syllables 13) both from thy fair) to flatterer thou abundarness,
(#syllables 11) right despised strangle and love doth seen,
(#syllables 12) all stars in thy heart is love therefore,
(#syllables 12)   they may detiven makes and soonur all wrong.
(#syllables 13) i will not polis conforned af your rarse,
(#syllables 14) their hold and then more in hope's all, with men's eyes,
Average #syllables: 12.142857142857142
Standard deviation #syllables: 1[...]
{'fraction_non_alphanumeric': 0.20055517002081888, 'fraction_numerical': 0.036433032616238724, 'mean_word_length': 2.7352051835853133, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '649038', 'license': 'MIT', 'n_tokens_mistral': 3258, 'n_tokens_neox': 2907, 'n_words': 757, 'path': 'project3/CS155project3_RNN.ipynb', 'repo_name': 'AstroHiro/CS155', 'size': '8646'}