import System.IO
import Text.ParserCombinators.Parsec (eof)
import NGramCrackers.NGramCrackers
import NGramCrackers.ParagraphParsers
import Data.Char (toUpper)
import Data.List (intersperse)
import Control.Monad (liftM)
import Data.Either.Unwrap (fromRight)

main = do
    inHandle   <- openFile "NGramCrackers/spacelessStory.txt" ReadMode
    outHandle  <- openFile "NGramCrackers/processedStory.txt" WriteMode
    contents <- hGetContents inHandle 
    case parseParagraph contents of 
         Left e  -> do putStrLn "Error parsing input: "
                       print e

         Right r -> mapM_ putStrLn $ liftM unwords r
    --eof

    --hPutStrLn outHandle $ (concat . intersperse "\n" . bigrams) contents
    -- putStrLn $ fromRight $ liftM concat $ liftM (intersperse "\n") $ liftM unwords $ liftM concat $ parseParagraph contents
--    putStrLn $ fromRight $ liftM unwords $ liftM (intersperse "\n") $ liftM concat $ parseParagraph contents
    hClose inHandle
    hClose outHandle

{- mainLoop :: Handle -> Handle -> IO ()
mainLoop inHandle outHandle = do isEnd <- hIsEOF inHandle
                                 if isEnd
                                   then return ()
                                   else do inputStr <- hGetLine inHandle
                                           hPutStrLn outHandle (map toUpper inputStr)
                                           mainLoop inHandle outHandle
-}

import System.IO
import Text.ParserCombinators.Parsec (eof)
import NGramCrackers.NGramCrackers
import NGramCrackers.TupleManipulation
import NGramCrackers.ParagraphParsers
import Data.Char (toUpper)
import Data.List (intersperse)
import Control.Monad (liftM)
import Data.Either.Unwrap (fromRight)

main = do
    inHandle   <- openFile "NGramCrackers/spacelessStory.txt" ReadMode
    outHandle  <- openFile "NGramCrackers/processedStory.txt" WriteMode
    contents <- hGetContents inHandle -- contents :: String
    case parseParagraph contents of 
         Left e  -> do putStrLn "Error parsing input: "
                       print e

         --Right r -> mapM_ putStrLn $ map stringifyLexemeCount $ (lexemeCountProfile . words . concat) r
         Right r   -> mapM_ putStrLn $ map stringifyLexemeCount $ lexemeCountProfile $ concat r
    --eof

    --hPutStrLn outHandle $ (concat . intersperse "\n" . bigrams) contents
    -- putStrLn $ fromRight $ liftM concat $ liftM (intersperse "\n") $ liftM unwords $ liftM concat $ parseParagraph contents
--    putStrLn $ fromRight $ liftM unwords $ liftM (intersperse "\n") $ liftM concat $ parseParagraph contents
    hClose inHandle
    hClose outHandle

{- mainLoop :: Handle -> Handle -> IO ()
mainLoop inHandle outHandle = do isEnd <- hIsEOF inHandle
                                 if isEnd
                                   then return ()
                                   else do inputStr <- hGetLine inHandle
                                           hPutStrLn outHandle (map toUpper inputStr)
                                           mainLoop inHandle outHandle
-}

Add output of lexeme counts to stdout
Add output of lexeme counts to stdout

  -- With the parser doing what it is supposed to mostly, it
     was necessary to include a function to put the count tuple
     into a string for printing.

  -- Added the TupleManipulation import

  -- Will be useful to clean up the excess, trial and error code,
     most of which results in errors, because they confuse the
     resultant type of parseParagraph.

  -- Next up may be getting this printed to a file, without using
     bash output redirection.
