我该如何进一步随机化这个文本生成器?

0 投票
1 回答
569 浏览
提问于 2025-04-16 03:27

我正在制作一个随机文本生成器——不使用马尔可夫链——目前它运行得还不错,能够生成我认为的很多随机句子,但我想让它更准确,尽量减少句子的重复。首先,这是我的代码流程:

  1. 输入一个句子作为起始句子——这个叫做触发字符串,赋值给一个变量。

  2. 找出触发字符串中最长的单词。

  3. 在古腾堡项目的数据库中搜索包含这个单词的句子——不管是大写还是小写。

  4. 返回包含我在第三步提到的单词的最长句子。

  5. 把第一步和第四步得到的句子合并在一起。

  6. 把第四步的句子作为新的“触发”句子,然后重复这个过程。注意,我需要在第二个句子中找出最长的单词,然后继续这样做。

这是我的代码:

import nltk

from nltk.corpus import gutenberg

from random import choice

import smtplib #will be for send e-mail option later

triggerSentence = raw_input("Please enter the trigger sentence: ")#get input str

longestLength = 0

longestString = ""

longestLen2 = 0

longestStr2 = ""

listOfSents = gutenberg.sents() #all sentences of gutenberg are assigned -list of list format-

listOfWords = gutenberg.words()# all words in gutenberg books -list format-

while triggerSentence:#run the loop so long as there is a trigger sentence
    sets = []
    sets2 = []
    split_str = triggerSentence.split()#split the sentence into words

    #code to find the longest word in the trigger sentence input
    for piece in split_str:
        if len(piece) > longestLength:
            longestString = piece
            longestLength = len(piece)





    #code to get the sentences containing the longest word, then selecting
    #random one of these sentences that are longer than 40 characters

    for sentence in listOfSents:
        if sentence.count(longestString):
            sents= " ".join(sentence)
            if len(sents) > 40:
                sets.append(" ".join(sentence))


    triggerSentence = choice(sets)
    print triggerSentence #the first sentence that comes up after I enter input-
    split_str = triggerSentence.split()

    for apiece in triggerSentence: #find the longest word in this new sentence
        if len(apiece) > longestLen2:
            longestStr2 = piece
            longestLen2 = len(apiece)
    if longestStr2 == longestString:
        second_longest = sorted(split_str, key=len)[-2]#this should return the second longest word in the sentence in case it's longest word is as same as the longest word of last sentence
    #print second_longest #now get second longest word if first is same
            #as longest word in previous sentence

        for sentence in listOfSents:
            if sentence.count(second_longest):
                sents = " ".join(sentence)
                if len(sents) > 40:
                    sets2.append(" ".join(sentence))
        triggerSentence = choice(sets2)
    else:
        for sentence in listOfSents:
            if sentence.count(longestStr2):
                sents = " ".join(sentence)
                if len(sents) > 40:
                sets.append(" ".join(sentence))
        triggerSentence = choice(sets)


    print triggerSentence

根据我的代码,一旦我输入一个触发句子,我应该得到另一个包含我输入的触发句子中最长单词的句子。然后这个新句子就变成了触发句子,接着再找出它的最长单词。这时候有时会出现问题。我观察到,尽管我在第47行到最后一行放置了代码,但算法仍然可能选择到相同的最长单词,而没有去寻找第二长的单词。

举个例子:

触发字符串 = "苏格兰是个不错的地方。"

句子1 = -这是一个包含苏格兰这个词的随机句子-

现在,这就是我代码中可能出现问题的地方——不管是出现在句子2、942,还是其他任何地方,我这里以句子2为例。

句子2 = 另一个包含苏格兰这个词的句子,但不是句子1中的第二长单词。根据我的代码,这个句子应该是包含句子1中第二长单词的某个句子,而不是苏格兰!

我该如何解决这个问题?我正在尽量优化代码,任何帮助都欢迎。

1 个回答

0

你的算法一点也不随机,它应该是确定的,也就是说每次运行结果都是一样的。

我不太明白你想要做什么。如果你是想生成随机单词,可以直接用字典和随机模块。如果你想从古腾堡计划中随机获取句子,可以先用随机模块选择一部作品,然后再从那部作品中选一句话。

撰写回答