Skip to content

Commit

Permalink
* let user pick baseIRI
Browse files Browse the repository at this point in the history
related to #10
  • Loading branch information
odanoburu committed Aug 20, 2019
1 parent 1f54209 commit d61db9c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
13 changes: 9 additions & 4 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import Options.Applicative (customExecParser, prefs, ParserInfo, Parser, showHel
, argument, str, help, metavar, header)

data Command = Validate FilePath
| ExportCommand FilePath FilePath deriving Show
| ExportCommand String -- baseIRI
FilePath -- lexicographer directory
FilePath -- output file
deriving Show

showHelpOnErrorExecParser :: ParserInfo a -> IO a
showHelpOnErrorExecParser = customExecParser (prefs showHelpOnError)
Expand All @@ -36,8 +39,10 @@ parseValidateCommand = Validate <$> validateParser
(metavar "PATH" <> help "Validates one lexicographer file in case PATH is a file, else validates all lexicographer files in directory. Assumes lexnames.tsv is in the same PATH")

parseExportCommand :: Parser Command
parseExportCommand = ExportCommand <$> lexDirectory <*> outputFile
parseExportCommand = ExportCommand <$> baseIRI <*> lexDirectory <*> outputFile
where
baseIRI = argument str
(metavar "IRI" <> help "Base IRI for RDF nodes")
lexDirectory = argument str
(metavar "DIR" <> help "Directory where lexicographer files to export are in")
outputFile = argument str
Expand All @@ -61,5 +66,5 @@ main = do
if isDirectory
then validateLexicographerFiles filepath
else validateLexicographerFile filepath
(ExportCommand lexDirectory outputFile) ->
lexicographerFilesInDirectoryToTriples lexDirectory outputFile
(ExportCommand baseIri lexDirectory outputFile) ->
lexicographerFilesInDirectoryToTriples baseIri lexDirectory outputFile
23 changes: 10 additions & 13 deletions src/Lib.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import Data.RDF.Encoder.NQuads (encodeRDFGraph)
import Data.RDF.ToRDF (toTriples)
import Data.RDF.Types (RDFGraph(..), IRI(..))
import Data.Semigroup (sconcat)
import Data.String (fromString)
import Data.Text (Text)
import qualified Data.Text as T
import qualified Data.Text.IO as TIO
Expand All @@ -37,7 +38,6 @@ import System.FilePath ((</>), takeDirectory,normalise,equalFilePath)
import Data.Text.Prettyprint.Doc (Pretty(..))
import Data.Text.Prettyprint.Doc.Render.Text (putDoc)


parseLexicographerFile :: FilePath -> IO (SourceValidation (NonEmpty (Synset Unvalidated)))
parseLexicographerFile filePath = do
content <- TIO.readFile $ normalise filePath
Expand Down Expand Up @@ -145,27 +145,24 @@ validateLexicographerFiles filesDirectory = do
Success lexFilesSynsets
-> validateSynsetsNoParseErrors lexFilesSynsets Nothing

wn30 :: IRI
wn30 = "https://w3id.org/own-pt/wn30-en/"

synsetsToTriples :: NonEmpty (Synset Validated) -> FilePath -> IO ()
synsetsToTriples synsets outputFile =
synsetsToTriples :: IRI -> NonEmpty (Synset Validated) -> FilePath -> IO ()
synsetsToTriples baseIRI synsets outputFile =
encodeFile outputFile
. toLazyByteString
. encodeRDFGraph $ RDFGraph Nothing synsetsTriples
where
synsetsTriples = concatMap (toTriples wn30) synsets
synsetsTriples = concatMap (toTriples baseIRI) synsets

lexicographerFilesToTriples :: NonEmpty FilePath -> FilePath -> IO ()
lexicographerFilesToTriples fileNames outputFile = do
lexicographerFilesToTriples :: IRI -> NonEmpty FilePath -> FilePath -> IO ()
lexicographerFilesToTriples baseIRI fileNames outputFile = do
synsetsValid <- parseLexicographerFiles fileNames
case synsetsValid of
(Success synsets) -> synsetsToTriples synsets outputFile
(Success synsets) -> synsetsToTriples baseIRI synsets outputFile
(Failure _) -> void
$ putStrLn "Errors in lexicographer files, please validate them before exporting."

lexicographerFilesInDirectoryToTriples :: FilePath -> FilePath -> IO ()
lexicographerFilesInDirectoryToTriples lexicographerDir outputFile = do
lexicographerFilesInDirectoryToTriples :: String -> FilePath -> FilePath -> IO ()
lexicographerFilesInDirectoryToTriples baseIriString lexicographerDir outputFile = do
lexicographerFiles <- lexicographerFilesInDirectory lexicographerDir
lexicographerFilesToTriples lexicographerFiles outputFile
lexicographerFilesToTriples (fromString baseIriString) lexicographerFiles outputFile

0 comments on commit d61db9c

Please sign in to comment.