Markov chain text generator: Difference between revisions
Content deleted Content added
→{{header|REXX}}: optimized the building of the prefix table. |
Edited my Python to conform to standards a bit better. |
||
Line 842:
import random
#Read a file and return its contents.▼
def readdata(file):
with open(file) as f:
contents = f.read()
return contents
#Make a markov rule for given data.▼
def makerule(data, context):
rule = {}
words = data.split(' ')
Line 864 ⟶ 866:
return rule
#Use a markov rule to create a string.▼
def makestring(rule, length):
oldwords = random.choice(list(rule.keys())).split(' ') #random starting words
string = ' '.join(oldwords) + ' '
Line 883 ⟶ 886:
return string
if __name__ == '__main__':
string = makestring(rule, int(sys.argv[3]))
print(string)</lang>
{{out}}
|