Markov chain text generator: Difference between revisions

→‎Functional Python: pylinted for Python 3. Updated output.
m (→‎{{header|Perl}}: code improvements and strict compliance)
(→‎Functional Python: pylinted for Python 3. Updated output.)
Line 1,035:
 
===Functional===
Defining an '''nGramsFromWords''' function in terms of itertools.starmapa generalized '''zipWithN''' wrapper).
(used with a generalized '''zipWithN''' wrapper).
 
{{Works with|Python|3.7}}
<lang python>from functools import (reduce)
<lang python>'''Markov chain text generation'''
from itertools import (starmap)
from random import (choice)
from textwrap import (fill)
 
from os.path import (expanduser)
from os import (getcwd)
 
from itertools import (starmap)
<lang python>from functools import (reduce)
from random import (choice)
from textwrap import (fill)
 
 
# markovText :: Dict -> [String] -> ([String] -> Bool) -> IO [String]
def markovText(dct):
'''nGram-hashed word dict -> opening words -> end condition -> text'''
'''
 
# nGram length
n = len(list(dct.keys())[0].split())
Line 1,064 ⟶ 1,066:
# markovRules :: Int -> [String] -> Dict
def markovRules(n):
'''All words in ordered list hashed by preceding nGrams of length n'''
preceding nGrams of length n.
'''
def nGramKey(dct, tpl):
k = ' '.join(list(tpl[:-1]))
Line 1,079 ⟶ 1,083:
# main :: IO ()
def main():
'''Text generation test.'''
 
nGramLength = 3
dctNGrams = markovRules(nGramLength)(
readFile(getcwd() + '/' + 'alice_oz.txt').split()
)
print (__doc__ + ':\n')
wordWrapped(75)print(
fill(
' '.join(
markovText(dctNGrams)(
anyNGramWithInitialCap(dctNGrams)
)(sentenceEndAfterMinWords(200))
),
width=75
)
)
Line 1,098 ⟶ 1,106:
# nGramsFromWords :: Int -> [String] -> [Tuple]
def nGramsFromWords(n):
'''List of nGrams, of length n, derived from ordered list of words ws'''
from ordered list of words ws.
'''
return lambda ws: zipWithN(lambda *xs: xs)(
map(lambda i: ws[i:], range(0, n))
Line 1,106 ⟶ 1,116:
# anyNGramWithInitialCap :: Dict -> [String]
def anyNGramWithInitialCap(dct):
'''Random pick from nGrams which start with capital letters'''
start with capital letters
'''
return choice(list(filter(
lambda k: 1 < len(k) and k[0].isupper() and k[1].islower(),
Line 1,115 ⟶ 1,127:
# sentenceEndAfterMinWords :: Int -> [String] -> Bool
def sentenceEndAfterMinWords(n):
'''Predicate :: Sentence punctuation after minimum word count'''
after minimum word count
'''
return lambda ws: n <= len(ws) and (
ws[-1][-1] in ['.', "'", '!', '?']
Line 1,125 ⟶ 1,139:
# readFile :: FilePath -> IO String
def readFile(fp):
'''The contents of any file at the path
with open(
derived by expanding any ~ in fp.'''
with open(expanduser(fp), 'r', encoding='utf-8') as f:
) as f:
return f.read()
 
Line 1,133 ⟶ 1,147:
# until :: (a -> Bool) -> (a -> a) -> a -> a
def until(p):
'''The result of repeatedly applying f until p holds.
The initial seed value is x.'''
def go(f, x):
v = x
Line 1,139 ⟶ 1,155:
return v
return lambda f: lambda x: go(f, x)
 
 
# wordWrapped :: Int -> [String] -> String
def wordWrapped(n):
return lambda s: fill(s, width=n)
 
 
# zipWithN :: (a -> b -> ... -> c) -> ([a], [b] ...) -> [c]
def zipWithN(f):
'''A new list constructed by the application of f
to each tuple in the zip of an arbitrary
number of existing lists.
'''
return lambda xs: list(
starmap(f, zip(*xs))
Line 1,156 ⟶ 1,171:
main()</lang>
{{Out}}
<pre>Markov chain text generation:
<pre>Soup! Who cares for fish, Game, or any place in order to start, for she was
 
losing her temper. 'Are you to destroy her. But until she starts back to
Great Oz, said Dorothy. The man was so surprised at this answer that he sat
this country alone, and cannot love. I pray you to obey you? Oh, yes; I am
down to breakfast, after which they started again upon their journey. The
sure we shall be very unhappy unless you give me brains, and very soon
sun shone bright and the birds sang sweetly, and Dorothy did not know
found out a box of comfits, (luckily the salt water had not gone much
anything about the charm of the Golden Cap and saw some words written upon
farther before she could do to ask: perhaps I shall not be afraid. One by
the lining. These, she thought, must be the right house, because the
one the mice to the Witch of the pole stuck up its back. While Dorothy was
chimneys were shaped like ears and the roof was thatched with fur. It was
shocked to see Oz, he said, 'on and off, for days and four kinds of cake
so large a house, that she did not like the look of the creature, but on
and four nights, hammering and twisting and bending and soldering and
the whole she thought it must be a very pretty dance,' said Alice timidly.
polishing and pounding at the picture.) 'Up, lazy thing!' said the Hatter,
'Would you like cats if you were me?' 'Well, perhaps not,' said Alice in a
it woke up again with a trumpet in one great flock of wild crows flew in
melancholy tone. 'Nobody seems to like her, down here, and I'm sure she's
one corner. She lay down to breakfast, after which she concluded that it
the best cat in the world! Oh, my dear Dinah! I wonder if I've been changed
will not send me back to Kansas, I hope it won't float, remarked Dorothy,
in the night? Let me think: was I the same when I got up this morning? I
it will be a kinder and more sounds of broken glass. 'What a curious plan!'</pre>
almost think I can swim to the shore and pull the raft after me, if you
like. If Oz will not give you any brains you will be the wisest man in all
the Land of Oz, said Dorothy, and I am thankful I am made of straw and
cannot be easily damaged.</pre>
 
=={{header|REXX}}==
9,659

edits