我怎样才能进一步随机化这个文本生成器?

发布于 2024-09-17 07:32:19 字数 3347 浏览 8 评论 0原文

我正在研究一个随机文本生成器 - 不使用马尔可夫链 - 目前它的工作没有太多问题 - 实际上根据我的标准生成了大量的随机句子,但我想让它更加准确以防止尽可能多的句子重复尽可能-。首先,这是我的代码流程:

  1. 输入一个句子作为输入 - 这称为触发字符串,分配给一个变量 -

  2. 获取触发字符串中最长的单词

  3. 在所有 Project Gutenberg 数据库中搜索包含该单词的句子 - 无论大小写 -

  4. 返回包含我在步骤 3 中谈到的单词的最长句子

  5. 附加将步骤 1 和步骤 4 中的句子放在一起

  6. ​​

    将步骤 4 中的句子指定为新的“触发”句子并重复该过程。请注意,我必须获取第二句中最长的单词并继续这样,依此类推 -

这是我的代码:

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 或无数或其他什么中,但是我在 send.2 中给出它,例如,

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

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

I'm working on a random text generator -without using Markov chains- and currently it works without too many problems -actually generates a good amount of random sentences by my criteria but I want to make it even more accurate to prevent as many sentence repeats as possible-. Firstly, here is my code flow:

  1. Enter a sentence as input -this is called trigger string, is assigned to a variable-

  2. Get longest word in trigger string

  3. Search all Project Gutenberg database for sentences that contain this word -regardless of uppercase lowercase-

  4. Return the longest sentence that has the word I spoke about in step 3

  5. Append the sentence in Step 1 and Step4 together

  6. Assign the sentence in Step 4 as the new 'trigger' sentence and repeat the process. Note that I have to get the longest word in second sentence and continue like that and so on-

And here is my code:

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

According to my code, once I enter a trigger sentence, I should get another one that contains the longest word of the trigger sentence I entered. Then this new sentence becomes the trigger sentence and it's longest word is picked. This is where the problem sometimes occurs. I observed that despite the code lines I placed - starting from line 47 to the end- , the algorithm still can pick the same longest word in the sentences that come along, not looking for the second longest word.

For example:

Trigger string = "Scotland is a nice place."

Sentence 1 = -This is a random sentence with the word Scotland in it-

Now, this is where the problem can occur in my code at times -doesn't matter whether it comes up in sentence 2 or 942 or zillion or whatever, but I give it in sent.2 for example's sake-

Sentence 2 = Another sentence that has the word Scotland in it but not the second longest word in sentence 1. According to my code, this sentence should have been some sentence that contained the second longest word in sentence 1, not Scotland !

How can I solve this ? I'm trying to optimize the code as much as possible and any help is welcome.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

请你别敷衍 2024-09-24 07:32:19

你的算法根本没有任何随机性。它应该始终是确定性的。

我不太确定你想在这里做什么。如果要生成随机单词,只需使用字典和 random 模块即可。如果您想从古腾堡项目中获取随机句子,请使用随机模块选择一个作品,然后从该作品中选取一个句子。

There is nothing random about your algorithm at all. It should always be deterministic.

I'm not quite sure what you want to do here. If it is to generate random words, just use a dictionary and the random module. If you want to grab random sentences from the Gutenberg project, use the random module to pick a work and then a sentence out of that work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文