repl.it
@AmeliaBlackwell/

Poem Maker

Python

No description

fork
loading
Files
  • main.py
  • All works.txt
  • Shakespeare's sonnets.txt
  • Works of Dr Seuss.txt
  • Works of Edgar Allen Poe.txt
  • Works of Elizabeth Barrett Browning.txt
  • Works of Emily Dickinson.txt
  • Works of Lord Byron.txt
  • Works of Percy Bysshe Shelley.txt
  • Works of Wilfred Owen.txt
  • Works of William Blake.txt
  • Works of William Wordsworth.txt
  • Packager files
  • poetry.lock
  • pyproject.toml
  • requirements.txt
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
import re
import pronouncing
import random as rand
from string import capwords
#import grammar_check

f=100 #lower number means poem is more about a specific topic but less gramatically correct

class Poeclass:

    def __init__(self, trained=False, words=[], wordprobs=[], wordnames=[]):
        self.trained = trained
        self.words = words
        self.wordprobs = wordprobs
        self.wordnames=wordnames
        self.words.append(Word("xyz", 0, []))


class Word:


    def __init__(self, word, freq, afters):
        self.word = word
        self.freq = freq
        self.afters = [afters]
        self.rhymes = pronouncing.rhymes(word)
        if "'" in word:
            tempword = word.split("'")
            phone = pronouncing.phones_for_word(tempword[0])
        else:
            phone = pronouncing.phones_for_word(word)
        if phone == []:
            self.syllables = 1
        else:
            self.syllables = pronouncing.syllable_count(phone[0])
        self.afterprobs = []
        self.befores = []
        self.beforeprobs = []


class Game:

    def __init__(self):
        pass

class Text:

    def __init__(self, name):
        self.name = name
        self.read = False
    
class Topic:

    def __init__(self, topic):
        self.topic = topic


texts = [Text("Shakespeare's sonnets"), Text("Works of Edgar Allen Poe"), Text("Works of Percy Bysshe Shelley"), Text("Works of William Blake"), Text("Works of William Wordsworth"), Text("Works of Emily Dickinson"), Text("Works of Elizabeth Barrett Browning"), Text("Works of Lord Byron"), Text("Works of Wilfred Owen"), Text("Works of Dr Seuss"), Text("All works")]
    

def findobj(toapp):
    try:
        for word in Poe.words:
            if toapp == word.word:
                return word
    except:
        pass

def findlastword(lines):
    try:
        last = lines[-1][-1][-1]
    except:
        last = lines[-1][-1]
    return last

# def findbefores(tofind):
#     for word in Poe.words:
#         for after in word.afters:
#             if tofind==after[0]:
#                 tofind.befores.append(word)
#                 for x in range(after[1]):
#                     tofind.beforeprobs.append(word)

def train(choice, texts):
    print("Reading {}...".format(texts[choice-1].name))
    textfile = open("{}.txt".format(texts[choice-1].name), "r")
    text = [x.strip() for x in textfile.readlines()]
    textfile.close()
    puncsplitlines = []
    for line in text:
        x = (re.split("[,.()?!:;<>]", line))
        for puncsplitphrase in x:
            puncsplitlines.append(puncsplitphrase)
    words = []
    Poe.wordnames=[]
    for doneword in Poe.words:
        Poe.wordnames.append(doneword.word)
    for puncsplitline in puncsplitlines:
        for word in puncsplitline.split(" "):
            word=word.strip("\"")
            word=word.strip("'")
            word=word.strip("[]")
            word=word.strip("1234567890")
            if word != "" and word.lower()!= "page":
                try:
                    val = int(word)
                except ValueError:
                    try:
                        val2 = int(word.strip("[]"))
                    except ValueError:
                        word=word.strip("[]")
                        words.append(word.lower())

    for n in range(0, len(words)-1):
        o = n+1
        if words[n] in Poe.wordnames:
            for word in Poe.words:
                if word.word == words[n]:
                    word.freq += 1
                    if n < len(words)-1:
                        foundafter = False
                        try:
                            for after in word.afters:
                                if after[0] == words[o]:
                                    after[1] += 1
                                    foundafter = True
                            if not foundafter:
                                word.afters.append([words[o], 1])
                        except:
                            print(words[n])
        elif words[n] not in Poe.wordnames:
            if n < len(words)-1:
                wordobj = Word(words[n], 1, [words[o], 1])
            else:
                wordobj = Word(words[n], 1, [])
            Poe.words.append(wordobj)
            Poe.wordnames.append(words[n].lower())


    # for word in Poe.words:
    #     findbefores(word)

    Poe.wordprobs=[]
    for word in Poe.words:
        word.afterprobs=[]
        try:
            for after in word.afters:
                for y in range(after[1]):
                    word.afterprobs.append(findobj(after[0]))
            for x in range(word.freq):
                Poe.wordprobs.append(word)
        except:
            pass

    texts[choice-1].read = True
    Poe.trained = True
    # print("done")
    # for x in Poe.words:
    #     if x.word == "maiden":
    #         print(x.word, x.freq, x.syllables, x.rhymes, x.afters)

def firstwordfind():
    print("Enter topic:")
    tdone=False
    while not tdone:
        topic=input().strip(" ")
        if topic in Poe.wordnames:
            firstword = findobj(topic)
            tdone=True
        else:
            print("Invalid topic. Please choose again:")
    curtop = Topic(firstword)
    return curtop, firstword

def shakespeareansonnet(firstword):
    print("Writing Shakespearean sonnet...")
    lines = []
    for x in range(1,3):
        qdone = False
        while not qdone:
            try:
                if x==1:
                    quatrain = quatrainABAB(lines, 10, firstword)
                else:
                    quatrain = quatrainABAB(lines, 10)
                qdone = True
            except:
                pass
        lines.append(quatrain)
    cdone = False
    while not cdone:
        try:
            couple = coupletAA(lines, 10)
            cdone = True
        except:
            pass
    lines.append(couple)
    return lines

def petrarchansonnet(firstword):
    print("Writing Petrarchan sonnet...")
    lines=[]
    odone=False
    while not odone:
        try:
            octa = octaveABBAABBA(lines, firstword)
            odone = True
        except:
            pass
    lines.append(octa)
    sdone = False
    while not sdone:
        try:
            sest = sestetCDECDE(lines)
            sdone = True
        except:
            pass
    lines.append(sest)
    return lines

def curtalsonnet(firstword):
    print("Writing curtal sonnet...")
    lines=[]
    sdone=False
    while not sdone:
        try:
            sest = sestetCDECDE(lines, firstword)
            sdone = True
        except:
            pass
    lines.append(sest)
    quintet=[]
    ldone=False
    while not ldone:
        try:
            l = line(lines, 10, findobj("xyz"))
            ldone=True
        except:
            pass
    quintet.append(l)
    ldone=False
    while not ldone:
        try:
            l = line(quintet, 10, sest[1][-1])
            ldone=True
        except:
            pass
    quintet.append(l)
    ldone=False
    while not ldone:
        try:
            l = line(quintet, 10, sest[2][-1])
            ldone=True
        except:
            pass
    quintet.append(l)
    ldone=False
    while not ldone:
        try:
            l = line(quintet, 10, quintet[0][-1])
            ldone=True
        except:
            pass
    quintet.append(l)
    ldone=False
    while not ldone:
        try:
            l = line(quintet, 2, quintet[2][-1])
            ldone=True
        except:
            pass
    quintet.append(l)
    lines.append(quintet)
    return lines


def haiku(firstword):
    print("Writing haiku...")
    lines=[]
    syls = [5,7,5]
    c=0
    for syl in syls:
        ldone=False
        while not ldone:
            try:
                if c==0:
                    l=line(lines,syl,findobj("xyz"),firstword)
                else:
                    l=line(lines,syl,findobj("xyz"))
                ldone=True
            except:
                pass
        c+=1
        lines.append(l)
    return lines

def limerick(firstword):
    print("Writing limerick...")
    lines=[]
    limdone = False
    while not limdone:
        try:
            lim = limerickAABBA(lines, firstword)
            limdone = True
        except:
            pass
    lines.append(lim)
    return lim

def couplet(firstword):
    print("Writing couplet...")
    lines=[]
    syl=rand.randint(6,12)
    cdone = False
    while not cdone:
        try:
            c = coupletAA(lines, syl, firstword)
            cdone = True
        except:
            pass
    lines.append(c)
    return c

def freeverse(firstword):
    print("Writing free verse...")
    lines=[]
    linev = rand.randint(5,20)
    for x in range(1,linev):
        ldone = False
        s = rand.randint(6,12)
        while not ldone:
            try:
                if x==1:
                    l=line(lines,s,findobj("xyz"),firstword)
                else:
                    l = line(lines, s, findobj("xyz"))
                ldone = True
            except:
                pass
        lines.append(l)
    return lines

def rhymingstanzas(firstword):
    print("Writing rhyming stanzas...")
    sn = rand.randint(2,8)
    syl = rand.randint(5,10)
    lines = []
    for x in range(1,sn):
        sdone = False
        while not sdone:
            try:
                if x==1:
                    stanz = quatrainABAB(lines, syl, firstword)
                else:
                    stanz = quatrainABAB(lines, syl)
                sdone = True
            except:
                pass
        lines.append(stanz)
    return lines

def rhymelessstanzas(firstword):
    print("Writing non-rhyming stanzas...")
    sn = rand.randint(2,8)
    syl = rand.randint(5,10)
    lines = []
    for x in range(1,sn):
        sdone = False
        while not sdone:
            try:
                if x==1:
                    stanz = stanzanorhyme(lines, syl, firstword)
                else:
                    stanz = stanzanorhyme(lines, syl)
                sdone = True
            except:
                pass
        lines.append(stanz)
    return lines

def multicouplets(firstword):
    print("Writing multiple couplets...")
    cn = rand.randint(3,10)
    syl=rand.randint(6,12)
    lines=[]
    for x in range(1,cn):
        cdone=False
        while not cdone:
            try:
                if x==1:
                    cou=coupletAA(lines,syl, firstword)
                else:
                    cou=coupletAA(lines,syl)
                cdone=True
            except:
                pass
        lines.append(cou)
    return lines

def cinquain(firstword):
    print("Writing cinquain...")
    syls=[2,4,6,8,2]
    lines=[]
    c=0
    for syl in syls:
        ldone=False
        while not ldone:
            try:
                if c==0:
                    l = line(lines, syl, findobj("xyz"), firstword)
                else:
                    l = line(lines, syl, findobj("xyz"))
                ldone=True
            except:
                pass
        c+=1
        lines.append(l)
    return lines

def monotetra(firstword):
    print("Writing monotetra...")
    lines=[]
    sn = rand.randint(2,8)
    for n in range(1,sn):
        sdone = False
        while not sdone:
            try:
                if n==1:
                    s = quatrainAAAA(lines, 8, firstword)
                else:
                    s = quatrainAAAA(lines, 8)
                sdone = True
            except:
                pass
        lines.append(s)
    return lines

def clogyrnach(firstword):
    print("Writing clogyrnach...")
    lines=[]
    cdone=False
    while not cdone:
        try:
            c = coupletAA(lines, 8, firstword)
            cdone = True
        except:
            pass
    for l in c:
        lines.append(l)
    cdone=False
    while not cdone:
        try:
            c = coupletAA(lines, 5)
            cdone = True
        except:
            pass
    for l in c:
        lines.append(l)
    ldone=False
    while not ldone:
        try:
            l = line(lines, 3, lines[2][-1])
            ldone = True
        except:
            pass
    lines.append(l)
    ldone=False
    while not ldone:
        try:
            l = line(lines, 3, lines[0][-1])
            ldone = True
        except:
            pass
    lines.append(l)
    return lines

def dodoitsu(firstword):
    print("Writing dodoitsu...")
    lines=[]
    syls=[7,7,7,5]
    c=0
    for syl in syls:
        ldone=False
        while not ldone:
            try:
                if c==0:
                    l=line(lines,syl,findobj("xyz"), firstword)
                else:
                    l=line(lines,syl,findobj("xyz"))
                ldone=True
            except:
                pass
        c+=1
        lines.append(l)
    return lines

def rhymingacrostic(firstword):
    print("Enter the word down the side:")
    sideword = input().lower()
    print("Writing rhyming acrostic...")
    c=0
    lines=[]
    syl=rand.randint(6,10)
    ldone=False
    while not ldone:
        try:
            sta=acrrhyme(lines,syl,firstword,sideword)
            ldone=True
        except:
            pass
    lines=sta
    return lines

def nonrhymingacrostic(firstword):
    print("Enter the word down the side:")
    sideword = input().lower()
    print("Writing non-rhyming acrostic...")
    c=0
    lines=[]
    syl=rand.randint(6,10)
    for letter in sideword:
        ldone = False
        while not ldone:
            try:
                if c==0:
                    l=line(lines,syl,findobj("xyz"),firstword,letter)
                else:
                    l=line(lines,syl,findobj("xyz"),sideletter=letter)
                ldone=True
            except:
                pass
        c+=1
        lines.append(l)
    return lines


def stanzanorhyme(lines, syl, firstword=None):
    sta=[]
    sta.append(line(lines, syl, findobj("xyz"), firstword))
    sta.append(line(sta, syl, findobj("xyz")))
    sta.append(line(sta, syl, findobj("xyz")))
    sta.append(line(sta, syl, findobj("xyz")))
    return sta

def acrrhyme(lines,syl,firstword=None,sideword=None):
    sta=[]
    c=0
    for letter in sideword:
        if c==0:
            sta.append(line(sta,syl,findobj("xyz"),firstword,letter))
        elif c%4 < 2:
            sta.append(line(sta,syl,findobj("xyz"),sideletter=letter))
        else:
            sta.append(line(sta,syl,sta[-2][-1],sideletter=letter))
        c+=1
    return sta

def limerickAABBA(lines, firstword=None):
    lim=[]
    randsyl = rand.randint(7,9)
    lim.append(line(lines, randsyl, findobj("xyz"), firstword))
    lim.append(line(lim, randsyl, lim[0][-1]))
    lim.append(line(lim, randsyl-3, findobj("xyz")))
    lim.append(line(lim, randsyl-3, lim[2][-1]))
    lim.append(line(lim, randsyl, lim[0][-1]))
    return lim

def quatrainABAB(lines, syl, firstword=None):
    quat=[]
    quat.append(line(lines, syl, findobj("xyz"), firstword))
    quat.append(line(quat, syl, findobj("xyz")))
    quat.append(line(quat, syl, quat[0][-1]))
    quat.append(line(quat, syl, quat[1][-1]))
    return quat

def quatrainAAAA(lines,syl, firstword=None):
    quat=[]
    quat.append(line(lines, syl, findobj("xyz"), firstword))
    quat.append(line(quat, syl, quat[0][-1]))
    quat.append(line(quat, syl, quat[0][-1]))
    quat.append(line(quat, syl/2, quat[0][-1]))
    return quat


def coupletAA(lines, syl, firstword=None):
    coup=[]
    coup.append(line(lines, syl, findobj("xyz"), firstword))
    coup.append(line(coup, syl, coup[-1][-1]))
    return coup

def octaveABBAABBA(lines, firstword=None):
    octa=[]
    octa.append(line(lines, 10, findobj("xyz"), firstword))
    octa.append(line(octa, 10, findobj("xyz")))
    octa.append(line(octa, 10, octa[1][-1]))
    octa.append(line(octa, 10, octa[0][-1]))
    octa.append(line(octa, 10, octa[0][-1]))
    octa.append(line(octa, 10, octa[1][-1]))
    octa.append(line(octa, 10, octa[1][-1]))
    octa.append(line(octa, 10, octa[0][-1]))
    return octa

def sestetCDECDE(lines, firstword=None):
    sest=[]
    sest.append(line(lines, 10, findobj("xyz"), firstword))
    sest.append(line(sest, 10, findobj("xyz")))
    sest.append(line(sest, 10, findobj("xyz")))
    sest.append(line(sest, 10, sest[0][-1]))
    sest.append(line(sest, 10, sest[1][-1]))
    sest.append(line(sest, 10, sest[2][-1]))
    return sest


def findlast(line, sc, ts, rhyme):
    hypafterprobs=line[-1].afterprobs[:]
    for a in range(len(hypafterprobs)):
        hypafter = rand.choice(hypafterprobs)
        hypafterprobs.remove(hypafter)
        if sc + hypafter.syllables == ts and (hypafter.word in rhyme.rhymes or (rhyme.word == "xyz" and len(hypafter.rhymes)>0)):
            line.append(hypafter)
            sc = sc + line[-1].syllables
            return line, sc
    if sc < ts:
        hypafterprobs = line[-1].afterprobs[:]
        for a in range(len(hypafterprobs)):
            hypafter = rand.choice(hypafterprobs)
            hypafterprobs.remove(hypafter)
            hyp2afterprobs = hypafter.afterprobs[:]
            for b in range(len(hyp2afterprobs)):
                hyp2after = rand.choice(hyp2afterprobs)
                hyp2afterprobs.remove(hyp2after)
                if sc + hypafter.syllables + hyp2after.syllables == ts and (hyp2after.word in rhyme.rhymes or (rhyme.word == "xyz" and len(hyp2after.rhymes)>0)):
                    line.append(hypafter)
                    line.append(hyp2after)
                    sc = sc + hypafter.syllables + hyp2after.syllables
                    return line, sc
    return line, sc

def findacr(sideletter, afterprobs):
    posswords=[]
    for word in afterprobs:
        if word.word[0]==sideletter:
            posswords.append(word)
    if posswords:
        acr=rand.choice(posswords)
        return acr
    else:
        for word in Poe.words:
            if word.word[0]==sideletter:
                posswords.append(word)
        acr=rand.choice(posswords)
        return acr

def line(lines, ts, rhyme, firstword=None, sideletter=None):
    global f
    varl=[1,2,2,2,2,3]
    var=rand.choice(varl)
    line = []
    sc = 0
    if firstword is not None:
        if sideletter is not None:
            line.append(findacr(sideletter, firstword.afterprobs))
        else:
            line.append(rand.choice(firstword.afterprobs))
    else:
        if sideletter is not None:
            line.append(findacr(sideletter,findlastword(lines).afterprobs))
        else:
            line.append(rand.choice(findlastword(lines).afterprobs))
    sc = sc + line[0].syllables
    while sc < ts-var:
        p=rand.randint(1,f)
        if p<f:
            possword = rand.choice(line[-1].afterprobs)
        else:
            possword= rand.choice(curtop.topic.afterprobs)
        if sc + possword.syllables > ts:
            pass
        elif sc + possword.syllables == ts and (possword not in rhyme.rhymes or rhyme.word == "xyz"):
            pass
        else:
            line.append(possword)
            sc = sc + line[-1].syllables

    line, sc = findlast(line, sc, ts, rhyme)
    if sc < ts:
        posswords = []
        for word in Poe.words:
            if word.syllables == ts-sc and (word.word in rhyme.rhymes or (rhyme.word == "xyz" and len(word.rhymes)>0)):
                for b in range(word.freq):
                    posswords.append(word)
        line.append(rand.choice(posswords))
        sc = sc + line[-1].syllables
    return line

def stanzaprettyprint(poem, pr):
    string=""
    if pr:
        print(" ")
    punc = [",",",",",",",",",",",",";",";",".",":","-"," "," ","!","...",",",","]
    endpunc = [". ",". ",". ",". ",". ","! ","... "]
    for stanza in poem:
        n=1
        for line in stanza:
            pline=[]
            for word in line:
                if word.word == "i":
                    pline.append("I")
                else:
                    pline.append(word.word)
            if n==len(stanza):
                string = string + capwords(pline[0]) + " " + " ".join(word for word in pline[1:]) + rand.choice(endpunc)
                if pr:
                    print(capwords(pline[0]) + " " + " ".join(word for word in pline[1:]) + rand.choice(endpunc))
            else:
                string = string + capwords(pline[0]) + " " + " ".join(word for word in pline[1:]) + rand.choice(punc)
                if pr:
                    print(capwords(pline[0]) + " " + " ".join(word for word in pline[1:]) + rand.choice(punc))
            n+=1
        if pr:
                print(" ")
    if pr:
        print(" ")
    return string
    
def freeprettyprint(poem, pr):
    punc = [",",",",",",",",",",",",";",";",".",":","-"," "," ","!","...",",",","]
    endpunc = [". ",". ",". ",". ",". ","! ","... "]
    string = ""
    n=1
    if pr:
        print(" ")
    for line in poem:
        pline=[]
        for word in line:
            if word.word == "i":
                pline.append("I")
            else:
                pline.append(word.word)
        if n==len(poem):
            string = string + capwords(pline[0]) + " " + " ".join(word for word in pline[1:]) + rand.choice(endpunc)
            if pr:
                print(capwords(pline[0]) + " " + " ".join(word for word in pline[1:]) + rand.choice(endpunc))
        else:
            string = string + capwords(pline[0]) + " " + " ".join(word for word in pline[1:]) + rand.choice(punc)
            if pr:
                print(capwords(pline[0]) + " " + " ".join(word for word in pline[1:]) + rand.choice(punc))
        n+=1
    if pr:
        print(" ")
    return string

def monotetraprettyprint(poem, pr):
    punc = [",",",",",",",",",",",",";",";",".",":","-"," "," ","!","...",",",","]
    endpunc = [". ",". ",". ",". " ,". ","! ","... "]
    string = ""
    if pr:
        print(" ")
    for stanza in poem:
        n=1
        for line in stanza:
            if n<4:
                pline=[]
                for word in line:
                    if word.word == "i":
                        pline.append("I")
                    else:
                        pline.append(word.word)
                string = string + capwords(pline[0]) + " " + " ".join(word for word in pline[1:]) + rand.choice(punc)
                if pr:
                    print(capwords(pline[0]) + " " + " ".join(word for word in pline[1:]) + rand.choice(punc))
            elif n==4:
                pline=[]
                for word in line:
                    if word.word == "i":
                        pline.append("I")
                    else:
                        pline.append(word.word)
                string = string + capwords(pline[0]) + " " + " ".join(word for word in pline[1:]) + ", " + " ".join(word for word in pline) + rand.choice(endpunc)
                if pr:
                    print(capwords(pline[0]) + " " + " ".join(word for word in pline[1:]) + ", " + " ".join(word for word in pline) + rand.choice(endpunc))
            n+=1
        if pr:
                print(" ")
    if pr:
        print(" ")
    return string

def menu(Poe):
    print("Please enter the number of one of the following options:")
    print("1. Train")
    print("2. Exit game")
    if Poe.trained == True:
        print("3. Write poem")
    choice_c = False
    while not choice_c:
        choice = int(input(""))
        if not ((choice!=1 and choice!=2) and Poe.trained == False):
            choice_c = True
            if choice == 1:
                textmenu()
            elif choice == 2:
                print("Thanks for playing.")
                return False
            elif choice == 3:
                poemmenu()
        else:
            pass
    return True

def poemmenu():
    b=True
    print("Please enter the number of one of the following poems:")
    print("1. Write Shakespearean sonnet")
    print("2. Write Petrarchan sonnet")
    print("3. Write curtal sonnet")
    print("4. Write haiku")
    print("5. Write limerick")
    print("6. Write rhyming stanzas")
    print("7. Write non-rhyming stanzas")
    print("8. Write couplet")
    print("9. Write multiple couplets")
    print("10. Write cinquin")
    print("11. Write monotetra")
    print("12. Write clogyrnach")
    print("13. Write dodoitsu")
    print("14. Write rhyming acrostic")
    print("15. Write non-rhyming acrostic")
    print("16. Write free verse")
    print("17. Return to menu")
    cdone = False
    while not cdone:
        choice = int(input())
        cdone=True
        if choice >=1 and choice <=16:
            global curtop
            curtop,firstword=firstwordfind()
        if choice == 1:
            stanzaprettyprint(shakespeareansonnet(firstword),b)
        elif choice == 2:
            stanzaprettyprint(petrarchansonnet(firstword),b)
        elif choice == 3:
            stanzaprettyprint(curtalsonnet(firstword),b)
        elif choice == 4:
            freeprettyprint(haiku(firstword),b)
        elif choice == 5:
            freeprettyprint(limerick(firstword),b)
        elif choice == 6:
            stanzaprettyprint(rhymingstanzas(firstword),b)
        elif choice == 7:
            stanzaprettyprint(rhymelessstanzas(firstword),b)
        elif choice == 8:
            freeprettyprint(couplet(firstword),b)
        elif choice == 9:
            stanzaprettyprint(multicouplets(firstword),b)
        elif choice == 10:
            freeprettyprint(cinquain(firstword),b)
        elif choice == 11:
            monotetraprettyprint(monotetra(firstword),b)
        elif choice == 12:
            freeprettyprint(clogyrnach(firstword),b)
        elif choice == 13:
            freeprettyprint(dodoitsu(firstword),b)
        elif choice == 14:
            freeprettyprint(rhymingacrostic(firstword),b)
        elif choice == 15:
            freeprettyprint(nonrhymingacrostic(firstword),b)
        elif choice == 16:
            freeprettyprint(freeverse(firstword),b)
        elif choice == 17:
            pass
        else:
            cdone=False
        

def textmenu():
    print("Please enter the number of one of the following texts:")
    n = 1
    for text in texts:
        print("{}. {}".format(n, text.name))
        n+=1
    print("{}. Return to menu".format(n))
    choice_c = False
    while not choice_c:
        choice = int(input(""))
        if not choice >= n:
            if not texts[choice-1].read:
                choice_c = True
                train(choice, texts)
                n+=1
            else:
                print("This text has already been read. Please choose another text.")
        else:
            if choice==n:
                choice_c = True

Poe = Poeclass()
Game = Game()

game = True

while game:
    game = menu(Poe)

Fetching token
?