[NGRAMS] WIP

parent 5b112d21
...@@ -116,6 +116,7 @@ library: ...@@ -116,6 +116,7 @@ library:
- opaleye - opaleye
- pandoc - pandoc
- parsec - parsec
- patches-class
- patches-map - patches-map
- path - path
- path-io - path-io
......
...@@ -26,12 +26,18 @@ add get ...@@ -26,12 +26,18 @@ add get
{-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeOperators #-} {-# LANGUAGE TypeOperators #-}
{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Gargantext.API.Ngrams module Gargantext.API.Ngrams
where where
-- import Gargantext.Database.User (UserId) -- import Gargantext.Database.User (UserId)
--import Data.Map.Strict.Patch (Patch, replace, fromList) import Data.Patch.Class (Replace(..), replace)
import qualified Data.Map.Strict.Patch as PM
import Data.Monoid
import Data.Semigroup
import Data.Set (Set)
import qualified Data.Set as Set
--import Data.Maybe (catMaybes) --import Data.Maybe (catMaybes)
--import qualified Data.Map.Strict as DM --import qualified Data.Map.Strict as DM
--import qualified Data.Set as Set --import qualified Data.Set as Set
...@@ -83,39 +89,43 @@ instance Arbitrary TabType ...@@ -83,39 +89,43 @@ instance Arbitrary TabType
arbitrary = elements [minBound .. maxBound] arbitrary = elements [minBound .. maxBound]
------------------------------------------------------------------------ ------------------------------------------------------------------------
type NgramsTerm = Text
data NgramsElement = data NgramsElement =
NgramsElement { _ne_ngrams :: Text NgramsElement { _ne_ngrams :: NgramsTerm
, _ne_list :: ListType , _ne_list :: ListType
, _ne_occurrences :: Int , _ne_occurrences :: Int
, _ne_root :: Maybe NgramsTerm
, _ne_children :: Set NgramsTerm
} }
deriving (Ord, Eq, Show, Generic) deriving (Ord, Eq, Show, Generic)
$(deriveJSON (unPrefix "_ne_") ''NgramsElement) $(deriveJSON (unPrefix "_ne_") ''NgramsElement)
instance ToSchema NgramsElement instance ToSchema NgramsElement
instance Arbitrary NgramsElement where instance Arbitrary NgramsElement where
arbitrary = elements [NgramsElement "sport" StopList 1] arbitrary = elements [NgramsElement "sport" StopList 1 Nothing mempty]
------------------------------------------------------------------------ ------------------------------------------------------------------------
data NgramsTable = NgramsTable { _ngramsTable :: [Tree NgramsElement] } newtype NgramsTable = NgramsTable { _ngramsTable :: [NgramsElement] }
deriving (Ord, Eq, Generic) deriving (Ord, Eq, Generic, ToJSON, FromJSON)
$(deriveJSON (unPrefix "_") ''NgramsTable)
instance Arbitrary NgramsTable where instance Arbitrary NgramsTable where
arbitrary = NgramsTable <$> arbitrary arbitrary = elements
[ NgramsTable
-- TODO [ NgramsElement "animal" GraphList 1 Nothing (Set.fromList ["dog"])
instance Arbitrary (Tree NgramsElement) where , NgramsElement "dog" GraphList 3 (Just "animal")
arbitrary = elements [ TreeN (NgramsElement "animal" GraphList 1) (Set.fromList ["object", "cat", "nothing"])
[TreeN (NgramsElement "dog" GraphList 3) [] , NgramsElement "object" CandidateList 2 (Just "animal") mempty
, TreeN (NgramsElement "object" CandidateList 2) [] , NgramsElement "cat" GraphList 1 (Just "animal") mempty
, TreeN (NgramsElement "cat" GraphList 1) [] , NgramsElement "nothing" StopList 4 (Just "animal") mempty
, TreeN (NgramsElement "nothing" StopList 4) []
] ]
, TreeN (NgramsElement "plant" GraphList 3) , NgramsTable
[TreeN (NgramsElement "flower" GraphList 3) [] [ NgramsElement "plant" GraphList 3 Nothing
, TreeN (NgramsElement "moon" CandidateList 1) [] (Set.fromList ["flower", "moon", "cat", "sky"])
, TreeN (NgramsElement "cat" GraphList 2) [] , NgramsElement "flower" GraphList 3 (Just "plant") mempty
, TreeN (NgramsElement "sky" StopList 1) [] , NgramsElement "moon" CandidateList 1 (Just "plant") mempty
, NgramsElement "cat" GraphList 2 (Just "plant") mempty
, NgramsElement "sky" StopList 1 (Just "plant") mempty
] ]
] ]
instance ToSchema NgramsTable instance ToSchema NgramsTable
...@@ -126,23 +136,35 @@ instance ToSchema NgramsTable ...@@ -126,23 +136,35 @@ instance ToSchema NgramsTable
-- | OutGroup NgramsId NgramsId -- | OutGroup NgramsId NgramsId
-- | SetListType NgramsId ListType -- | SetListType NgramsId ListType
data PatchSet a = PatchSet
{ _rem :: Set a
, _add :: Set a
}
deriving (Eq, Ord, Show, Generic)
instance (Ord a, Arbitrary a) => Arbitrary (PatchSet a) where
arbitrary = PatchSet <$> arbitrary <*> arbitrary
instance ToSchema a => ToSchema (PatchSet a)
instance ToSchema a => ToSchema (Replace a)
data NgramsPatch = data NgramsPatch =
NgramsPatch { _np_list_types :: ListType -- TODO Map UserId ListType NgramsPatch { _patch_children :: PatchSet NgramsElement
, _np_add_children :: Set NgramsElement , _patch_list :: Replace ListType -- TODO Map UserId ListType
, _np_rem_children :: Set NgramsElement
} }
deriving (Ord, Eq, Show, Generic) deriving (Ord, Eq, Show, Generic)
$(deriveJSON (unPrefix "_np_") ''NgramsPatch) $(deriveJSON (unPrefix "_") ''NgramsPatch)
instance Semigroup NgramsPatch where
instance ToSchema NgramsPatch instance ToSchema NgramsPatch
instance Arbitrary NgramsPatch where instance Arbitrary NgramsPatch where
arbitrary = NgramsPatch <$> arbitrary <*> arbitrary <*> arbitrary arbitrary = NgramsPatch <$> arbitrary <*> (replace <$> arbitrary <*> arbitrary)
--
data NgramsIdPatch = data NgramsIdPatch =
NgramsIdPatch { _nip_ngramsId :: NgramsElement NgramsIdPatch { _nip_ngramsId :: NgramsTerm
, _nip_ngramsPatch :: NgramsPatch , _nip_ngramsPatch :: NgramsPatch
} }
deriving (Ord, Eq, Show, Generic) deriving (Ord, Eq, Show, Generic)
...@@ -155,14 +177,14 @@ instance Arbitrary NgramsIdPatch where ...@@ -155,14 +177,14 @@ instance Arbitrary NgramsIdPatch where
arbitrary = NgramsIdPatch <$> arbitrary <*> arbitrary arbitrary = NgramsIdPatch <$> arbitrary <*> arbitrary
-- --
-- TODO:
data NgramsIdPatchs = -- * This should be a Map NgramsId NgramsPatch
-- * Patchs -> Patches
newtype NgramsIdPatchs =
NgramsIdPatchs { _nip_ngramsIdPatchs :: [NgramsIdPatch] } NgramsIdPatchs { _nip_ngramsIdPatchs :: [NgramsIdPatch] }
deriving (Ord, Eq, Show, Generic) deriving (Ord, Eq, Show, Generic, Arbitrary)
$(deriveJSON (unPrefix "_nip_") ''NgramsIdPatchs) $(deriveJSON (unPrefix "_nip_") ''NgramsIdPatchs)
instance ToSchema NgramsIdPatchs instance ToSchema NgramsIdPatchs
instance Arbitrary NgramsIdPatchs where
arbitrary = NgramsIdPatchs <$> arbitrary
------------------------------------------------------------------------ ------------------------------------------------------------------------
------------------------------------------------------------------------ ------------------------------------------------------------------------
...@@ -198,8 +220,8 @@ ngramsIdPatch = fromList $ catMaybes $ reverse [ replace (1::NgramsId) (Just $ n ...@@ -198,8 +220,8 @@ ngramsIdPatch = fromList $ catMaybes $ reverse [ replace (1::NgramsId) (Just $ n
type CorpusId = Int type CorpusId = Int
type TableNgramsApi = Summary " Table Ngrams API Change" type TableNgramsApi = Summary " Table Ngrams API Change"
:> QueryParam "list" ListId :> QueryParam "list" ListId
:> ReqBody '[JSON] NgramsIdPatchs :> ReqBody '[JSON] NgramsIdPatchs -- Versioned ...
:> Put '[JSON] NgramsIdPatchsBack :> Put '[JSON] NgramsIdPatchsBack -- Versioned ...
type TableNgramsApiGet = Summary " Table Ngrams API Get" type TableNgramsApiGet = Summary " Table Ngrams API Get"
:> QueryParam "ngramsType" TabType :> QueryParam "ngramsType" TabType
...@@ -217,18 +239,20 @@ defaultList c cId = view node_id <$> maybe (panic noListFound) identity ...@@ -217,18 +239,20 @@ defaultList c cId = view node_id <$> maybe (panic noListFound) identity
where where
noListFound = "Gargantext.API.Ngrams.defaultList: no list found" noListFound = "Gargantext.API.Ngrams.defaultList: no list found"
{-
toLists :: ListId -> NgramsIdPatchs -> [(ListId, NgramsId, ListTypeId)] toLists :: ListId -> NgramsIdPatchs -> [(ListId, NgramsId, ListTypeId)]
toLists lId np = map (toList lId) (_nip_ngramsIdPatchs np) toLists lId np =
[ (lId,ngId,listTypeId lt) | map (toList lId) (_nip_ngramsIdPatchs np) ]
toList :: ListId -> NgramsIdPatch -> (ListId, NgramsId, ListTypeId) toList :: ListId -> NgramsIdPatch -> (ListId, NgramsId, ListTypeId)
toList = undefined toList = undefined
-- toList lId (NgramsIdPatch ngId (NgramsPatch lt _ _)) = (lId,ngId,listTypeId lt)
toGroups :: ListId -> (NgramsPatch -> Set NgramsId) -> NgramsIdPatchs -> [NodeNgramsNgrams] toGroups :: ListId -> (NgramsPatch -> Set NgramsId) -> NgramsIdPatchs -> [NodeNgramsNgrams]
toGroups lId addOrRem ps = concat $ map (toGroup lId addOrRem) $ _nip_ngramsIdPatchs ps toGroups lId addOrRem ps = concat $ map (toGroup lId addOrRem) $ _nip_ngramsIdPatchs ps
toGroup :: ListId -> (NgramsPatch -> Set NgramsId) -> NgramsIdPatch -> [NodeNgramsNgrams] toGroup :: ListId -> (NgramsPatch -> Set NgramsId) -> NgramsIdPatch -> [NodeNgramsNgrams]
toGroup = undefined toGroup = undefined
-}
{- {-
toGroup lId addOrRem (NgramsIdPatch ngId patch) = toGroup lId addOrRem (NgramsIdPatch ngId patch) =
map (\ng -> (NodeNgramsNgrams lId ngId ng (Just 1))) (Set.toList $ addOrRem patch) map (\ng -> (NodeNgramsNgrams lId ngId ng (Just 1))) (Set.toList $ addOrRem patch)
......
...@@ -32,3 +32,4 @@ extra-deps: ...@@ -32,3 +32,4 @@ extra-deps:
- servant-flatten-0.2 - servant-flatten-0.2
- serialise-0.2.0.0 # imt-api-client - serialise-0.2.0.0 # imt-api-client
- KMP-0.1.0.2 - KMP-0.1.0.2
- validity-0.8.0.0 # patches-{map,class}
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