Ich möchte das folgende Skript ändern, so dass es Absätze aus einer zufälligen Anzahl von Sätzen erstellt, die vom Skript generiert werden. Mit anderen Worten, verketten Sie eine zufällige Zahl (wie 1-5) von Sätzen, bevor Sie eine neue Zeile hinzufügen.Wie erstellt man Absätze aus der Markov-Kette?
Das Skript funktioniert gut wie es ist, aber die Ausgabe besteht aus kurzen Sätzen, die durch einen Zeilenumbruch getrennt sind. Ich möchte einige Sätze in Absätze zusammenfassen.
Irgendwelche Ideen zu Best Practices? Vielen Dank.
"""
from: http://code.activestate.com/recipes/194364-the-markov-chain-algorithm/?in=lang-python
"""
import random;
import sys;
stopword = "\n" # Since we split on whitespace, this can never be a word
stopsentence = (".", "!", "?",) # Cause a "new sentence" if found at the end of a word
sentencesep = "\n" #String used to seperate sentences
# GENERATE TABLE
w1 = stopword
w2 = stopword
table = {}
for line in sys.stdin:
for word in line.split():
if word[-1] in stopsentence:
table.setdefault((w1, w2), []).append(word[0:-1])
w1, w2 = w2, word[0:-1]
word = word[-1]
table.setdefault((w1, w2), []).append(word)
w1, w2 = w2, word
# Mark the end of the file
table.setdefault((w1, w2), []).append(stopword)
# GENERATE SENTENCE OUTPUT
maxsentences = 20
w1 = stopword
w2 = stopword
sentencecount = 0
sentence = []
while sentencecount < maxsentences:
newword = random.choice(table[(w1, w2)])
if newword == stopword: sys.exit()
if newword in stopsentence:
print ("%s%s%s" % (" ".join(sentence), newword, sentencesep))
sentence = []
sentencecount += 1
else:
sentence.append(newword)
w1, w2 = w2, newword
EDIT 01:
Okay, ich habe zusammen einen einfachen „Absatz-Wrapper“, gepflasterten, die gut die Sätze in den Absätze zu sammeln funktionieren, aber es verwirrte mit dem Ausgang der Satz-Generator - Ich bekomme übermäßige Wiederholung der ersten Wörter, zum Beispiel, unter anderem.
Aber die Prämisse ist gesund; Ich muss nur herausfinden, warum die Funktionalität der Satzschleife durch das Hinzufügen der Absatzschleife beeinflusst wurde. Bitte informieren Sie, wenn Sie das Problem sehen:
###
# usage: $ python markov_sentences.py <input.txt> output.txt
# from: http://code.activestate.com/recipes/194364-the-markov-chain-algorithm/?in=lang-python
###
import random;
import sys;
stopword = "\n" # Since we split on whitespace, this can never be a word
stopsentence = (".", "!", "?",) # Cause a "new sentence" if found at the end of a word
paragraphsep = "\n\n" #String used to seperate sentences
# GENERATE TABLE
w1 = stopword
w2 = stopword
table = {}
for line in sys.stdin:
for word in line.split():
if word[-1] in stopsentence:
table.setdefault((w1, w2), []).append(word[0:-1])
w1, w2 = w2, word[0:-1]
word = word[-1]
table.setdefault((w1, w2), []).append(word)
w1, w2 = w2, word
# Mark the end of the file
table.setdefault((w1, w2), []).append(stopword)
# GENERATE PARAGRAPH OUTPUT
maxparagraphs = 10
paragraphs = 0 # reset the outer 'while' loop counter to zero
while paragraphs < maxparagraphs: # start outer loop, until maxparagraphs is reached
w1 = stopword
w2 = stopword
stopsentence = (".", "!", "?",)
sentence = []
sentencecount = 0 # reset the inner 'while' loop counter to zero
maxsentences = random.randrange(1,5) # random sentences per paragraph
while sentencecount < maxsentences: # start inner loop, until maxsentences is reached
newword = random.choice(table[(w1, w2)]) # random word from word table
if newword == stopword: sys.exit()
elif newword in stopsentence:
print ("%s%s" % (" ".join(sentence), newword), end=" ")
sentencecount += 1 # increment the sentence counter
else:
sentence.append(newword)
w1, w2 = w2, newword
print (paragraphsep) # newline space
paragraphs = paragraphs + 1 # increment the paragraph counter
# EOF
EDIT 02:
Added sentence = []
gemäß Antwort unten in elif
Aussage. Nämlich;
elif newword in stopsentence:
print ("%s%s" % (" ".join(sentence), newword), end=" ")
sentence = [] # I have to be here to make the new sentence start as an empty list!!!
sentencecount += 1 # increment the sentence counter
EDIT 03:
Dies ist die letzte Iteration dieses Skripts. Danke für die Hilfe beim Aussortieren. Ich hoffe, dass andere damit Spaß haben können, ich weiß, dass ich es werde. ;)
Zu Ihrer Information: Es gibt ein kleines Artefakt - es gibt ein zusätzliches Ende des Absatzes, das Sie aufräumen sollten, wenn Sie dieses Skript verwenden. Aber, abgesehen davon, eine perfekte Implementierung der Markov-Chain-Text-Generierung.
###
# usage: python markov_sentences.py <input.txt> output.txt
# from: http://code.activestate.com/recipes/194364-the-markov-chain-algorithm/?in=lang-python
###
import random;
import sys;
stopword = "\n" # Since we split on whitespace, this can never be a word
stopsentence = (".", "!", "?",) # Cause a "new sentence" if found at the end of a word
sentencesep = "\n" #String used to seperate sentences
# GENERATE TABLE
w1 = stopword
w2 = stopword
table = {}
for line in sys.stdin:
for word in line.split():
if word[-1] in stopsentence:
table.setdefault((w1, w2), []).append(word[0:-1])
w1, w2 = w2, word[0:-1]
word = word[-1]
table.setdefault((w1, w2), []).append(word)
w1, w2 = w2, word
# Mark the end of the file
table.setdefault((w1, w2), []).append(stopword)
# GENERATE SENTENCE OUTPUT
maxsentences = 20
w1 = stopword
w2 = stopword
sentencecount = 0
sentence = []
paragraphsep = "\n"
count = random.randrange(1,5)
while sentencecount < maxsentences:
newword = random.choice(table[(w1, w2)]) # random word from word table
if newword == stopword: sys.exit()
if newword in stopsentence:
print ("%s%s" % (" ".join(sentence), newword), end=" ")
sentence = []
sentencecount += 1 # increment the sentence counter
count -= 1
if count == 0:
count = random.randrange(1,5)
print (paragraphsep) # newline space
else:
sentence.append(newword)
w1, w2 = w2, newword
# EOF
Hoppla! Ja, ich muss es irgendwann herausgerissen und vergessen haben, es wieder einzusetzen. Danke für die Einsicht! Das hat den Trick gemacht - fast. Es scheint, als ob die Satzschleife die gleichen Startwörter für jeden Satz wieder verwendet. Irgendwelche Ideen, wie man die ersten Wörter, die es für die Satzgenerierung wählt, verwechselt? –
Ich habe eine separate Lösung hinzugefügt, die die äußere Schleife nicht benötigt. – grieve
Ich habe Python 3 momentan nicht installiert, daher müssen Sie möglicherweise die zweite Lösung für die Syntax optimieren. – grieve