मैं नीचे लिपि को संशोधित करना चाहता हूं ताकि यह स्क्रिप्ट द्वारा उत्पन्न वाक्यों की यादृच्छिक संख्या से पैराग्राफ तैयार कर सके। दूसरे शब्दों में, एक नई लाइन जोड़ने से पहले वाक्यों की यादृच्छिक संख्या (जैसे 1-5) को संयोजित करें।मार्कोव चेन आउटपुट से पैराग्राफ कैसे बनाएं?
स्क्रिप्ट ठीक काम करता है, लेकिन आउटपुट एक छोटी सी रेखा से अलग वाक्य को अलग करता है। मैं पैराग्राफ में कुछ वाक्यों को इकट्ठा करना चाहता हूं।
सर्वोत्तम प्रथाओं पर कोई विचार? धन्यवाद।
"""
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
संपादित करें 01:
ठीक है, मैं एक साथ एक सरल "पैरा आवरण," जो अच्छी तरह से काम पैराग्राफ में वाक्य इकट्ठा करने के लिए पत्थर की है, लेकिन यह के उत्पादन के साथ गड़बड़ वाक्य जनरेटर - मुझे पहले शब्दों की अत्यधिक दोहराव मिल रही है, उदाहरण के लिए, अन्य मुद्दों के बीच।
लेकिन आधार ध्वनि है; मुझे बस यह पता लगाने की आवश्यकता है कि अनुच्छेद लूप के अतिरिक्त वाक्य लूप की कार्यक्षमता क्यों प्रभावित हुई थी। कृपया सलाह आप इस समस्या को देख सकते हैं:
###
# 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
संपादित करें 02:
जोड़ा गया sentence = []
प्रति जवाब के रूप में elif
बयान में नीचे। अर्थात;
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
संपादित करें 03:
यह इस स्क्रिप्ट के अंतिम यात्रा है। इसे हल करने में मदद के लिए शोक करने के लिए धन्यवाद। मुझे उम्मीद है कि दूसरों के साथ कुछ मज़ा आएगा, मुझे पता है कि मैं करूँगा। ;)
एफवाईआई: एक छोटा सा आर्टिफैक्ट है - यदि आप इस स्क्रिप्ट का उपयोग करते हैं तो आप एक अतिरिक्त अंत-पैराग्राफ स्पेस साफ़ कर सकते हैं। लेकिन, इसके अलावा, मार्कोव चेन टेक्स्ट पीढ़ी का एकदम सही कार्यान्वयन।
###
# 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
ओह! हाँ, मुझे लगता है कि कुछ बिंदु पर बाहर yanked और अंतर्दृष्टि के लिए इसे वापस डाल करने के लिए। धन्यवाद भूल गए हैं चाहिए! यह चाल है - लगभग। ऐसा लगता है जैसे वाक्य लूप प्रत्येक वाक्य के लिए एक ही प्रारंभिक शब्द का पुन: उपयोग करता है। वाक्य पीढ़ी के लिए पहले शब्दों को कैसे मिलाएं इस पर कोई विचार है? –
मैं एक अलग समाधान है कि बाहरी पाश की जरूरत नहीं है गयी। – grieve
मैं अजगर 3 पल में स्थापित है, तो आप वाक्य रचना के लिए दूसरा समाधान बदलाव करने की आवश्यकता हो सकती है। – grieve