samedi 6 mai 2023

Abecedarian program on Python

I am trying to create a little program that uses a statistical approach to generate Abecedarian poems ( a poem in which the first letter of each line or stanza follows sequentially through the alphabet), but I tried two different approaches and neither seem to work. The first one just runs forever and doesn't yield any results; the second gives me an Index Error that reads "# raises IndexError if seq is empty" and "list index out of range".

The poem does not need to rhyme or have lines of any particular length.

These are the two options I have tried:

def generateAbecedarian():

  alphabet = ['a','b','c','d','e','f',
            'g','h','i','j','k','l',
            'm','n','o','p','q','r',
            's','t','u','v','w','x',
            'y','z']
        
  for i in range(1):
    line_i = model.make_sentence()
    line_i_list = line_i.split(' ')
    first_word_i = line_i_list[0]
    first_letter_i = list(first_word_i)[0]
    while first_letter_i != alphabet[i]:
      line_i = model.make_sentence()
    else:
      print(line_i)  

This first one runs and runs without ever giving any output. This is the second option I have tried:

def generateAbecedarian2():

  alphabet = ['a','b','c','d','e','f',
            'g','h','i','j','k','l',
            'm','n','o','p','q','r',
            's','t','u','v','w','x',
            'y','z']
        
  word_list_i = []      
  for i in range(26):
    word_list_i = [w
                   for w in corpus
                   if list(sentence)[0] == alphabet[i]
                  ]
    random_first_word_i = random.choice(word_list_i)
    line_i = model.make_sentence_with_start(random_first_word_i)
    print(line_i)

This one gives me the index error.

In both of these I make use of random, markovify, and a predefined corpus, as well as a Bigram model I have set up previously.




Aucun commentaire:

Enregistrer un commentaire