<commit_before>module HSat.Parser (
  fromCNFFile,
  fromFile,
  fromFolder,
  runReadFile
  ) where

import HSat.Problem
import Control.Monad.Trans.Either
import HSat.Problem.ProblemType (ProblemType)
import qualified HSat.Problem.ProblemType as P
import HSat.Problem.BSP.CNF
import System.Directory
import Data.Attoparsec.Text
import HSat.Problem.BSP.CNF.Builder
import Control.Applicative
import Data.Text.IO as T
import HSat.Parser.CNF
import Control.Monad.Trans
import HSat.Problem.ProblemExpr
import HSat.Problem.Source
import Data.List (delete)

type ReadFile a = EitherT ProblemParseError IO a

runReadFile :: ReadFile a -> IO (Either ProblemParseError a)
runReadFile f = runEitherT f

fromCNFFile :: FilePath -> ReadFile CNF
fromCNFFile fp = do
  doesFileExist' fp
  text <- lift $ T.readFile fp
  let cnf = parseOnly cnfParser text
  hoistEither $ cnfParserErrorToProblemParseError cnf

doesFileExist' :: FilePath -> ReadFile ()
doesFileExist' fp = do
  exists <- lift $ doesFileExist fp
  case exists of
    True -> return ()
    False -> left $ (FileNotFound fp)

fromFile :: FilePath -> ReadFile Problem
fromFile filePath = do
  fileType <- hoistEither $ getProblemType filePath
  expr <- case fileType of
    P.CNF -> mkCNFProblem `liftA` fromCNFFile filePath
  return $ mkProblem (mkFileSource filePath) expr

fromFolder :: (FilePath -> ReadFile Problem) -> FilePath -> IO [Either ProblemParseError Problem]<commit_msg>Removed argument to create point free runReadFile<commit_after>module HSat.Parser (
  fromCNFFile,
  fromFile,
  fromFolder,
  runReadFile
  ) where

import HSat.Problem
import Control.Monad.Trans.Either
import HSat.Problem.ProblemType (ProblemType)
import qualified HSat.Problem.ProblemType as P
import HSat.Problem.BSP.CNF
import System.Directory
import Data.Attoparsec.Text
import HSat.Problem.BSP.CNF.Builder
import Control.Applicative
import Data.Text.IO as T
import HSat.Parser.CNF
import Control.Monad.Trans
import HSat.Problem.ProblemExpr
import HSat.Problem.Source
import Data.List (delete)

type ReadFile a = EitherT ProblemParseError IO a

runReadFile :: ReadFile a -> IO (Either ProblemParseError a)
runReadFile = runEitherT

fromCNFFile :: FilePath -> ReadFile CNF
fromCNFFile fp = do
  doesFileExist' fp
  text <- lift $ T.readFile fp
  let cnf = parseOnly cnfParser text
  hoistEither $ cnfParserErrorToProblemParseError cnf

doesFileExist' :: FilePath -> ReadFile ()
doesFileExist' fp = do
  exists <- lift $ doesFileExist fp
  case exists of
    True -> return ()
    False -> left $ (FileNotFound fp)

fromFile :: FilePath -> ReadFile Problem
fromFile filePath = do
  fileType <- hoistEither $ getProblemType filePath
  expr <- case fileType of
    P.CNF -> mkCNFProblem `liftA` fromCNFFile filePath
  return $ mkProblem (mkFileSource filePath) expr

fromFolder :: (FilePath -> ReadFile Problem) -> FilePath -> IO [Either ProblemParseError Problem]
{'fraction_non_alphanumeric': 0.07862068965517241, 'fraction_numerical': 0.0, 'mean_word_length': 4.442776735459662, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 23, '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': '7086188', 'n_tokens_mistral': 971, 'n_tokens_neox': 900, 'n_words': 275}