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
{{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)
▲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.
'''
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
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.
'''
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
'''
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
'''
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
derived by expanding any ~ in fp.'''
with
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)
# 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:
Great Oz, said Dorothy. The man was so surprised at this answer that he sat
down to breakfast, after which they started again upon their journey. The
sun shone bright and the birds sang sweetly, and Dorothy did not know
anything about the charm of the Golden Cap and saw some words written upon
the lining. These, she thought, must be the right house, because the
chimneys were shaped like ears and the roof was thatched with fur. It was
so large a house, that she did not like the look of the creature, but on
the whole she thought it must be a very pretty dance,' said Alice timidly.
'Would you like cats if you were me?' 'Well, perhaps not,' said Alice in a
melancholy tone. 'Nobody seems to like her, down here, and I'm sure she's
the best cat in the world! Oh, my dear Dinah! I wonder if I've been changed
in the night? Let me think: was I the same when I got up this morning? I
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}}==
|