<commit_before>{-# LANGUAGE GeneralizedNewtypeDeriving #-}

module FileUtil where

import           BasicPrelude
import           Data.ByteString    (hGetContents)
import qualified Data.Text          as T
import           Data.Text.Encoding (decodeLatin1)
import qualified System.Directory   as Dir
import           System.IO          (IOMode (ReadMode), withFile)
import           System.Process



listFilesInDirectory :: FilePath -> IO [FilePath]
listFilesInDirectory dir = do
    rawList  <- Dir.listDirectory dir
    let absolutePaths = map (dir </>) rawList
    fileList <- filterM Dir.doesFileExist absolutePaths
    return fileList


-- Does not work when invoked from the fish shell.
-- Not sure why not.
readFileLatin1 :: FilePath -> IO Text
readFileLatin1 pathname =
    fmap decodeLatin1 (withFile pathname ReadMode hGetContents)


-- Accepts encodings such as LATIN1.
-- Not currently in use.
readFileAsUtf8 :: FilePath -> String -> IO Text
readFileAsUtf8 pathname sourceEncoding = do
    let stdin' = ""
    stdout' <- readProcess "iconv" ["-f", sourceEncoding, "-t", "utf-8", pathname] stdin'<commit_msg>refactor: monadic progamming<commit_after>{-# LANGUAGE GeneralizedNewtypeDeriving #-}

module FileUtil where

import           BasicPrelude
import           Data.ByteString    (hGetContents)
import qualified Data.Text          as T
import           Data.Text.Encoding (decodeLatin1)
import qualified System.Directory   as Dir
import           System.IO          (IOMode (ReadMode), withFile)
import           System.Process



listFilesInDirectory :: FilePath -> IO [FilePath]
listFilesInDirectory dir = do
    rawList <- Dir.listDirectory dir
    let absolutePaths = map (dir </>) rawList
    filterM Dir.doesFileExist absolutePaths


-- Does not work when invoked from the fish shell.
-- Not sure why not.
readFileLatin1 :: FilePath -> IO Text
readFileLatin1 pathname =
    fmap decodeLatin1 (withFile pathname ReadMode hGetContents)


-- Accepts encodings such as LATIN1.
-- Not currently in use.
readFileAsUtf8 :: FilePath -> String -> IO Text
readFileAsUtf8 pathname sourceEncoding = do
    let stdin' = ""
    stdout' <- readProcess "iconv" ["-f", sourceEncoding, "-t", "utf-8", pathname] stdin'
{'fraction_non_alphanumeric': 0.08513513513513514, 'fraction_numerical': 0.007207207207207207, 'mean_word_length': 3.1359404096834265, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 13, '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': '6669500', 'n_tokens_mistral': 658, 'n_tokens_neox': 631, 'n_words': 210}