1 module Gargantext.Core.NLP where
3 import Control.Lens (Getter, at, non)
4 import qualified Data.Map.Strict as Map
5 import Data.Maybe (fromJust)
6 import Network.URI (URI(..), parseURI)
7 import Gargantext.Core (Lang(..), NLPServerConfig(..), PosTagAlgo(..))
8 import Gargantext.Prelude.NLP.Types (NLPConfig(..))
9 import Gargantext.Utils.Tuple (uncurryMaybeSecond)
10 import Protolude hiding (All)
13 type NLPServerMap = Map.Map Lang NLPServerConfig
15 class HasNLPServer env where
16 nlpServer :: Getter env NLPServerMap
17 nlpServerGet :: Lang -> Getter env NLPServerConfig
18 -- default implementation
19 nlpServerGet l = nlpServer . at l . non defaultNLPServer
21 defaultNLPServer :: NLPServerConfig
22 defaultNLPServer = NLPServerConfig { server = CoreNLP
23 , url = fromJust $ parseURI "http://localhost:9000"
26 nlpServerConfigFromURI :: URI -> Maybe NLPServerConfig
27 nlpServerConfigFromURI uri@(URI { uriScheme = "corenlp:" }) =
28 Just $ NLPServerConfig { server = CoreNLP
29 , url = uri { uriScheme = "http:" }
31 nlpServerConfigFromURI uri@(URI { uriScheme = "johnsnow:" }) =
32 Just $ NLPServerConfig { server = JohnSnowServer
33 , url = uri { uriScheme = "http:" }
35 nlpServerConfigFromURI uri@(URI { uriScheme = "spacy:" }) =
36 Just $ NLPServerConfig { server = Spacy
37 , url = uri { uriScheme = "http:" }
39 nlpServerConfigFromURI _ = Nothing
42 nlpServerMap :: NLPConfig -> NLPServerMap
43 nlpServerMap (NLPConfig { .. }) =
44 Map.fromList $ catMaybes [ uncurryMaybeSecond (EN, nlpServerConfigFromURI _nlp_en)
45 , uncurryMaybeSecond (FR, nlpServerConfigFromURI _nlp_fr)
46 , uncurryMaybeSecond (All, nlpServerConfigFromURI _nlp_all) ]