我正在研究一个随机文本生成器 - 不使用马尔可夫链 - 目前它没有太多问题 - 实际上按照我的标准生成了大量随机句子,但我想让它更加准确以防止尽可能多的句子重复尽可能-。首先,这是我的代码流程:
输入一个句子作为输入-这称为触发字符串,分配给一个变量-
获取触发字符串中最长的单词
在所有 Project Gutenberg 数据库中搜索包含此单词的句子 - 不管大写小写 -
返回包含我在步骤 3 中谈到的单词的最长句子
将步骤 1 和步骤 4 中的句子附加在一起
将步骤 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 中给出了它,例如,为了——
句子 2 = 另一个句子中包含单词 Scotland 但不是句子 1 中第二长的单词。根据我的代码,这句话应该是包含句子 1 中第二长单词的句子,而不是 Scotland!
我该如何解决这个问题?我正在尝试尽可能优化代码,欢迎提供任何帮助。