【问题标题】:How can I randomize this text generator even further?我怎样才能进一步随机化这个文本生成器?
【发布时间】:2010-08-29 21:50:32
【问题描述】:

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

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

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

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

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

  5. 将Step 1和Step4中的句子附加在一起

  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 句或无数个或其他任何东西中,但我在 sent.2 中给出了它,例如,为了——

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

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

【问题讨论】:

  • 开头的所有换行符是怎么回事?
  • @Zonda333,哦,如果你的意思是为什么在开始时代码之间总是有一个空行-在 import nltk 等等-,我是故意这样做的。在复制/粘贴代码期间,行往往会混淆,我可能在行之间按下了太多的回车按钮。
  • [process] 标签没有添加任何有用的信息。 [random] 标签会更合适。

标签: python text nltk


【解决方案1】:

您的算法完全没有随机性。它应该始终是确定性的。

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

【讨论】:

    猜你喜欢
    • 2023-02-09
    • 1970-01-01
    • 2019-09-09
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多