2 Module : Gargantext.API.Ngrams
3 Description : Server API
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
13 -- get data of NgramsTable
14 -- post :: update NodeNodeNgrams
17 get ngrams filtered by NgramsType
22 {-# LANGUAGE DataKinds #-}
23 {-# LANGUAGE DeriveGeneric #-}
24 {-# LANGUAGE NoImplicitPrelude #-}
25 {-# LANGUAGE OverloadedStrings #-}
26 {-# LANGUAGE TemplateHaskell #-}
27 {-# LANGUAGE TypeOperators #-}
28 {-# LANGUAGE FlexibleInstances #-}
30 module Gargantext.API.Ngrams
33 -- import Gargantext.Database.User (UserId)
34 --import Data.Map.Strict.Patch (Patch, replace, fromList)
35 --import Data.Maybe (catMaybes)
36 --import qualified Data.Map.Strict as DM
37 --import qualified Data.Set as Set
38 import Control.Lens (view)
39 import Data.Aeson (FromJSON, ToJSON)
40 import Data.Aeson.TH (deriveJSON)
41 import Data.Either(Either(Left))
42 import Data.List (concat)
44 import Data.Swagger (ToSchema, ToParamSchema)
45 import Data.Text (Text)
46 import Database.PostgreSQL.Simple (Connection)
47 import GHC.Generics (Generic)
48 import Gargantext.Core.Types (node_id)
49 import Gargantext.Core.Types.Main (Tree(..))
50 import Gargantext.Core.Utils.Prefix (unPrefix)
51 import Gargantext.Database.Ngrams (NgramsId)
52 import Gargantext.Database.Node (getListsWithParentId)
53 -- import Gargantext.Database.NodeNgram -- (NodeNgram(..), NodeNgram, updateNodeNgrams, NodeNgramPoly)
54 import Gargantext.Database.NodeNgramsNgrams -- (NodeNgramsNgramsPoly(NodeNgramsNgrams))
55 import Gargantext.Prelude
56 import Gargantext.Text.List.Types (ListType(..), ListId, ListTypeId) --,listTypeId )
57 import Prelude (Enum, Bounded, minBound, maxBound)
58 import Servant hiding (Patch)
59 import Test.QuickCheck (elements)
60 import Test.QuickCheck.Arbitrary (Arbitrary, arbitrary)
61 -- import qualified Data.Set as Set
63 ------------------------------------------------------------------------
64 --data FacetFormat = Table | Chart
65 data TabType = Docs | Terms | Sources | Authors | Trash
66 deriving (Generic, Enum, Bounded)
68 instance FromHttpApiData TabType
70 parseUrlPiece "Docs" = pure Docs
71 parseUrlPiece "Terms" = pure Terms
72 parseUrlPiece "Sources" = pure Sources
73 parseUrlPiece "Authors" = pure Authors
74 parseUrlPiece "Trash" = pure Trash
75 parseUrlPiece _ = Left "Unexpected value of TabType"
77 instance ToParamSchema TabType
78 instance ToJSON TabType
79 instance FromJSON TabType
80 instance ToSchema TabType
81 instance Arbitrary TabType
83 arbitrary = elements [minBound .. maxBound]
85 ------------------------------------------------------------------------
87 NgramsElement { _ne_ngrams :: Text
88 , _ne_list :: ListType
89 , _ne_occurrences :: Int
91 deriving (Ord, Eq, Show, Generic)
92 $(deriveJSON (unPrefix "_ne_") ''NgramsElement)
94 instance ToSchema NgramsElement
95 instance Arbitrary NgramsElement where
96 arbitrary = elements [NgramsElement "sport" StopList 1]
98 ------------------------------------------------------------------------
99 data NgramsTable = NgramsTable { _ngramsTable :: [Tree NgramsElement] }
100 deriving (Ord, Eq, Generic)
101 $(deriveJSON (unPrefix "_") ''NgramsTable)
103 instance Arbitrary NgramsTable where
104 arbitrary = NgramsTable <$> arbitrary
107 instance Arbitrary (Tree NgramsElement) where
108 arbitrary = elements [ TreeN (NgramsElement "animal" GraphList 1)
109 [TreeN (NgramsElement "dog" GraphList 3) []
110 , TreeN (NgramsElement "object" CandidateList 2) []
111 , TreeN (NgramsElement "cat" GraphList 1) []
112 , TreeN (NgramsElement "nothing" StopList 4) []
114 , TreeN (NgramsElement "plant" GraphList 3)
115 [TreeN (NgramsElement "flower" GraphList 3) []
116 , TreeN (NgramsElement "moon" CandidateList 1) []
117 , TreeN (NgramsElement "cat" GraphList 2) []
118 , TreeN (NgramsElement "sky" StopList 1) []
121 instance ToSchema NgramsTable
123 ------------------------------------------------------------------------
124 -- On the Client side:
125 --data Action = InGroup NgramsId NgramsId
126 -- | OutGroup NgramsId NgramsId
127 -- | SetListType NgramsId ListType
130 NgramsPatch { _np_list_types :: ListType -- TODO Map UserId ListType
131 , _np_add_children :: Set NgramsElement
132 , _np_rem_children :: Set NgramsElement
134 deriving (Ord, Eq, Show, Generic)
135 $(deriveJSON (unPrefix "_np_") ''NgramsPatch)
137 instance ToSchema NgramsPatch
139 instance Arbitrary NgramsPatch where
140 arbitrary = NgramsPatch <$> arbitrary <*> arbitrary <*> arbitrary
145 NgramsIdPatch { _nip_ngramsId :: NgramsElement
146 , _nip_ngramsPatch :: NgramsPatch
148 deriving (Ord, Eq, Show, Generic)
150 $(deriveJSON (unPrefix "_nip_") ''NgramsIdPatch)
152 instance ToSchema NgramsIdPatch
154 instance Arbitrary NgramsIdPatch where
155 arbitrary = NgramsIdPatch <$> arbitrary <*> arbitrary
159 data NgramsIdPatchs =
160 NgramsIdPatchs { _nip_ngramsIdPatchs :: [NgramsIdPatch] }
161 deriving (Ord, Eq, Show, Generic)
162 $(deriveJSON (unPrefix "_nip_") ''NgramsIdPatchs)
163 instance ToSchema NgramsIdPatchs
164 instance Arbitrary NgramsIdPatchs where
165 arbitrary = NgramsIdPatchs <$> arbitrary
167 ------------------------------------------------------------------------
168 ------------------------------------------------------------------------
171 data Versioned a = Versioned
172 { _v_version :: Version
178 -- TODO sequencs of modifications (Patchs)
179 type NgramsIdPatch = Patch NgramsId NgramsPatch
181 ngramsPatch :: Int -> NgramsPatch
182 ngramsPatch n = NgramsPatch (DM.fromList [(1, StopList)]) (Set.fromList [n]) Set.empty
184 toEdit :: NgramsId -> NgramsPatch -> Edit NgramsId NgramsPatch
185 toEdit n p = Edit n p
186 ngramsIdPatch :: Patch NgramsId NgramsPatch
187 ngramsIdPatch = fromList $ catMaybes $ reverse [ replace (1::NgramsId) (Just $ ngramsPatch 1) Nothing
188 , replace (1::NgramsId) Nothing (Just $ ngramsPatch 2)
189 , replace (2::NgramsId) Nothing (Just $ ngramsPatch 2)
192 -- applyPatchBack :: Patch -> IO Patch
193 -- isEmptyPatch = Map.all (\x -> Set.isEmpty (add_children x) && Set.isEmpty ... )
195 ------------------------------------------------------------------------
196 ------------------------------------------------------------------------
197 ------------------------------------------------------------------------
199 type TableNgramsApi = Summary " Table Ngrams API Change"
200 :> QueryParam "list" ListId
201 :> ReqBody '[JSON] NgramsIdPatchs
202 :> Put '[JSON] NgramsIdPatchsBack
204 type TableNgramsApiGet = Summary " Table Ngrams API Get"
205 :> QueryParam "ngramsType" TabType
206 :> QueryParam "list" ListId
207 :> Get '[JSON] NgramsTable
209 type NgramsIdPatchsFeed = NgramsIdPatchs
210 type NgramsIdPatchsBack = NgramsIdPatchs
213 defaultList :: Connection -> CorpusId -> IO ListId
214 defaultList c cId = view node_id <$> maybe (panic errorMessage) identity
216 <$> getListsWithParentId c cId
218 errorMessage = "Gargantext.API.Ngrams.defaultList: no list found"
220 toLists :: ListId -> NgramsIdPatchs -> [(ListId, NgramsId, ListTypeId)]
221 toLists lId np = map (toList lId) (_nip_ngramsIdPatchs np)
223 toList :: ListId -> NgramsIdPatch -> (ListId, NgramsId, ListTypeId)
225 -- toList lId (NgramsIdPatch ngId (NgramsPatch lt _ _)) = (lId,ngId,listTypeId lt)
227 toGroups :: ListId -> (NgramsPatch -> Set NgramsId) -> NgramsIdPatchs -> [NodeNgramsNgrams]
228 toGroups lId addOrRem ps = concat $ map (toGroup lId addOrRem) $ _nip_ngramsIdPatchs ps
230 toGroup :: ListId -> (NgramsPatch -> Set NgramsId) -> NgramsIdPatch -> [NodeNgramsNgrams]
233 toGroup lId addOrRem (NgramsIdPatch ngId patch) =
234 map (\ng -> (NodeNgramsNgrams lId ngId ng (Just 1))) (Set.toList $ addOrRem patch)
238 tableNgramsPatch :: Connection -> CorpusId -> Maybe ListId -> NgramsIdPatchsFeed -> IO NgramsIdPatchsBack
239 tableNgramsPatch = undefined
241 tableNgramsPatch conn corpusId maybeList patchs = do
242 listId <- case maybeList of
243 Nothing -> defaultList conn corpusId
244 Just listId' -> pure listId'
245 _ <- ngramsGroup' conn Add $ toGroups listId _np_add_children patchs
246 _ <- ngramsGroup' conn Del $ toGroups listId _np_rem_children patchs
247 _ <- updateNodeNgrams conn (toLists listId patchs)
248 pure (NgramsIdPatchs [])
251 getTableNgramsPatch :: Connection -> CorpusId -> Maybe TabType -> Maybe ListId -> IO NgramsTable
252 getTableNgramsPatch = undefined