Commit 5523d058 authored by Alexandre Delanoë's avatar Alexandre Delanoë

[API][NGRAMS][PATCH] basic change working with replace function.

parent 33a3fc84
...@@ -32,14 +32,18 @@ import Data.Aeson (FromJSON, ToJSON) ...@@ -32,14 +32,18 @@ import Data.Aeson (FromJSON, ToJSON)
import Data.Aeson.TH (deriveJSON) import Data.Aeson.TH (deriveJSON)
import Data.Map.Strict (Map) import Data.Map.Strict (Map)
import Data.Map.Strict.Patch (Patch, apply, transformWith) import qualified Data.Map.Strict as DM
import Data.Map.Strict.Patch (Patch, apply, Edit, EditV, replace, transformWith, fromList)
import Data.Text (Text) import Data.Text (Text)
import Data.Maybe (catMaybes)
import Data.Set (Set) import Data.Set (Set)
import qualified Data.Set as Set
import GHC.Generics (Generic) import GHC.Generics (Generic)
import Gargantext.Database.Ngram (NgramsId) import Gargantext.Database.Ngram (NgramsId)
import Gargantext.Database.User (UserId) import Gargantext.Database.User (UserId)
import Gargantext.Core.Types (ListType(..)) import Gargantext.Text.List.Types (ListType(..))
import Gargantext.Core.Types.Main (Tree(..)) import Gargantext.Core.Types.Main (Tree(..))
import Gargantext.Core.Utils.Prefix (unPrefix) import Gargantext.Core.Utils.Prefix (unPrefix)
import Gargantext.Prelude import Gargantext.Prelude
...@@ -49,11 +53,12 @@ data NgramsElement = ...@@ -49,11 +53,12 @@ data NgramsElement =
, _ne_ngrams :: Text , _ne_ngrams :: Text
, _ne_list :: ListType , _ne_list :: ListType
} }
deriving (Ord, Eq)
$(deriveJSON (unPrefix "_ne_") ''NgramsElement) $(deriveJSON (unPrefix "_ne_") ''NgramsElement)
data NgramsTable = NgramsTable { _ngramsTable :: [Tree NgramsElement] } data NgramsTable = NgramsTable { _ngramsTable :: [Tree NgramsElement] }
deriving (Generic) deriving (Ord, Eq, Generic)
instance ToJSON NgramsTable instance ToJSON NgramsTable
instance FromJSON NgramsTable instance FromJSON NgramsTable
...@@ -67,16 +72,40 @@ instance ToJSON (Tree NgramsElement) ...@@ -67,16 +72,40 @@ instance ToJSON (Tree NgramsElement)
-- | SetListType NgramsId ListType -- | SetListType NgramsId ListType
data NgramsPatch = data NgramsPatch =
NgramsPatch { list_types :: Map UserId ListType NgramsPatch { _np_list_types :: Map UserId ListType
, add_children :: Set NgramsId , _np_add_children :: Set NgramsId
, rem_children :: Set NgramsId , _np_rem_children :: Set NgramsId
} }
deriving (Ord, Eq, Show)
$(deriveJSON (unPrefix "_np_") ''NgramsPatch)
type NgramsIdPatch = Patch NgramsId NgramsPatch
------------------------------------------------------------------------
type Version = Int
data Versioned a = Versioned
{ _v_version :: Version
, _v_data :: a
}
ngramsPatch :: NgramsPatch
ngramsPatch = NgramsPatch (DM.fromList [(1, StopList)]) Set.empty Set.empty
{-
toEdit :: NgramsId -> NgramsPatch -> Edit NgramsId NgramsPatch
toEdit n p = Edit n p
-}
ngramsIdPatch :: Patch NgramsId NgramsPatch
ngramsIdPatch = fromList $ catMaybes $ reverse [ replace (1::NgramsId) (Just ngramsPatch) Nothing
, replace (1::NgramsId) Nothing (Just ngramsPatch)
, replace (2::NgramsId) Nothing (Just ngramsPatch)
]
data Patch = Map NgramsId NgramsPatch
-- applyPatchBack :: Patch -> IO Patch -- applyPatchBack :: Patch -> IO Patch
-- isEmptyPatch = Map.all (\x -> Set.isEmpty (add_children x) && Set.isEmpty ... ) -- isEmptyPatch = Map.all (\x -> Set.isEmpty (add_children x) && Set.isEmpty ... )
------------------------------------------------------------------- ------------------------------------------------------------------------
-------------------------------------------------------------------
-------------------------------------------------------------------
...@@ -78,6 +78,7 @@ corpusTree nId t = TreeN (NodeTree ("Corpus " <> t) NodeCorpus nId) ( [ leafT ...@@ -78,6 +78,7 @@ corpusTree nId t = TreeN (NodeTree ("Corpus " <> t) NodeCorpus nId) ( [ leafT
--data Classification = Favorites | MyClassifcation --data Classification = Favorites | MyClassifcation
-- TODO multiple ListType declaration, remove it
data ListType = Stop | Candidate | Map data ListType = Stop | Candidate | Map
deriving (Generic) deriving (Generic)
...@@ -132,7 +133,7 @@ type Offset = Int ...@@ -132,7 +133,7 @@ type Offset = Int
------------------------------------------------------------------------ ------------------------------------------------------------------------
-- All the Database is structred like a hierarchical Tree -- All the Database is structred like a hierarchical Tree
data Tree a = TreeN a [Tree a] data Tree a = TreeN a [Tree a]
deriving (Show, Read, Eq, Generic) deriving (Show, Read, Eq, Generic, Ord)
instance ToJSON (Tree NodeTree) where instance ToJSON (Tree NodeTree) where
toJSON (TreeN node nodes) = toJSON (TreeN node nodes) =
......
...@@ -11,19 +11,25 @@ CSV parser for Gargantext corpus files. ...@@ -11,19 +11,25 @@ CSV parser for Gargantext corpus files.
-} -}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE NoImplicitPrelude #-} {-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE OverloadedStrings #-}
module Gargantext.Text.List.Types where module Gargantext.Text.List.Types where
import Prelude (Bounded, Enum, minBound, maxBound) import Data.Aeson (FromJSON, ToJSON)
import Data.Text (Text)
import Data.Map (Map, empty, fromList) import Data.Map (Map, empty, fromList)
import Data.Text (Text)
import GHC.Generics (Generic)
import Gargantext.Prelude import Gargantext.Prelude
import Prelude (Bounded, Enum, minBound, maxBound)
------------------------------------------------------------------- -------------------------------------------------------------------
data ListType = GraphList | StopList | CandidateList data ListType = GraphList | StopList | CandidateList
deriving (Show, Eq, Ord, Enum, Bounded) deriving (Show, Eq, Ord, Enum, Bounded, Generic)
instance FromJSON ListType
instance ToJSON ListType
type Lists = Map ListType (Map Text [Text]) type Lists = Map ListType (Map Text [Text])
......
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