Chciałbym zmodyfikować poniższy skrypt, aby tworzył akapity z losowej liczby zdań generowanych przez skrypt. Innymi słowy, przed dodaniem nowego wiersza połącz losową liczbę (np. 1-5) zdań.Jak tworzyć akapity z wyjścia łańcucha Markowa?
Skrypt działa poprawnie tak jak jest, ale wyniki są krótkimi zdaniami oddzielonymi znakiem nowej linii. Chciałbym zebrać kilka zdań w akapity.
Wszelkie pomysły dotyczące najlepszych praktyk? Dzięki.
"""
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:
Ok, mam sklecony prostą „paragraf opakowanie”, który działa dobrze zebrać zdania w akapity, ale to pomieszane ze wyjściu generator zdań - uzyskuję nadmierną powtarzalność pierwszych słów, na przykład, między innymi.
Ale przesłanką jest dźwięk; Po prostu muszę dowiedzieć się, dlaczego na funkcjonalność pętli zdań wpłynęło dodanie pętli akapitu. Proszę doradzić, jeśli widzisz problem:
###
# 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:
Dodany sentence = []
jak za odpowiedź poniżej w elif
oświadczeniu. Na dowcip;
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:
To jest ostatni iteracja tego skryptu. Dzięki żal za pomoc w posortowaniu tego. Mam nadzieję, że inni mogą się z tym dobrze bawić, wiem, że to zrobię. ;)
FYI: Istnieje jeden mały artefakt - istnieje dodatkowa przestrzeń na końcu akapitu, którą możesz wyczyścić, jeśli używasz tego skryptu. Ale, poza tym, doskonała implementacja generowania tekstu łańcucha Markowa.
###
# 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
Ups! Tak, musiałem to w pewnym momencie wyciągnąć i zapomniałem włożyć to z powrotem. Dzięki za wgląd! To wystarczyło - prawie. Wydaje się, że pętla zdań ponownie używa tych samych słów początkowych dla każdego zdania. Jakieś pomysły na pomieszanie pierwszych słów, które wybiera do generowania zdań? –
Dodałem osobne rozwiązanie, które nie wymaga zewnętrznej pętli. – grieve
Nie mam zainstalowanego obecnie Pythona 3, więc może być konieczne zmodyfikowanie drugiego rozwiązania pod kątem składni. – grieve