2012-10-20 12 views
5

मैं नीचे लिपि को संशोधित करना चाहता हूं ताकि यह स्क्रिप्ट द्वारा उत्पन्न वाक्यों की यादृच्छिक संख्या से पैराग्राफ तैयार कर सके। दूसरे शब्दों में, एक नई लाइन जोड़ने से पहले वाक्यों की यादृच्छिक संख्या (जैसे 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 

उत्तर

3

आप

elif newword in stopsentence: 

खंड में कॉपी करने के लिए

sentence = [] 

वापस की जरूरत है।

तो

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=" ") 
      sentence = [] # I have to be here to make the new sentence start as an empty list!!! 
      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 

संपादित

यहाँ बाहरी पाश का उपयोग किए बिना एक समाधान है।

""" 
    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\n" 
count = random.randrange(1,5) 

while sentencecount < maxsentences: 
    newword = random.choice(table[(w1, w2)]) 
    if newword == stopword: sys.exit() 
    if newword in stopsentence: 
     print ("%s%s" % (" ".join(sentence), newword), end=" ") 
     sentence = [] 
     sentencecount += 1 
     count -= 1 
     if count == 0: 
      count = random.randrange(1,5) 
      print (paragraphsep) 
    else: 
     sentence.append(newword) 
    w1, w2 = w2, newword 
+0

ओह! हाँ, मुझे लगता है कि कुछ बिंदु पर बाहर yanked और अंतर्दृष्टि के लिए इसे वापस डाल करने के लिए। धन्यवाद भूल गए हैं चाहिए! यह चाल है - लगभग। ऐसा लगता है जैसे वाक्य लूप प्रत्येक वाक्य के लिए एक ही प्रारंभिक शब्द का पुन: उपयोग करता है। वाक्य पीढ़ी के लिए पहले शब्दों को कैसे मिलाएं इस पर कोई विचार है? –

+0

मैं एक अलग समाधान है कि बाहरी पाश की जरूरत नहीं है गयी। – grieve

+0

मैं अजगर 3 पल में स्थापित है, तो आप वाक्य रचना के लिए दूसरा समाधान बदलाव करने की आवश्यकता हो सकती है। – grieve

1

क्या आप इस कोड को समझते हैं? मैं शर्त लगाता हूं कि आप उस वाक्य को प्रिंट कर सकते हैं जो वाक्य को प्रिंट कर रहा है, और रिटर्न के बिना, कई वाक्यों को एक साथ प्रिंट करने के लिए इसे बदल सकता है। आप एकाधिक पैराग्राफ प्राप्त करने के लिए वाक्य बिट के चारों ओर लूप जबकि एक और जोड़ सकते हैं।

सिंटेक्स संकेत:

print 'hello' 
print 'there' 
hello 
there 

print 'hello', 
print 'there' 
hello there 

print 'hello', 
print 
print 'there' 

मुद्दा यह है कि एक प्रिंट बयान के अंत में एक अल्पविराम पंक्ति के अंत में वापसी से बचाता है, और एक खाली प्रिंट बयान एक वापसी प्रिंट है।

+0

हाँ, मैं का पालन करें। मुसीबत, सब कुछ मैं 'print' बयान के साथ की कोशिश की (एक विशाल पैरा बनाने जब तक आप _all_ पंक्ति विराम बाहर ले गिनती,) पैराग्राफ में वाक्य इकट्ठा करने के लिए मदद नहीं की है। 'While' पाश मैं क्या करने की सोच रहा था, लेकिन मैं काफी यकीन है कि वाक्य हिस्सा रैप करने के लिए कैसे नहीं था। मैंने जो कुछ भी कोशिश की, वह विभिन्न त्रुटियों का कारण बन गई, इसलिए मुझे लगा कि मैं विशेषज्ञों से पूछूंगा। इसे बताने का सबसे अच्छा तरीका क्या है "एक्स (उदाहरण के लिए 1-5) उदाहरण की मात्रा उत्पन्न करें, फिर एक लाइन ब्रेक डालें, और फिर 'maxsentences' तक पहुंचने तक दोहराएं"? –

संबंधित मुद्दे