Commit e209df2e authored by Alfredo Di Napoli's avatar Alfredo Di Napoli

Add addLanguageToCorpus function

parent cb739183
...@@ -44,6 +44,7 @@ library ...@@ -44,6 +44,7 @@ library
Gargantext.API.Node Gargantext.API.Node
Gargantext.API.Node.Corpus.New Gargantext.API.Node.Corpus.New
Gargantext.API.Node.Corpus.Types Gargantext.API.Node.Corpus.Types
Gargantext.API.Node.Corpus.Update
Gargantext.API.Node.File Gargantext.API.Node.File
Gargantext.API.Node.Share Gargantext.API.Node.Share
Gargantext.API.Prelude Gargantext.API.Prelude
......
...@@ -14,6 +14,7 @@ New corpus means either: ...@@ -14,6 +14,7 @@ New corpus means either:
{-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeOperators #-} {-# LANGUAGE TypeOperators #-}
{-# LANGUAGE ViewPatterns #-}
module Gargantext.API.Node.Corpus.New module Gargantext.API.Node.Corpus.New
where where
...@@ -26,7 +27,6 @@ import Data.Aeson.TH (deriveJSON) ...@@ -26,7 +27,6 @@ import Data.Aeson.TH (deriveJSON)
import qualified Data.ByteString.Base64 as BSB64 import qualified Data.ByteString.Base64 as BSB64
import Data.Conduit.Internal (zipSources) import Data.Conduit.Internal (zipSources)
import Data.Either import Data.Either
import Data.Maybe (fromMaybe)
import Data.Swagger import Data.Swagger
import Data.Text (Text) import Data.Text (Text)
import qualified Data.Text as T import qualified Data.Text as T
...@@ -43,8 +43,9 @@ import Gargantext.API.Admin.Types (HasSettings) ...@@ -43,8 +43,9 @@ import Gargantext.API.Admin.Types (HasSettings)
import Gargantext.API.Node.Corpus.New.Types import Gargantext.API.Node.Corpus.New.Types
import Gargantext.API.Node.Corpus.Searx import Gargantext.API.Node.Corpus.Searx
import Gargantext.API.Node.Corpus.Types import Gargantext.API.Node.Corpus.Types
import Gargantext.API.Node.Corpus.Update (addLanguageToCorpus)
import Gargantext.API.Node.Types import Gargantext.API.Node.Types
import Gargantext.Core (Lang(..)) import Gargantext.Core (Lang(..), withDefaultLanguage)
import Gargantext.Core.Text.List.Social (FlowSocialListWith(..)) import Gargantext.Core.Text.List.Social (FlowSocialListWith(..))
import Gargantext.Core.Types.Individu (User(..)) import Gargantext.Core.Types.Individu (User(..))
import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger) import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger)
...@@ -205,10 +206,7 @@ addToCorpusWithQuery user cid (WithQuery { _wq_query = q ...@@ -205,10 +206,7 @@ addToCorpusWithQuery user cid (WithQuery { _wq_query = q
-- printDebug "[addToCorpusWithQuery] datafield" datafield -- printDebug "[addToCorpusWithQuery] datafield" datafield
-- printDebug "[addToCorpusWithQuery] flowListWith" flw -- printDebug "[addToCorpusWithQuery] flowListWith" flw
-- TODO: update Node Corpus with the Lang addLanguageToCorpus cid l
-- n <- getNode cid
-- let n.wq_lang = l
-- saveNode n
case datafield of case datafield of
Just Web -> do Just Web -> do
...@@ -265,15 +263,12 @@ addToCorpusWithForm :: (FlowCmdM env err m, MonadJobStatus m) ...@@ -265,15 +263,12 @@ addToCorpusWithForm :: (FlowCmdM env err m, MonadJobStatus m)
-> NewWithForm -> NewWithForm
-> JobHandle m -> JobHandle m
-> m () -> m ()
addToCorpusWithForm user cid (NewWithForm ft ff d l _n sel) jobHandle = do addToCorpusWithForm user cid (NewWithForm ft ff d (withDefaultLanguage -> l) _n sel) jobHandle = do
-- printDebug "[addToCorpusWithForm] Parsing corpus: " cid -- printDebug "[addToCorpusWithForm] Parsing corpus: " cid
-- printDebug "[addToCorpusWithForm] fileType" ft -- printDebug "[addToCorpusWithForm] fileType" ft
-- printDebug "[addToCorpusWithForm] fileFormat" ff -- printDebug "[addToCorpusWithForm] fileFormat" ff
-- TODO: update Node Corpus with the Lang addLanguageToCorpus cid l
-- n <- getNode cid
-- let n.wq_lang = l
-- saveNode n
limit' <- view $ hasConfig . gc_max_docs_parsers limit' <- view $ hasConfig . gc_max_docs_parsers
let limit = fromIntegral limit' :: Integer let limit = fromIntegral limit' :: Integer
...@@ -322,7 +317,7 @@ addToCorpusWithForm user cid (NewWithForm ft ff d l _n sel) jobHandle = do ...@@ -322,7 +317,7 @@ addToCorpusWithForm user cid (NewWithForm ft ff d l _n sel) jobHandle = do
_cid' <- flowCorpus user _cid' <- flowCorpus user
(Right [cid]) (Right [cid])
(Multi $ fromMaybe EN l) (Multi l)
(Just sel) (Just sel)
--(Just $ fromIntegral $ length docs, docsC') --(Just $ fromIntegral $ length docs, docsC')
(mCount, transPipe liftBase docsC') -- TODO fix number of docs (mCount, transPipe liftBase docsC') -- TODO fix number of docs
...@@ -378,12 +373,9 @@ addToCorpusWithFile :: (HasSettings env, FlowCmdM env err m, MonadJobStatus m) ...@@ -378,12 +373,9 @@ addToCorpusWithFile :: (HasSettings env, FlowCmdM env err m, MonadJobStatus m)
-> NewWithFile -> NewWithFile
-> JobHandle m -> JobHandle m
-> m () -> m ()
addToCorpusWithFile user cid nwf@(NewWithFile _d _l fName) jobHandle = do addToCorpusWithFile user cid nwf@(NewWithFile _d (withDefaultLanguage -> l) fName) jobHandle = do
-- TODO: update Node Corpus with the Lang addLanguageToCorpus cid l
-- n <- getNode cid
-- let n.wq_lang = l
-- saveNode n
printDebug "[addToCorpusWithFile] Uploading file to corpus: " cid printDebug "[addToCorpusWithFile] Uploading file to corpus: " cid
markStarted 1 jobHandle markStarted 1 jobHandle
......
{-# LANGUAGE TypeApplications #-}
module Gargantext.API.Node.Corpus.Update
( addLanguageToCorpus )
where
import Gargantext.Core
import Gargantext.Database.Action.Flow.Types
import Gargantext.Database.Admin.Types.Node
import Gargantext.Prelude
import Gargantext.Utils.Jobs
import Gargantext.Database.Admin.Types.Hyperdata.Corpus
import Gargantext.Database.Query.Table.Node
import Data.Proxy
import Control.Lens
import Gargantext.Database.Schema.Node (node_hyperdata)
import Gargantext.Database.Query.Table.Node.UpdateOpaleye (updateHyperdata)
import Control.Monad
-- | Updates the 'HyperdataCorpus' with the input 'Lang'.
addLanguageToCorpus :: (FlowCmdM env err m, MonadJobStatus m)
=> CorpusId
-> Lang
-> m ()
addLanguageToCorpus cId lang = do
hyperNode <- getNodeWith cId (Proxy @HyperdataCorpus)
let hyperNode' = hyperNode & over node_hyperdata (\corpus -> corpus { _hc_lang = lang })
void $ updateHyperdata cId hyperNode'
...@@ -17,6 +17,7 @@ module Gargantext.Core ...@@ -17,6 +17,7 @@ module Gargantext.Core
import Data.Aeson import Data.Aeson
import Data.Either(Either(Left)) import Data.Either(Either(Left))
import Data.Hashable (Hashable) import Data.Hashable (Hashable)
import Data.Maybe (fromMaybe)
import Data.Morpheus.Types (GQLType) import Data.Morpheus.Types (GQLType)
import Data.Swagger import Data.Swagger
import Data.Text (Text, pack) import Data.Text (Text, pack)
...@@ -58,6 +59,15 @@ data Lang = All ...@@ -58,6 +59,15 @@ data Lang = All
| ZH | ZH
deriving (Show, Eq, Ord, Enum, Bounded, Generic, GQLType) deriving (Show, Eq, Ord, Enum, Bounded, Generic, GQLType)
-- | Defaults to 'EN' in all those places where a language is mandatory,
-- but an optional one has been passed.
withDefaultLanguage :: Maybe Lang -> Lang
withDefaultLanguage = fromMaybe defaultLanguage
-- | The default 'Lang'.
defaultLanguage :: Lang
defaultLanguage = EN
instance ToJSON Lang instance ToJSON Lang
instance FromJSON Lang instance FromJSON Lang
instance ToSchema Lang where instance ToSchema Lang where
......
...@@ -22,16 +22,18 @@ module Gargantext.Database.Admin.Types.Hyperdata.Corpus ...@@ -22,16 +22,18 @@ module Gargantext.Database.Admin.Types.Hyperdata.Corpus
where where
import Gargantext.Prelude import Gargantext.Prelude
import Gargantext.Core (Lang) import Gargantext.Core (Lang, defaultLanguage)
import Gargantext.Database.Admin.Types.Hyperdata.CorpusField import Gargantext.Database.Admin.Types.Hyperdata.CorpusField
import Gargantext.Database.Admin.Types.Hyperdata.Prelude import Gargantext.Database.Admin.Types.Hyperdata.Prelude
import PUBMED.Types (APIKey) import PUBMED.Types (APIKey)
------------------------------------------------------------------------ ------------------------------------------------------------------------
data HyperdataCorpus = data HyperdataCorpus =
HyperdataCorpus { _hc_fields :: ![HyperdataField CorpusField] HyperdataCorpus { _hc_fields :: [HyperdataField CorpusField]
, _hc_pubmed_api_key :: Maybe APIKey , _hc_pubmed_api_key :: Maybe APIKey
, _hc_lang :: Maybe Lang -- | The language for the corpus. It defaults to
-- 'defaultLanguage' if we don't know which language it is.
, _hc_lang :: !Lang
} }
deriving (Generic) deriving (Generic)
...@@ -47,7 +49,7 @@ defaultHyperdataCorpus = ...@@ -47,7 +49,7 @@ defaultHyperdataCorpus =
(JsonField "Title" "Descr" "Bool query" "Authors") (JsonField "Title" "Descr" "Bool query" "Authors")
] ]
, _hc_pubmed_api_key = Nothing , _hc_pubmed_api_key = Nothing
, _hc_lang = Nothing , _hc_lang = defaultLanguage
} }
------------------------------------------------------------------------ ------------------------------------------------------------------------
......
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