<commit_before>{-# LANGUAGE NamedFieldPuns #-}

module Javran.MaxFlow.Lib
  ( main
  )
where

import Control.Monad.Except
import qualified Data.Map.Strict as M
import qualified Data.Text.IO as T
import Javran.MaxFlow.Common
import Javran.MaxFlow.EdmondsKarp
import Javran.MaxFlow.Parser
import Javran.MaxFlow.TestData
import Javran.MaxFlow.Verify
import System.Directory
import System.Environment
import System.Exit
import System.FilePath.Posix

batchProcess :: FilePath -> IO ()
batchProcess pBase = do
  de <- doesDirectoryExist pBase
  if de
    then
      listDirectory pBase
        >>= mapM_ (batchProcess . (pBase </>))
    else when (takeExtension pBase == ".dimacs") $
      do
        let fName = pBase
        putStr $ pBase <> ": "
        raw <- T.readFile fName
        case parseFromRaw raw of
          Left msg -> do
            putStrLn $ "parse error: " <> msg
          Right nr -> do
            let (result, _logs) = maxFlow (normalize nr)
            case result of
              Left msg -> do
                putStrLn $ "error: " <> msg
              Right (v, _arcs, _) -> do
                putStrLn $ "max flow: " <> show v

main :: IO ()
main = do
  args <- getArgs
  case args of
    ["dev", fName] -> do
      raw <- T.readFile fName
      case parseFromRaw raw of
        Left msg -> do
          putStrLn $ "parse error: " <> msg
          exitFailure
        Right nrRaw -> do
          let nn = normalize nrRaw
              nr@NetworkRep {nrSource, nrSink} = getNR nn
          print nr
          let (result, logs) = maxFlow nn
          mapM_ T.putStrLn logs
          case result of
            Left msg -> do
              putStrLn $ "error: " <> msg
              exitFailure
            Right (v, arcs, cMap) -> do
              putStrLn $ "max flow: " <> show v
              putStrLn $
                "non zero assignments: "
                  <> show
                    (filter ((/= 0) . snd) $ M.toList arcs)
              putStrLn $
                "verification: "
                  <> (show $ runExcept $ verify nrSource nrSink cMap arcs)
    ["batch", basePath] -> do
      p <- doesPathExist basePath
      if p
        then batchProcess basePath
        else do
          putStrLn "path does not exist"
          exitFailure
    ["embed"] -> do
      print (fst <$> (packSimple <> packGenetic <> packHandmade <> packRandomBest))
    _ -> do
      putStrLn "<prog> dev <data file>: run on a single dimacs file."
      putStrLn "<prog> batch <base path>: batch-process files."
      exitFailure<commit_msg>factor out loading from file.<commit_after>{-# LANGUAGE NamedFieldPuns #-}

module Javran.MaxFlow.Lib
  ( main
  )
where

import Control.Monad.Except
import qualified Data.Map.Strict as M
import qualified Data.Text.IO as T
import Javran.MaxFlow.Common
import Javran.MaxFlow.EdmondsKarp
import Javran.MaxFlow.Parser
import Javran.MaxFlow.TestData
import Javran.MaxFlow.Verify
import System.Directory
import System.Environment
import System.Exit
import System.FilePath.Posix

batchProcess :: FilePath -> IO ()
batchProcess pBase = do
  de <- doesDirectoryExist pBase
  if de
    then
      listDirectory pBase
        >>= mapM_ (batchProcess . (pBase </>))
    else when (takeExtension pBase == ".dimacs") $
      do
        let fName = pBase
        putStr $ pBase <> ": "
        raw <- T.readFile fName
        case parseFromRaw raw of
          Left msg -> do
            putStrLn $ "parse error: " <> msg
          Right nr -> do
            let (result, _logs) = maxFlow (normalize nr)
            case result of
              Left msg -> do
                putStrLn $ "error: " <> msg
              Right (v, _arcs, _) -> do
                putStrLn $ "max flow: " <> show v

-- load, parse, and normalize a network from FilePath.
loadNetwork :: FilePath -> IO NormalizedNetwork
loadNetwork fName = do
  raw <- T.readFile fName
  case parseFromRaw raw of
    Left msg -> do
      putStrLn $ "parse error: " <> msg
      exitFailure
    Right nrRaw -> do
      pure $ normalize nrRaw

main :: IO ()
main = do
  args <- getArgs
  case args of
    ["run", fName] -> do
      nn <- loadNetwork fName
      let nr@NetworkRep {nrSource, nrSink} = getNR nn
      print nr
      let (result, logs) = maxFlow nn
      mapM_ T.putStrLn logs
      case result of
        Left msg -> do
          putStrLn $ "error: " <> msg
          exitFailure
        Right (v, arcs, cMap) -> do
          putStrLn $ "max flow: " <> show v
          putStrLn $
            "non zero assignments: "
              <> show
                (filter ((/= 0) . snd) $ M.toList arcs)
          putStrLn $
            "verification: "
              <> (show $ runExcept $ verify nrSource nrSink cMap arcs)
    ["batch", basePath] -> do
      p <- doesPathExist basePath
      if p
        then batchProcess basePath
        else do
          putStrLn "path does not exist"
          exitFailure
    ["embed"] -> do
      print (fst <$> (packSimple <> packGenetic <> packHandmade <> packRandomBest))
    _ -> do
      putStrLn "<prog> run <data file>: run on a single dimacs file."
      putStrLn "<prog> batch <base path>: batch-process files."
      exitFailure
{'fraction_non_alphanumeric': 0.09574263147755731, 'fraction_numerical': 0.00038528221922558276, 'mean_word_length': 1.835608956854178, 'pattern_counts': {'":': 2, '<': 50, '<?xml version=': 0, '>': 70, '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': '7467438', 'n_tokens_mistral': 1608, 'n_tokens_neox': 1549, 'n_words': 503}