-- TODO-EVENTS: InsertedNodes
-}
-{-# OPTIONS_GHC -fno-warn-orphans #-}
+{-# OPTIONS_GHC -fno-warn-orphans #-}
-{-# LANGUAGE ConstraintKinds #-}
-{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ConstrainedClassMethods #-}
{-# LANGUAGE ConstraintKinds #-}
-{-# LANGUAGE DeriveGeneric #-}
-{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE InstanceSigs #-}
-{-# LANGUAGE NoImplicitPrelude #-}
-{-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TemplateHaskell #-}
module Gargantext.Database.Action.Flow -- (flowDatabase, ngrams2list)
- ( FlowCmdM
+ ( DataText(..)
, getDataText
, flowDataText
+ , flow
, flowCorpusFile
, flowCorpus
, flowAnnuaire
+ , insertMasterDocs
+ , saveDocNgramsWith
, getOrMkRoot
, getOrMk_RootWithCorpus
, TermType(..)
, DataOrigin(..)
+ , allDataOrigins
+
+ , do_api
+ , indexAllDocumentsWithPosTag
)
where
import Control.Lens ((^.), view, _Just, makeLenses)
import Data.Aeson.TH (deriveJSON)
import Data.Either
+import Data.HashMap.Strict (HashMap)
+import Data.Hashable (Hashable)
import Data.List (concat)
import Data.Map (Map, lookup)
-import Data.Maybe (Maybe(..), catMaybes)
+import Data.Maybe (catMaybes)
import Data.Monoid
import Data.Swagger
-import Data.Text (Text, splitOn, intercalate)
+import qualified Data.Text as T
import Data.Traversable (traverse)
import Data.Tuple.Extra (first, second)
-import Debug.Trace (trace)
-import Gargantext.Core (Lang(..))
+import GHC.Generics (Generic)
+import System.FilePath (FilePath)
+import qualified Data.HashMap.Strict as HashMap
+import qualified Gargantext.Data.HashMap.Strict.Utils as HashMap
+import qualified Data.Map as Map
+
+import Gargantext.API.Admin.Orchestrator.Types (JobLog(..))
+import Gargantext.Core (Lang(..), PosTagAlgo(..))
+import Gargantext.Core.Ext.IMT (toSchoolName)
+import Gargantext.Core.Ext.IMTUser (readFile_Annuaire)
import Gargantext.Core.Flow.Types
-import Gargantext.Core.Types (Terms(..))
+import Gargantext.Core.Text
+import Gargantext.Core.Text.List.Group.WithStem ({-StopSize(..),-} GroupParams(..))
+import Gargantext.Core.Text.Corpus.Parsers (parseFile, FileFormat)
+import Gargantext.Core.Text.List (buildNgramsLists)
+import Gargantext.Core.Text.List.Social (FlowSocialListWith)
+import Gargantext.Core.Text.Terms
+import Gargantext.Core.Text.Terms.Mono.Stem.En (stemIt)
+import Gargantext.Core.Types (POS(NP))
import Gargantext.Core.Types.Individu (User(..))
import Gargantext.Core.Types.Main
+import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger)
import Gargantext.Database.Action.Flow.List
import Gargantext.Database.Action.Flow.Types
-import Gargantext.Database.Action.Flow.Utils (insertDocNgrams)
-import Gargantext.Database.Action.Query.Node
-import Gargantext.Database.Action.Query.Node.Contact -- (HyperdataContact(..), ContactWho(..))
-import Gargantext.Database.Action.Query.Node.Document.Insert -- (insertDocuments, ReturnId(..), addUniqIdsDoc, addUniqIdsContact, ToDbData(..))
-import Gargantext.Database.Action.Query.Tree.Root (getOrMkRoot, getOrMk_RootWithCorpus)
-import Gargantext.Database.Action.Search (searchInDatabase)
+import Gargantext.Database.Action.Flow.Utils (insertDocNgrams, DocumentIdWithNgrams(..))
+import Gargantext.Database.Action.Search (searchDocInDatabase)
import Gargantext.Database.Admin.Config (userMaster, corpusMasterName)
-import Gargantext.Database.Admin.Types.Errors (HasNodeError(..))
+import Gargantext.Database.Admin.Types.Hyperdata
import Gargantext.Database.Admin.Types.Node -- (HyperdataDocument(..), NodeType(..), NodeId, UserId, ListId, CorpusId, RootId, MasterCorpusId, MasterUserId)
-import Gargantext.Database.Admin.Utils (Cmd)
-import Gargantext.Database.Schema.Ngrams -- (insertNgrams, Ngrams(..), NgramsIndexed(..), indexNgrams, NgramsType(..), text2ngrams, ngramsTypeId)
-import Gargantext.Database.Schema.NodeNgrams (listInsertDb , getCgramsId)
-import Gargantext.Database.Schema.NodeNodeNgrams2 -- (NodeNodeNgrams2, insertNodeNodeNgrams2)
-import Gargantext.Ext.IMT (toSchoolName)
-import Gargantext.Core.Utils.Prefix (unPrefix, unPrefixSwagger)
-import Gargantext.Ext.IMTUser (deserialiseImtUsersFromFile)
+import Gargantext.Database.Prelude
+import Gargantext.Database.Query.Table.Ngrams
+import Gargantext.Database.Query.Table.Node
+import Gargantext.Database.Query.Table.Node.Document.Insert -- (insertDocuments, ReturnId(..), addUniqIdsDoc, addUniqIdsContact, ToDbData(..))
+import Gargantext.Database.Query.Table.Node.Error (HasNodeError(..))
+import Gargantext.Database.Query.Table.NodeNgrams (listInsertDb , getCgramsId)
+import Gargantext.Database.Query.Table.NodeNodeNgrams2
+import Gargantext.Database.Query.Tree.Root (getOrMkRoot, getOrMk_RootWithCorpus)
+import Gargantext.Database.Schema.Node (NodePoly(..), node_id)
+import Gargantext.Database.Types
import Gargantext.Prelude
-import Gargantext.Text.Corpus.Parsers (parseFile, FileFormat)
-import Gargantext.Text.List (buildNgramsLists,StopSize(..))
-import qualified Gargantext.Text.Terms as GTT (TermType(..), tt_lang, extractTerms, uniText)
-import Gargantext.Text.Terms.Eleve (buildTries, toToken)
-import Gargantext.Text.Terms.Mono.Stem.En (stemIt)
-import GHC.Generics (Generic)
-import Prelude (String)
-import System.FilePath (FilePath)
-import qualified Data.List as List
-import qualified Data.Map as Map
-import qualified Data.Text as Text
-import qualified Gargantext.Database.Action.Query.Node.Document.Add as Doc (add)
-import qualified Gargantext.Text.Corpus.API as API
+import Gargantext.Prelude.Crypto.Hash (Hash)
+import qualified Gargantext.Core.Text.Corpus.API as API
+import qualified Gargantext.Database.Query.Table.Node.Document.Add as Doc (add)
+------------------------------------------------------------------------
+-- Imports for upgrade function
+import Gargantext.Database.Query.Tree.Root (getRootId)
+import Gargantext.Database.Query.Tree (findNodesId)
+import qualified Data.List as List
------------------------------------------------------------------------
-- TODO use internal with API name (could be old data)
-data DataOrigin = Internal Gargantext
- | External API.ExternalAPIs
+data DataOrigin = InternalOrigin { _do_api :: API.ExternalAPIs }
+ | ExternalOrigin { _do_api :: API.ExternalAPIs }
-- TODO Web
+ deriving (Generic, Eq)
+
+makeLenses ''DataOrigin
+deriveJSON (unPrefix "_do_") ''DataOrigin
+instance ToSchema DataOrigin where
+ declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_do_")
+
+allDataOrigins :: [DataOrigin]
+allDataOrigins = map InternalOrigin API.externalAPIs
+ <> map ExternalOrigin API.externalAPIs
+---------------
data DataText = DataOld ![NodeId]
| DataNew ![[HyperdataDocument]]
-
-- TODO use the split parameter in config file
getDataText :: FlowCmdM env err m
=> DataOrigin
-> API.Query
-> Maybe API.Limit
-> m DataText
-getDataText (External api) la q li = liftBase $ DataNew
+getDataText (ExternalOrigin api) la q li = liftBase $ DataNew
<$> splitEvery 500
<$> API.get api (_tt_lang la) q li
-getDataText Gargantext la q li = do
+
+getDataText (InternalOrigin _) _la q _li = do
(_masterUserId, _masterRootId, cId) <- getOrMk_RootWithCorpus
(UserName userMaster)
(Left "")
(Nothing :: Maybe HyperdataCorpus)
- ids <- map fst <$> searchInDatabase cId (stemIt q)
+ ids <- map fst <$> searchDocInDatabase cId (stemIt q)
pure $ DataOld ids
-------------------------------------------------------------------------------
--- API for termType
-data TermType lang
- = Mono { _tt_lang :: lang }
- | Multi { _tt_lang :: lang }
- | MonoMulti { _tt_lang :: lang }
- | Unsupervised { _tt_lang :: lang
- , _tt_windowSize :: Int
- , _tt_ngramsSize :: Int
- }
- deriving Generic
-
--- | GTT.TermType as a complex type in Unsupervised configuration that is not needed
--- for the API use
-tta2tt :: TermType lang -> GTT.TermType lang
-tta2tt (Mono l) = GTT.Mono l
-tta2tt (Multi l) = GTT.Multi l
-tta2tt (MonoMulti l) = GTT.MonoMulti l
-tta2tt (Unsupervised la w ng) = GTT.Unsupervised la w ng Nothing
-
-makeLenses ''TermType
-deriveJSON (unPrefix "_tt_") ''TermType
-
-instance (ToSchema a) => ToSchema (TermType a) where
- declareNamedSchema = genericDeclareNamedSchema (unPrefixSwagger "_tta_")
-
-
-flowDataText :: FlowCmdM env err m
- => User
- -> DataText
- -> TermType Lang
- -> CorpusId
- -> m CorpusId
-flowDataText u (DataOld ids) tt cid = flowCorpusUser (_tt_lang tt) u (Right [cid]) corpusType ids
+flowDataText :: ( FlowCmdM env err m
+ )
+ => User
+ -> DataText
+ -> TermType Lang
+ -> CorpusId
+ -> Maybe FlowSocialListWith
+ -> (JobLog -> m ())
+ -> m CorpusId
+flowDataText u (DataOld ids) tt cid mfslw _ = flowCorpusUser (_tt_lang tt) u (Right [cid]) corpusType ids mfslw
where
corpusType = (Nothing :: Maybe HyperdataCorpus)
-flowDataText u (DataNew txt) tt cid = flowCorpus u (Right [cid]) tt txt
+flowDataText u (DataNew txt) tt cid mfslw logStatus = flowCorpus u (Right [cid]) tt mfslw txt logStatus
------------------------------------------------------------------------
-- TODO use proxy
-flowAnnuaire :: FlowCmdM env err m
+flowAnnuaire :: (FlowCmdM env err m)
=> User
-> Either CorpusName [CorpusId]
-> (TermType Lang)
-> FilePath
+ -> (JobLog -> m ())
-> m AnnuaireId
-flowAnnuaire u n l filePath = do
- docs <- liftBase $ (( splitEvery 500 <$> deserialiseImtUsersFromFile filePath) :: IO [[HyperdataContact]])
- flow (Nothing :: Maybe HyperdataAnnuaire) u n l docs
+flowAnnuaire u n l filePath logStatus = do
+ docs <- liftBase $ (( splitEvery 500 <$> readFile_Annuaire filePath) :: IO [[HyperdataContact]])
+ flow (Nothing :: Maybe HyperdataAnnuaire) u n l Nothing docs logStatus
------------------------------------------------------------------------
-flowCorpusFile :: FlowCmdM env err m
+flowCorpusFile :: (FlowCmdM env err m)
=> User
-> Either CorpusName [CorpusId]
-> Limit -- Limit the number of docs (for dev purpose)
-> TermType Lang -> FileFormat -> FilePath
+ -> Maybe FlowSocialListWith
+ -> (JobLog -> m ())
-> m CorpusId
-flowCorpusFile u n l la ff fp = do
- docs <- liftBase ( splitEvery 500
- <$> take l
- <$> parseFile ff fp
- )
- flowCorpus u n la (map (map toHyperdataDocument) docs)
+flowCorpusFile u n l la ff fp mfslw logStatus = do
+ eParsed <- liftBase $ parseFile ff fp
+ case eParsed of
+ Right parsed -> do
+ let docs = splitEvery 500 $ take l parsed
+ flowCorpus u n la mfslw (map (map toHyperdataDocument) docs) logStatus
+ Left e -> panic $ "Error: " <> (T.pack e)
------------------------------------------------------------------------
-- | TODO improve the needed type to create/update a corpus
=> User
-> Either CorpusName [CorpusId]
-> TermType Lang
+ -> Maybe FlowSocialListWith
-> [[a]]
+ -> (JobLog -> m ())
-> m CorpusId
flowCorpus = flow (Nothing :: Maybe HyperdataCorpus)
-flow :: (FlowCmdM env err m, FlowCorpus a, MkCorpus c)
- => Maybe c
- -> User
- -> Either CorpusName [CorpusId]
- -> TermType Lang
- -> [[a]]
- -> m CorpusId
-flow c u cn la docs = do
- let la' = tta2tt la
- ids <- traverse (insertMasterDocs c la') docs
- flowCorpusUser (la' ^. GTT.tt_lang) u cn c (concat ids)
+flow :: ( FlowCmdM env err m
+ , FlowCorpus a
+ , MkCorpus c
+ )
+ => Maybe c
+ -> User
+ -> Either CorpusName [CorpusId]
+ -> TermType Lang
+ -> Maybe FlowSocialListWith
+ -> [[a]]
+ -> (JobLog -> m ())
+ -> m CorpusId
+flow c u cn la mfslw docs logStatus = do
+ -- TODO if public insertMasterDocs else insertUserDocs
+ ids <- traverse (\(idx, doc) -> do
+ id <- insertMasterDocs c la doc
+ logStatus JobLog { _scst_succeeded = Just $ 1 + idx
+ , _scst_failed = Just 0
+ , _scst_remaining = Just $ length docs - idx
+ , _scst_events = Just []
+ }
+ pure id
+ ) (zip [1..] docs)
+ flowCorpusUser (la ^. tt_lang) u cn c (concat ids) mfslw
------------------------------------------------------------------------
-flowCorpusUser :: (FlowCmdM env err m, MkCorpus c)
+flowCorpusUser :: ( FlowCmdM env err m
+ , MkCorpus c
+ )
=> Lang
-> User
-> Either CorpusName [CorpusId]
-> Maybe c
-> [NodeId]
+ -> Maybe FlowSocialListWith
-> m CorpusId
-flowCorpusUser l user corpusName ctype ids = do
+flowCorpusUser l user corpusName ctype ids mfslw = do
-- User Flow
(userId, _rootId, userCorpusId) <- getOrMk_RootWithCorpus user corpusName ctype
+ -- NodeTexts is first
+ _tId <- insertDefaultNode NodeTexts userCorpusId userId
+ -- printDebug "NodeTexts: " tId
+
+ -- NodeList is second
listId <- getOrMkList userCorpusId userId
- _cooc <- mkNode NodeListCooc listId userId
+ -- _cooc <- insertDefaultNode NodeListCooc listId userId
-- TODO: check if present already, ignore
_ <- Doc.add userCorpusId ids
- _tId <- mkNode NodeTexts userCorpusId userId
- -- printDebug "Node Text Id" tId
+ -- printDebug "Node Text Ids:" tId
-- User List Flow
- (masterUserId, _masterRootId, masterCorpusId) <- getOrMk_RootWithCorpus (UserName userMaster) (Left "") ctype
- ngs <- buildNgramsLists l 2 3 (StopSize 3) userCorpusId masterCorpusId
+ (masterUserId, _masterRootId, masterCorpusId)
+ <- getOrMk_RootWithCorpus (UserName userMaster) (Left "") ctype
+
+ --let gp = (GroupParams l 2 3 (StopSize 3))
+ let gp = GroupWithPosTag l CoreNLP HashMap.empty
+ ngs <- buildNgramsLists user userCorpusId masterCorpusId mfslw gp
+
_userListId <- flowList_DbRepo listId ngs
_mastListId <- getOrMkList masterCorpusId masterUserId
-- _ <- insertOccsUpdates userCorpusId mastListId
-- printDebug "userListId" userListId
-- User Graph Flow
- _ <- mkDashboard userCorpusId userId
- _ <- mkGraph userCorpusId userId
+ _ <- insertDefaultNode NodeDashboard userCorpusId userId
+ _ <- insertDefaultNode NodeGraph userCorpusId userId
--_ <- mkPhylo userCorpusId userId
-
-- Annuaire Flow
-- _ <- mkAnnuaire rootUserId userId
pure userCorpusId
, MkCorpus c
)
=> Maybe c
- -> GTT.TermType Lang
+ -> TermType Lang
-> [a]
-> m [DocId]
insertMasterDocs c lang hs = do
(masterUserId, _, masterCorpusId) <- getOrMk_RootWithCorpus (UserName userMaster) (Left corpusMasterName) c
-
- -- TODO Type NodeDocumentUnicised
- let docs = map addUniqId hs
- ids <- insertDb masterUserId masterCorpusId docs
- let
- ids' = map reId ids
- documentsWithId = mergeData (toInserted ids) (Map.fromList $ map viewUniqId' docs)
+ (ids', documentsWithId) <- insertDocs masterUserId masterCorpusId (map (toNode masterUserId masterCorpusId) hs )
+ _ <- Doc.add masterCorpusId ids'
-- TODO
-- create a corpus with database name (CSV or PubMed)
-- add documents to the corpus (create node_node link)
-- this will enable global database monitoring
-- maps :: IO Map Ngrams (Map NgramsType (Map NodeId Int))
- maps <- mapNodeIdNgrams
- <$> documentIdWithNgrams (extractNgramsT $ withLang lang documentsWithId) documentsWithId
+ mapNgramsDocs' :: HashMap ExtractedNgrams (Map NgramsType (Map NodeId Int))
+ <- mapNodeIdNgrams
+ <$> documentIdWithNgrams
+ (extractNgramsT $ withLang lang documentsWithId)
+ documentsWithId
+
+ lId <- getOrMkList masterCorpusId masterUserId
+ _ <- saveDocNgramsWith lId mapNgramsDocs'
+
+ -- _cooc <- insertDefaultNode NodeListCooc lId masterUserId
+ pure ids'
+
+saveDocNgramsWith :: ( FlowCmdM env err m)
+ => ListId
+ -> HashMap ExtractedNgrams (Map NgramsType (Map NodeId Int))
+ -> m ()
+saveDocNgramsWith lId mapNgramsDocs' = do
+ terms2id <- insertExtractedNgrams $ HashMap.keys mapNgramsDocs'
+ let mapNgramsDocs = HashMap.mapKeys extracted2ngrams mapNgramsDocs'
- terms2id <- insertNgrams $ Map.keys maps
-- to be removed
- let indexedNgrams = Map.mapKeys (indexNgrams terms2id) maps
+ let indexedNgrams = HashMap.mapKeys (indexNgrams terms2id) mapNgramsDocs
-- new
- lId <- getOrMkList masterCorpusId masterUserId
mapCgramsId <- listInsertDb lId toNodeNgramsW'
- $ map (first _ngramsTerms . second Map.keys)
- $ Map.toList maps
+ $ map (first _ngramsTerms . second Map.keys)
+ $ HashMap.toList mapNgramsDocs
+
-- insertDocNgrams
_return <- insertNodeNodeNgrams2
$ catMaybes [ NodeNodeNgrams2 <$> Just nId
- <*> getCgramsId mapCgramsId ngrams_type (_ngramsTerms terms)
+ <*> getCgramsId mapCgramsId ngrams_type (_ngramsTerms terms'')
<*> Just (fromIntegral w :: Double)
- | (terms, mapNgramsTypes) <- Map.toList maps
+ | (terms'', mapNgramsTypes) <- HashMap.toList mapNgramsDocs
, (ngrams_type, mapNodeIdWeight) <- Map.toList mapNgramsTypes
- , (nId, w) <- Map.toList mapNodeIdWeight
+ , (nId, w) <- Map.toList mapNodeIdWeight
]
-
- _ <- Doc.add masterCorpusId ids'
- _cooc <- mkNode NodeListCooc lId masterUserId
-- to be removed
_ <- insertDocNgrams lId indexedNgrams
- pure ids'
+ pure ()
-withLang :: HasText a
- => GTT.TermType Lang
- -> [DocumentWithId a]
- -> GTT.TermType Lang
-withLang (GTT.Unsupervised l n s m) ns = GTT.Unsupervised l n s m'
- where
- m' = case m of
- Nothing -> trace ("buildTries here" :: String)
- $ Just
- $ buildTries n ( fmap toToken $ GTT.uniText
- $ Text.intercalate " . "
- $ List.concat
- $ map hasText ns
- )
- just_m -> just_m
-withLang l _ = l
+------------------------------------------------------------------------
+-- TODO Type NodeDocumentUnicised
+insertDocs :: ( FlowCmdM env err m
+ -- , FlowCorpus a
+ , FlowInsertDB a
+ )
+ => UserId
+ -> CorpusId
+ -> [a]
+ -> m ([DocId], [Indexed NodeId a])
+insertDocs uId cId hs = do
+ let docs = map addUniqId hs
+ newIds <- insertDb uId cId docs
+ -- printDebug "newIds" newIds
+ let
+ newIds' = map reId newIds
+ documentsWithId = mergeData (toInserted newIds) (Map.fromList $ map viewUniqId' docs)
+ _ <- Doc.add cId newIds'
+ pure (newIds', documentsWithId)
------------------------------------------------------------------------
viewUniqId' :: UniqId a
=> a
- -> (HashId, a)
+ -> (Hash, a)
viewUniqId' d = maybe err (\h -> (h,d)) (view uniqId d)
where
err = panic "[ERROR] Database.Flow.toInsert"
toInserted :: [ReturnId]
- -> Map HashId ReturnId
+ -> Map Hash ReturnId
toInserted =
- Map.fromList . map (\r -> (reUniqId r, r) )
+ Map.fromList . map (\r -> (reUniqId r, r) )
. filter (\r -> reInserted r == True)
-mergeData :: Map HashId ReturnId
- -> Map HashId a
- -> [DocumentWithId a]
+mergeData :: Map Hash ReturnId
+ -> Map Hash a
+ -> [Indexed NodeId a]
mergeData rs = catMaybes . map toDocumentWithId . Map.toList
where
toDocumentWithId (sha,hpd) =
- DocumentWithId <$> fmap reId (lookup sha rs)
- <*> Just hpd
+ Indexed <$> fmap reId (lookup sha rs)
+ <*> Just hpd
------------------------------------------------------------------------
+------------------------------------------------------------------------
+------------------------------------------------------------------------
+documentIdWithNgrams :: HasNodeError err
+ => (a
+ -> Cmd err (HashMap b (Map NgramsType Int)))
+ -> [Indexed NodeId a]
+ -> Cmd err [DocumentIdWithNgrams a b]
+documentIdWithNgrams f = traverse toDocumentIdWithNgrams
+ where
+ toDocumentIdWithNgrams d = do
+ e <- f $ _unIndex d
+ pure $ DocumentIdWithNgrams d e
+
-instance HasText HyperdataContact
+-- | TODO check optimization
+mapNodeIdNgrams :: (Ord b, Hashable b)
+ => [DocumentIdWithNgrams a b]
+ -> HashMap b
+ (Map NgramsType
+ (Map NodeId Int)
+ )
+mapNodeIdNgrams = HashMap.unionsWith (Map.unionWith (Map.unionWith (+))) . fmap f
where
- hasText = undefined
+ f :: DocumentIdWithNgrams a b
+ -> HashMap b (Map NgramsType (Map NodeId Int))
+ f d = fmap (fmap (Map.singleton nId)) $ documentNgrams d
+ where
+ nId = _index $ documentWithId d
+
+------------------------------------------------------------------------
instance ExtractNgramsT HyperdataContact
where
- extractNgramsT l hc = filterNgramsT 255 <$> extract l hc
+ extractNgramsT l hc = HashMap.mapKeys (cleanExtractedNgrams 255) <$> extract l hc
where
- extract :: GTT.TermType Lang -> HyperdataContact
- -> Cmd err (Map Ngrams (Map NgramsType Int))
+ extract :: TermType Lang -> HyperdataContact
+ -> Cmd err (HashMap ExtractedNgrams (Map NgramsType Int))
extract _l hc' = do
let authors = map text2ngrams
- $ maybe ["Nothing"] (\a -> [a])
- $ view (hc_who . _Just . cw_lastName) hc'
+ $ maybe ["Nothing"] (\a -> [a])
+ $ view (hc_who . _Just . cw_lastName) hc'
- pure $ Map.fromList $ [(a', Map.singleton Authors 1) | a' <- authors ]
+ pure $ HashMap.fromList $ [(SimpleNgrams a', Map.singleton Authors 1) | a' <- authors ]
-instance HasText HyperdataDocument
- where
- hasText h = catMaybes [ _hyperdataDocument_title h
- , _hyperdataDocument_abstract h
- ]
instance ExtractNgramsT HyperdataDocument
where
- extractNgramsT :: GTT.TermType Lang
+ extractNgramsT :: TermType Lang
-> HyperdataDocument
- -> Cmd err (Map Ngrams (Map NgramsType Int))
- extractNgramsT lang hd = filterNgramsT 255 <$> extractNgramsT' lang hd
+ -> Cmd err (HashMap ExtractedNgrams (Map NgramsType Int))
+ extractNgramsT lang hd = HashMap.mapKeys (cleanExtractedNgrams 255) <$> extractNgramsT' lang hd
where
- extractNgramsT' :: GTT.TermType Lang
+ extractNgramsT' :: TermType Lang
-> HyperdataDocument
- -> Cmd err (Map Ngrams (Map NgramsType Int))
+ -> Cmd err (HashMap ExtractedNgrams (Map NgramsType Int))
extractNgramsT' lang' doc = do
let source = text2ngrams
$ maybe "Nothing" identity
- $ _hyperdataDocument_source doc
+ $ _hd_source doc
institutes = map text2ngrams
- $ maybe ["Nothing"] (map toSchoolName . (splitOn ", "))
- $ _hyperdataDocument_institutes doc
+ $ maybe ["Nothing"] (map toSchoolName . (T.splitOn ", "))
+ $ _hd_institutes doc
authors = map text2ngrams
- $ maybe ["Nothing"] (splitOn ", ")
- $ _hyperdataDocument_authors doc
+ $ maybe ["Nothing"] (T.splitOn ", ")
+ $ _hd_authors doc
- terms' <- map text2ngrams
- <$> map (intercalate " " . _terms_label)
+ terms' <- map (enrichedTerms (lang' ^. tt_lang) CoreNLP NP)
<$> concat
- <$> liftBase (GTT.extractTerms lang' $ hasText doc)
+ <$> liftBase (extractTerms lang' $ hasText doc)
- pure $ Map.fromList $ [(source, Map.singleton Sources 1)]
- <> [(i', Map.singleton Institutes 1) | i' <- institutes ]
- <> [(a', Map.singleton Authors 1) | a' <- authors ]
- <> [(t', Map.singleton NgramsTerms 1) | t' <- terms' ]
+ pure $ HashMap.fromList
+ $ [(SimpleNgrams source, Map.singleton Sources 1) ]
+ <> [(SimpleNgrams i', Map.singleton Institutes 1) | i' <- institutes ]
+ <> [(SimpleNgrams a', Map.singleton Authors 1) | a' <- authors ]
+ <> [(EnrichedNgrams t', Map.singleton NgramsTerms 1) | t' <- terms' ]
-filterNgramsT :: Int -> Map Ngrams (Map NgramsType Int)
- -> Map Ngrams (Map NgramsType Int)
-filterNgramsT s ms = Map.fromList $ map (\a -> filter' s a) $ Map.toList ms
+instance (ExtractNgramsT a, HasText a) => ExtractNgramsT (Node a)
where
- filter' s' (ng@(Ngrams t n),y) = case (Text.length t) < s' of
- True -> (ng,y)
- False -> (Ngrams (Text.take s' t) n , y)
+ extractNgramsT l (Node _ _ _ _ _ _ _ h) = extractNgramsT l h
-
-documentIdWithNgrams :: HasNodeError err
- => (a
- -> Cmd err (Map Ngrams (Map NgramsType Int)))
- -> [DocumentWithId a]
- -> Cmd err [DocumentIdWithNgrams a]
-documentIdWithNgrams f = traverse toDocumentIdWithNgrams
+instance HasText a => HasText (Node a)
where
- toDocumentIdWithNgrams d = do
- e <- f $ documentData d
- pure $ DocumentIdWithNgrams d e
+ hasText (Node _ _ _ _ _ _ _ h) = hasText h
+
+
+
+-- | TODO putelsewhere
+-- | Upgrade function
+-- Suppose all documents are English (this is the case actually)
+indexAllDocumentsWithPosTag :: FlowCmdM env err m => m ()
+indexAllDocumentsWithPosTag = do
+ rootId <- getRootId (UserName userMaster)
+ corpusIds <- findNodesId rootId [NodeCorpus]
+ docs <- List.concat <$> mapM getDocumentsWithParentId corpusIds
+
+ _ <- mapM extractInsert (splitEvery 1000 docs)
+
+ pure ()
+
+extractInsert :: FlowCmdM env err m => [Node HyperdataDocument] -> m ()
+extractInsert docs = do
+ let documentsWithId = map (\doc -> Indexed (doc ^. node_id) doc) docs
+
+ mapNgramsDocs' <- mapNodeIdNgrams
+ <$> documentIdWithNgrams
+ (extractNgramsT $ withLang (Multi EN) documentsWithId)
+ documentsWithId
+
+ _ <- insertExtractedNgrams $ HashMap.keys mapNgramsDocs'
+
+ pure ()
+