[NLP] implement reading lang configs for other languages

parent 175d4b29
cabal-version: 1.12
-- This file has been generated from package.yaml by hpack version 0.34.7.
-- This file has been generated from package.yaml by hpack version 0.35.1.
--
-- see: https://github.com/sol/hpack
......@@ -57,6 +57,7 @@ library
OverloadedStrings
RankNTypes
RecordWildCards
StrictData
build-depends:
MonadRandom
, SHA
......@@ -115,6 +116,7 @@ executable gargantext-prelude-exe
OverloadedStrings
RankNTypes
RecordWildCards
StrictData
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
MonadRandom
......@@ -176,6 +178,7 @@ test-suite gargantext-prelude-test
OverloadedStrings
RankNTypes
RecordWildCards
StrictData
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
MonadRandom
......
......@@ -71,6 +71,7 @@ default-extensions:
- OverloadedStrings
- RankNTypes
- RecordWildCards
- StrictData
library:
source-dirs: src
......
......@@ -13,28 +13,38 @@ module Gargantext.Prelude.NLP
(NLPConfig(..), readConfig)
where
import qualified Data.Ini as Ini
import qualified Data.Map.Strict as Map
import Data.Text (Text, unpack)
import qualified Data.Text as T
import Data.Maybe
import Gargantext.Prelude
import Gargantext.Prelude.Config (readIniFile', val)
import Gargantext.Prelude.NLP.Types (NLPConfig(..))
import Gargantext.Prelude.Utils (listToMaybeAll)
import Network.URI (parseURI)
import Protolude hiding (show)
import System.IO (FilePath)
type URL = Text
iniSection :: Text
iniSection = "nlp"
readConfig :: FilePath -> IO NLPConfig
readConfig fp = do
ini <- readIniFile' fp
let val' = val ini "nlp"
let val' = val ini iniSection
let m_nlp_en = parseURI $ cs $ val' "EN"
let m_nlp_fr = parseURI $ cs $ val' "FR"
let m_nlp_all = parseURI $ cs $ val' "All"
let mRet = NLPConfig <$> m_nlp_en <*> m_nlp_fr <*> m_nlp_all
let m_nlp_keys = filter (\k -> k `notElem` ["EN", "FR", "All"]) $ fromRight [] $ Ini.keys iniSection ini
let m_nlp_other = listToMaybeAll $ (\k -> (,) k <$> (parseURI $ cs $ val' k)) <$> m_nlp_keys
let mRet = NLPConfig <$> m_nlp_en <*> m_nlp_fr <*> m_nlp_all <*> (Map.fromList <$> m_nlp_other)
case mRet of
Nothing -> panic $ T.concat [ "Cannot read config file: _nlp_en = "
......@@ -42,5 +52,7 @@ readConfig fp = do
, ", _nlp_fr = "
, T.pack $ show m_nlp_fr
, ", _nlp_all = "
, T.pack $ show m_nlp_all ]
, T.pack $ show m_nlp_all
, ", _nlp_other = "
, T.pack $ show m_nlp_other ]
Just ret -> pure ret
......@@ -14,16 +14,17 @@ Portability : POSIX
module Gargantext.Prelude.NLP.Types where
import Control.Lens (makeLenses)
import qualified Data.Map.Strict as Map
import qualified Data.Text as T
import GHC.Generics (Generic)
import Network.Socket (PortNumber)
import Network.URI (URI)
import Protolude
data NLPConfig = NLPConfig { _nlp_en :: !URI
, _nlp_fr :: !URI
, _nlp_all :: !URI
}
data NLPConfig = NLPConfig { _nlp_en :: URI
, _nlp_fr :: URI
, _nlp_all :: URI
, _nlp_other :: (Map.Map T.Text URI) }
deriving (Generic, Show)
makeLenses ''NLPConfig
......@@ -17,12 +17,13 @@ module Gargantext.Prelude.Utils
where
import Control.Monad.Random.Class (MonadRandom)
import Protolude
import qualified System.Random.Shuffle as SRS
------------------------------------------------------------------------
-- | Misc Utils
shuffle :: MonadRandom m => [a] -> m [a]
shuffle ns = SRS.shuffleM ns
shuffle ns = SRS.shuffleM ns
--------------------------------------------------------------------------
-- TODO gargDB instance for NodeType
......@@ -31,3 +32,15 @@ data NodeToHash = NodeToHash { nodeType :: NodeType
, nodeId :: NodeId
}
-}
-- | Convert list of Maybe's to `Nothing` if at least 1 element is a
-- `Nothing` or `Just` otherwise.
listToMaybeAll :: [Maybe a] -> Maybe [a]
listToMaybeAll lst =
if length lst == length cm then
Just cm
else
Nothing
where
cm = catMaybes lst
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment