<commit_before>
module Paper.Talk(talk) where

import Control.Monad
import Data.List
import Data.Char
import Data.Maybe
import System.FilePath
import System.Cmd
import System.Directory


talk :: FilePath -> [FilePath] -> [String] -> IO ()
talk obj files flags = do
    ss <- mapM (liftM text . readFile) files
    let out = obj </> "talk.txt"
    writeFile out $ flatten $ concat $ intersperse "\n\n" ss
    when ("" `notElem` flags) $ do
        let prog = head (flags ++ ["ptts"])
        r <- findExecutable prog
        case r of
            Nothing -> putStrLn $ "Can't find \"" ++ prog ++ "\" program, please install"
            Just y -> speak prog out (obj </> "talk.wav")


speak "ptts" input output = do
    system $ "ptts -voice \"Microsoft Mary\" -w \"" ++ output ++ "\" < \"" ++ input ++ "\""
    return ()

speak prog input output = putStrLn $ "Don't know how to speak with " ++ prog


-- compress \n\n\n -> \n
--          "  " -> " "
--          " \n" -> "\n"
flatten = f . g
    where
        g (' ':'\n':xs) = g ('\n':xs)
        g ('\n':' ':xs) = g ('\n':xs)
        g (' ':' ':xs) = g (' ':xs)
        g (x:xs) = x : g xs
        g [] = []

        f ('\n':'\n':'\n':xs) = f ('\n':'\n':xs)
        f (x:xs) = x : f xs
        f [] = []


text :: String -> String

text ('%':xs) = text $ dropWhile (/= '\n') xs
text ('\\':'%':xs) = '%' : text xs

text (x:xs) | x `elem` "|$" = case lex xs of
    [(s,y:xs)] | x == y -> s ++ text xs
    _ -> "expression" ++ text (drop 1 $ dropWhile (/= x) xs)
text ('\\':'$':xs) = '$' : text xs


-- Given an environment there are several behaviours:
-- replace the entire environment with some alternative text (figure, code)
-- drop the begin/end bits
text ('\\':xs) | "begin" `isPrefixOf` xs =
    case lookup a rep of<commit_msg>Deal with smart quotes in latex documents<commit_after>
module Paper.Talk(talk) where

import Control.Monad
import Data.List
import Data.Char
import Data.Maybe
import System.FilePath
import System.Cmd
import System.Directory


talk :: FilePath -> [FilePath] -> [String] -> IO ()
talk obj files flags = do
    ss <- mapM (liftM text . readFile) files
    let out = obj </> "talk.txt"
    writeFile out $ flatten $ concat $ intersperse "\n\n" ss
    when ("" `notElem` flags) $ do
        let prog = head (flags ++ ["ptts"])
        r <- findExecutable prog
        case r of
            Nothing -> putStrLn $ "Can't find \"" ++ prog ++ "\" program, please install"
            Just y -> speak prog out (obj </> "talk.wav")


speak "ptts" input output = do
    system $ "ptts -voice \"Microsoft Mary\" -w \"" ++ output ++ "\" < \"" ++ input ++ "\""
    return ()

speak prog input output = putStrLn $ "Don't know how to speak with " ++ prog


-- compress \n\n\n -> \n
--          "  " -> " "
--          " \n" -> "\n"
flatten = f . g
    where
        g (' ':'\n':xs) = g ('\n':xs)
        g ('\n':' ':xs) = g ('\n':xs)
        g (' ':' ':xs) = g (' ':xs)
        g (x:xs) = x : g xs
        g [] = []

        f ('\n':'\n':'\n':xs) = f ('\n':'\n':xs)
        f (x:xs) = x : f xs
        f [] = []


text :: String -> String

text ('%':xs) = text $ dropWhile (/= '\n') xs
text ('\\':'%':xs) = '%' : text xs

text (x:xs) | x `elem` "|$" = case lex xs of
    [(s,y:xs)] | x == y -> s ++ text xs
    _ -> "expression" ++ text (drop 1 $ dropWhile (/= x) xs)
text ('\\':'$':xs) = '$' : text xs

text ('`':'`':xs) = '\"' : text xs
text (''':''':xs) = '\"' : text xs
text ('`':xs) = '\'' : text xs

-- Given an environment there are several behaviours:
-- replace the entire environment with some alternative text (figure, code)
-- drop the begin/end bits
text ('\\':xs) | "begin" `isPrefixOf` xs =
    case lookup a rep of
{'fraction_non_alphanumeric': 0.20924574209245742, 'fraction_numerical': 0.0005406866720735334, 'mean_word_length': 2.1596925704526044, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3569259', 'n_tokens_mistral': 1364, 'n_tokens_neox': 1278, 'n_words': 433}