<commit_before>module Main (main) where

import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as BC
import Data.Char (isSpace)
import Data.List (nub, sort)
import System.Environment (getArgs)
import System.FilePath (takeDirectory)
import System.TimeIt

import HsSearch.SearchOptions
import HsSearch.Searcher
import HsSearch.SearchResult
import HsSearch.SearchSettings


validateSettings :: SearchSettings -> [String]
validateSettings settings = concat $ map ($settings) validators
  where validators = [ (\s -> if startPath s == ""
                              then ["Startpath not defined"]
                              else [])
                     , (\s -> if length (searchPatterns s) == 0
                              then ["No search patterns defined"]
                              else [])
                     ]

errsOrUsage :: [SearchOption] -> SearchSettings -> Maybe String
errsOrUsage searchOptions settings = do
  case usage of
    "" -> Nothing
    _  -> Just usage
  where errs   = validateSettings settings
        errMsg = if length errs > 0
                 then "\nERROR:\n" ++ unlines errs ++ "\n"
                 else ""
        usage  = if length errMsg > 0 || printUsage settings
                 then errMsg ++ getUsage searchOptions
                 else ""
        
formatResults :: SearchSettings -> [SearchResult] -> String
formatResults settings results =
  "\nSearch results (" ++ show (length results) ++ "):" ++
    (if length results > 0
       then "\n" ++ unlines (map (formatSearchResult settings) results)
       else "") ++
    (concat (map matchString (searchPatterns settings)))
  where matchString p = "\nMatches for "++ show p ++ ": " ++
          show (patternCount p) ++ "\n"
        patternCount :: String -> Int
        patternCount p = foldr accMatches 0 results
          where accMatches x acc = if p == (searchPattern x) then acc + 1 else acc

getMatchingDirs :: [SearchResult] -> [FilePath]
getMatchingDirs results = (sort . nub . map getDirectory) results
  where getDirectory r = takeDirectory (filePath r)
<commit_msg>Minor edit for output consistency in haskell<commit_after>module Main (main) where

import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as BC
import Data.Char (isSpace)
import Data.List (nub, sort)
import System.Environment (getArgs)
import System.FilePath (takeDirectory)
import System.TimeIt

import HsSearch.SearchOptions
import HsSearch.Searcher
import HsSearch.SearchResult
import HsSearch.SearchSettings


validateSettings :: SearchSettings -> [String]
validateSettings settings = concat $ map ($settings) validators
  where validators = [ (\s -> if startPath s == ""
                              then ["Startpath not defined"]
                              else [])
                     , (\s -> if length (searchPatterns s) == 0
                              then ["No search patterns defined"]
                              else [])
                     ]

errsOrUsage :: [SearchOption] -> SearchSettings -> Maybe String
errsOrUsage searchOptions settings = do
  case usage of
    "" -> Nothing
    _  -> Just usage
  where errs   = validateSettings settings
        errMsg = if length errs > 0
                 then "\nERROR:\n" ++ unlines errs ++ "\n"
                 else ""
        usage  = if length errMsg > 0 || printUsage settings
                 then errMsg ++ getUsage searchOptions
                 else ""
        
formatResults :: SearchSettings -> [SearchResult] -> String
formatResults settings results =
  "\nSearch results (" ++ show (length results) ++ "):" ++
    (if length results > 0
       then "\n" ++ unlines (map (formatSearchResult settings) results)
       else "")
  where matchString p = "\nMatches for "++ show p ++ ": " ++
          show (patternCount p) ++ "\n"
        patternCount :: String -> Int
        patternCount p = foldr accMatches 0 results
          where accMatches x acc = if p == (searchPattern x) then acc + 1 else acc

getMatchingDirs :: [SearchResult] -> [FilePath]
getMatchingDirs results = (sort . nub . map getDirectory) results
  where getDirectory r = takeDirectory (filePath r)

{'fraction_non_alphanumeric': 0.09633357296908698, 'fraction_numerical': 0.0033549005511622335, 'mean_word_length': 2.310071371927042, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 31, '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': '2065616', 'n_tokens_mistral': 1077, 'n_tokens_neox': 1060, 'n_words': 379}