[NLP] implement reading lang configs for other languages

parent 175d4b29
cabal-version: 1.12 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 -- see: https://github.com/sol/hpack
...@@ -57,6 +57,7 @@ library ...@@ -57,6 +57,7 @@ library
OverloadedStrings OverloadedStrings
RankNTypes RankNTypes
RecordWildCards RecordWildCards
StrictData
build-depends: build-depends:
MonadRandom MonadRandom
, SHA , SHA
...@@ -115,6 +116,7 @@ executable gargantext-prelude-exe ...@@ -115,6 +116,7 @@ executable gargantext-prelude-exe
OverloadedStrings OverloadedStrings
RankNTypes RankNTypes
RecordWildCards RecordWildCards
StrictData
ghc-options: -threaded -rtsopts -with-rtsopts=-N ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends: build-depends:
MonadRandom MonadRandom
...@@ -176,6 +178,7 @@ test-suite gargantext-prelude-test ...@@ -176,6 +178,7 @@ test-suite gargantext-prelude-test
OverloadedStrings OverloadedStrings
RankNTypes RankNTypes
RecordWildCards RecordWildCards
StrictData
ghc-options: -threaded -rtsopts -with-rtsopts=-N ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends: build-depends:
MonadRandom MonadRandom
......
...@@ -71,6 +71,7 @@ default-extensions: ...@@ -71,6 +71,7 @@ default-extensions:
- OverloadedStrings - OverloadedStrings
- RankNTypes - RankNTypes
- RecordWildCards - RecordWildCards
- StrictData
library: library:
source-dirs: src source-dirs: src
......
...@@ -13,28 +13,38 @@ module Gargantext.Prelude.NLP ...@@ -13,28 +13,38 @@ module Gargantext.Prelude.NLP
(NLPConfig(..), readConfig) (NLPConfig(..), readConfig)
where where
import qualified Data.Ini as Ini
import qualified Data.Map.Strict as Map
import Data.Text (Text, unpack) import Data.Text (Text, unpack)
import qualified Data.Text as T import qualified Data.Text as T
import Data.Maybe import Data.Maybe
import Gargantext.Prelude import Gargantext.Prelude
import Gargantext.Prelude.Config (readIniFile', val) import Gargantext.Prelude.Config (readIniFile', val)
import Gargantext.Prelude.NLP.Types (NLPConfig(..)) import Gargantext.Prelude.NLP.Types (NLPConfig(..))
import Gargantext.Prelude.Utils (listToMaybeAll)
import Network.URI (parseURI) import Network.URI (parseURI)
import Protolude hiding (show)
import System.IO (FilePath) import System.IO (FilePath)
type URL = Text type URL = Text
iniSection :: Text
iniSection = "nlp"
readConfig :: FilePath -> IO NLPConfig readConfig :: FilePath -> IO NLPConfig
readConfig fp = do readConfig fp = do
ini <- readIniFile' fp ini <- readIniFile' fp
let val' = val ini "nlp" let val' = val ini iniSection
let m_nlp_en = parseURI $ cs $ val' "EN" let m_nlp_en = parseURI $ cs $ val' "EN"
let m_nlp_fr = parseURI $ cs $ val' "FR" let m_nlp_fr = parseURI $ cs $ val' "FR"
let m_nlp_all = parseURI $ cs $ val' "All" 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 case mRet of
Nothing -> panic $ T.concat [ "Cannot read config file: _nlp_en = " Nothing -> panic $ T.concat [ "Cannot read config file: _nlp_en = "
...@@ -42,5 +52,7 @@ readConfig fp = do ...@@ -42,5 +52,7 @@ readConfig fp = do
, ", _nlp_fr = " , ", _nlp_fr = "
, T.pack $ show m_nlp_fr , T.pack $ show m_nlp_fr
, ", _nlp_all = " , ", _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 Just ret -> pure ret
...@@ -14,16 +14,17 @@ Portability : POSIX ...@@ -14,16 +14,17 @@ Portability : POSIX
module Gargantext.Prelude.NLP.Types where module Gargantext.Prelude.NLP.Types where
import Control.Lens (makeLenses) import Control.Lens (makeLenses)
import qualified Data.Map.Strict as Map
import qualified Data.Text as T import qualified Data.Text as T
import GHC.Generics (Generic) import GHC.Generics (Generic)
import Network.Socket (PortNumber) import Network.Socket (PortNumber)
import Network.URI (URI) import Network.URI (URI)
import Protolude import Protolude
data NLPConfig = NLPConfig { _nlp_en :: !URI data NLPConfig = NLPConfig { _nlp_en :: URI
, _nlp_fr :: !URI , _nlp_fr :: URI
, _nlp_all :: !URI , _nlp_all :: URI
} , _nlp_other :: (Map.Map T.Text URI) }
deriving (Generic, Show) deriving (Generic, Show)
makeLenses ''NLPConfig makeLenses ''NLPConfig
...@@ -17,12 +17,13 @@ module Gargantext.Prelude.Utils ...@@ -17,12 +17,13 @@ module Gargantext.Prelude.Utils
where where
import Control.Monad.Random.Class (MonadRandom) import Control.Monad.Random.Class (MonadRandom)
import Protolude
import qualified System.Random.Shuffle as SRS import qualified System.Random.Shuffle as SRS
------------------------------------------------------------------------ ------------------------------------------------------------------------
-- | Misc Utils -- | Misc Utils
shuffle :: MonadRandom m => [a] -> m [a] shuffle :: MonadRandom m => [a] -> m [a]
shuffle ns = SRS.shuffleM ns shuffle ns = SRS.shuffleM ns
-------------------------------------------------------------------------- --------------------------------------------------------------------------
-- TODO gargDB instance for NodeType -- TODO gargDB instance for NodeType
...@@ -31,3 +32,15 @@ data NodeToHash = NodeToHash { nodeType :: NodeType ...@@ -31,3 +32,15 @@ data NodeToHash = NodeToHash { nodeType :: NodeType
, nodeId :: NodeId , 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