2 Module : Gargantext.Database.Learn
3 Description : Learn Small Data Analytics with big data connection (DB)
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
12 {-# LANGUAGE NoImplicitPrelude #-}
13 {-# LANGUAGE QuasiQuotes #-}
14 {-# LANGUAGE RankNTypes #-}
15 {-# LANGUAGE OverloadedStrings #-}
16 {-# LANGUAGE FlexibleContexts #-}
17 {-# LANGUAGE MonoLocalBinds #-}
19 module Gargantext.Database.Learn where
21 import Data.Text (Text)
22 import Data.Tuple (snd)
24 import Gargantext.Database.Facet
25 import Gargantext.Database.Types.Node
26 import Gargantext.Prelude
27 import Gargantext.Text.Learn
28 import qualified Data.List as List
29 import qualified Data.Text as Text
30 import Gargantext.Database.Schema.NodeNode (nodesToFavorite)
31 import Gargantext.API.Node (delDocs, Documents(..))
32 --import Gargantext.Database.Utils (Cmd)
33 --import Gargantext.Database.Schema.Node (HasNodeError)
35 import Gargantext.API.Settings
36 import Gargantext.Database.Flow (FlowCmdM)
38 data FavOrTrash = IsFav | IsTrash
43 --moreLike :: FlowCmdM env error m => FavOrTrash -> CorpusId -> m (Events Bool, [FacetDoc])
46 moreLike :: FlowCmdM DevEnv GargError m => FavOrTrash -> CorpusId -> m [FacetDoc]
48 priors <- getPriors ft cId
49 moreLikeWith priors ft cId
52 ---------------------------------------------------------------------------
53 getPriors :: FlowCmdM DevEnv GargError m => FavOrTrash -> CorpusId -> m (Events Bool)
55 docs_trash <- runViewDocuments cId True Nothing Nothing Nothing
57 docs_fav <- filter (\(FacetDoc _ _ _ _ f _) -> f == True)
58 <$> runViewDocuments cId False Nothing Nothing Nothing
61 let priors = priorEventsWith text (fav2bool ft) ( List.zip (repeat False) docs_fav
62 <> List.zip (repeat True ) docs_trash
67 moreLikeWith :: FlowCmdM DevEnv GargError m => Events Bool -> FavOrTrash -> CorpusId -> m [FacetDoc]
68 moreLikeWith priors ft cId = do
70 docs_test <- filter (\(FacetDoc _ _ _ _ f _) -> f == False)
71 <$> runViewDocuments cId False Nothing Nothing Nothing
74 $ filter ((==) (Just $ not $ fav2bool ft) . snd)
75 $ map (\f -> (f, detectDefaultWithPriors text priors f)) docs_test
79 ---------------------------------------------------------------------------
80 fav2bool :: FavOrTrash -> Bool
81 fav2bool ft = if (==) ft IsFav then True else False
84 text :: FacetDoc -> Text
85 text (FacetDoc _ _ _ h _ _) = title <> "" <> Text.take 100 abstr
87 title = maybe "" identity (_hyperdataDocument_title h)
88 abstr = maybe "" identity (_hyperdataDocument_abstract h)
90 ---------------------------------------------------------------------------
93 apply :: (FlowCmdM DevEnv GargError m) => FavOrTrash -> CorpusId -> [NodeId] -> m [Int]
94 apply favTrash cId ns = case favTrash of
95 IsFav -> nodesToFavorite $ map (\n -> (cId, n, True)) ns
96 IsTrash -> delDocs cId (Documents ns)
99 moreLikeAndApply :: FlowCmdM DevEnv GargError m => FavOrTrash -> CorpusId -> m [Int]
100 moreLikeAndApply ft cId = do
101 priors <- getPriors ft cId
102 moreLikeWithAndApply priors ft cId
105 moreLikeWithAndApply :: FlowCmdM DevEnv GargError m => Events Bool -> FavOrTrash -> CorpusId -> m [Int]
106 moreLikeWithAndApply priors ft cId = do
107 ids <- map facetDoc_id <$> moreLikeWith priors ft cId