module Conllu.IO where
import Conllu.Type
import Conllu.Utils
import Conllu.Parse
import Conllu.Print
import Conllu.Diff
import System.Directory
import System.FilePath
readConlluFileWith :: Parser Sent -> FilePath -> IO Doc
readConlluFileWith p f = do
ds <- readFile f
case parseConlluWith p f ds of
Left err -> putStr err *> return []
Right d -> return d
readDirectoryWith :: Parser Sent -> FilePath -> IO [Doc]
readDirectoryWith p d = do fs' <- listDirectory d
let fs = map (d </>) fs'
mapM (readConlluFileWith p) fs
readConlluWith :: Parser Sent -> FilePath -> IO [Doc]
readConlluWith p fp = do f <- doesFileExist fp
if' f (mapM (readConlluFileWith p) [fp]) $
do d <- doesDirectoryExist fp
if' d (readDirectoryWith p fp) (return [])
readConlluFile :: FilePath -> IO Doc
readConlluFile = readConlluFileWith sentence
readDirectory :: FilePath -> IO [Doc]
readDirectory = readDirectoryWith sentence
readConllu :: FilePath -> IO [Doc]
readConllu = readConlluWith sentence
writeConlluFile :: FilePath -> Doc -> IO ()
writeConlluFile fp = writeFile fp . printDoc
readAndPrintConllu :: FilePath -> IO ()
readAndPrintConllu fp = do
readConlluFile fp >>= putStr . printDoc
return ()
diffConllu :: FilePath -> FilePath -> IO ()
diffConllu fp1 fp2 = do
ss1 <- readConlluFile fp1
ss2 <- readConlluFile fp2
print . printDDiff . diffSs $ zip ss1 ss2
return ()