HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Haskell markov text generator

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
textmarkovhaskellgenerator

Problem

I'm new to Haskell, and here is my first not-totally-trivial program. It's the first program I tend to write in any language -- a Markov text generator. I'm wondering what I can change to make it more idiomatic, or what language features I could make better use of.

import Data.List
import System.Environment
import qualified Data.Map as Map
import Control.Monad.State
import System.Random

type MarkovMap = Map.Map String String
type MarkovState = (MarkovMap, StdGen, String)

transition :: State MarkovState Char
transition = do 
    (m, gen, sofar)  State MarkovState String
generateText s = do
    x  Int -> [String]
getWords m n =
    let keys        = filter ((==) ' ' . last) $ Map.keys m 
        (r, gen)    = randomR (0, length keys - 1) $ mkStdGen 137
        startState  = keys !! r 
        markovChars = evalState (generateText transition) (m, gen, startState)
    in  take n . words $ markovChars

printWords :: [String] -> IO ()
printWords ws = mapM_ putStrLn $ makeLines ws
    where makeLines [] = []
          makeLines ws = unwords (take 10 ws) : makeLines (drop 10 ws)

main :: IO ()
main = do
    (n:nwords:fileName:[])  String -> Map.Map String String
chain n xs = 
    let from = map (take n) . tails $ xs ++ " " ++ xs
        to   = drop n . map (:[]) $ xs ++ " " ++ take n xs
    in Map.fromListWith (++) $ zip from to


Example usage:

Keep track of the last 3 characters, take 100 words from tobeornottobe.txt

```
> runhaskell markov.hs 3 100 tobeornottobe.txt

delay, the law's count with who would bear things all;
and that make and sweary life; fortal shocks the hue
of returns tural consience of somethis againsolution. To die, thers
the he law's the have, or nobler retus resolence to
troublesh is noblesh is sicklied of regards of some will,
and arrows of? To beary from who would by a
life; for inst give spurns, and, but to sleep: perchan
flesh is heir thing afterprises us for no mortal shocks
turns of action devoutly to dreathe sleep: perchance thance

Solution

First of all, it seems like you never change the MarkovMap in your States. So, why don't you take it out of MarkovState and change the type of, say, transition, to MarkovMap -> State MarkovState Char?

Try to use more combinators and less pattern-matching. For example, the generate function is something like sequence . repeat.

Context

StackExchange Code Review Q#24791, answer score: 3

Revisions (0)

No revisions yet.