<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def generate(topic, add_punctuation, character_count=None):
 """ Generate the text for a given topic """

    corpus_cursor = db.markovify.find({'topic': topic})
    if(corpus_cursor):
        corpus = ''
        for text in corpus_cursor:
            corpus = punctuate(corpus, text['text'], add_punctuation)
        text_model = markovify.Text(corpus)
        sentence = text_model.make_short_sentence(character_count) \
            if character_count else text_model.make_sentence()
        if not sentence:
            raise Exception('There is not enough in the corpus to generate a sentence.')
        return sentence
    raise Exception('No text found for topic: ' + topic)