2012-10-20 7 views
5

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 

Antwort

3

Sie benötigen

sentence = [] 

Zurück in die

elif newword in stopsentence: 

Klausel zu kopieren.

So

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 

bearbeiten

Hier ist eine Lösung, ohne die äußere Schleife.

""" 
    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

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? –

+0

Ich habe eine separate Lösung hinzugefügt, die die äußere Schleife nicht benötigt. – grieve

+0

Ich habe Python 3 momentan nicht installiert, daher müssen Sie möglicherweise die zweite Lösung für die Syntax optimieren. – grieve

1

Verstehen Sie diesen Code? Ich wette, Sie können das Bit finden, das den Satz druckt, und ändern Sie es, um mehrere Sätze zusammen ohne Rückkehr zu drucken. Sie könnten eine weitere while-Schleife um das Satzbit hinzufügen, um mehrere Absätze zu erhalten.

Syntax Hinweis:

print 'hello' 
print 'there' 
hello 
there 

print 'hello', 
print 'there' 
hello there 

print 'hello', 
print 
print 'there' 

Der Punkt ist, dass ein Komma am Ende eines Drucks Anweisung am Ende der Leitung die Rückkehr verhindert, und eine leere print-Anweisung druckt eine Rückkehr.

+0

Ja, ich folge. Das Problem ist, dass alles, was ich mit der 'print'-Anweisung versucht habe, nicht dazu beigetragen hat, die Sätze in Absätze zu fassen (es sei denn, du zählst, _all_ Zeilenumbrüche herauszunehmen und einen riesigen Absatz zu machen). Die "while" -Schleife ist das, woran ich dachte, aber ich war nicht ganz sicher, wie ich den Satzteil umhüllen sollte. Alles, was ich versuchte, führte zu verschiedenen Fehlern, also dachte ich, ich würde die Experten fragen. Was ist der beste Weg, um es zu sagen "generieren x (1-5 zum Beispiel) Menge von Sätzen, dann fügen Sie einen Zeilenumbruch, und wiederholen Sie dann, bis" maxsentents "erreicht ist"? –

Verwandte Themen