Commit 11163872 authored by Alexandre Delanoë's avatar Alexandre Delanoë

[FEAT] Improving patch to scores

parent 80293e9d
Pipeline #1296 canceled with stage
...@@ -13,20 +13,18 @@ module Gargantext.Core.Text.List.Social.Patch ...@@ -13,20 +13,18 @@ module Gargantext.Core.Text.List.Social.Patch
import Control.Lens hiding (cons) import Control.Lens hiding (cons)
import Data.Map (Map) import Data.Map (Map)
import Data.Maybe (fromMaybe, maybe)
import Data.Monoid import Data.Monoid
import Data.Semigroup
import Data.Text (Text) import Data.Text (Text)
import Gargantext.API.Ngrams.Types import Gargantext.API.Ngrams.Types
import Gargantext.Core.Text.List.Social.Prelude import Gargantext.Core.Text.List.Social.Prelude
import Gargantext.Core.Types (ListId) import Gargantext.Core.Types (ListId)
import Gargantext.Core.Types.Main (ListType)
import Gargantext.Database.Schema.Ngrams (NgramsType(..)) import Gargantext.Database.Schema.Ngrams (NgramsType(..))
import Gargantext.Prelude import Gargantext.Prelude
import qualified Data.List as List import qualified Data.List as List
import qualified Data.Map as Map import qualified Data.Map as Map
import qualified Data.Patch.Class as Patch (Replace(..)) import qualified Data.Patch.Class as Patch (Replace(..))
addScorePatches :: NgramsType -> [ListId] addScorePatches :: NgramsType -> [ListId]
-> FlowCont Text FlowListScores -> FlowCont Text FlowListScores
-> Map NgramsType (Map ListId [Map NgramsTerm NgramsPatch]) -> Map NgramsType (Map ListId [Map NgramsTerm NgramsPatch])
...@@ -68,21 +66,25 @@ Children are not modified in this specific case. ...@@ -68,21 +66,25 @@ Children are not modified in this specific case.
-- New list get +1 score -- New list get +1 score
-- Hence others lists lay around 0 score -- Hence others lists lay around 0 score
-- TODO add children -- TODO add children
addScorePatch fl (NgramsTerm t, (NgramsPatch _children (Patch.Replace old_list new_list))) = addScorePatch fl (NgramsTerm t, (NgramsPatch children' (Patch.Replace old_list new_list))) =
fl & flc_scores . at t %~ (score fls_listType old_list (-1)) -- | Adding New Children score
addScorePatch fl' (NgramsTerm t, NgramsPatch children' Patch.Keep)
where
-- | Adding New ListType score
fl' = fl & flc_scores . at t %~ (score fls_listType old_list (-1))
& flc_scores . at t %~ (score fls_listType new_list ( 1)) & flc_scores . at t %~ (score fls_listType new_list ( 1))
-- | Patching existing Ngrams with children
addScorePatch fl (NgramsTerm p, NgramsPatch children Patch.Keep) = addScorePatch fl (NgramsTerm p, NgramsPatch children' Patch.Keep) =
foldl' add fl $ toList children foldl' add' fl $ patchMSet_toList children'
where where
add fl' (NgramsTerm t, Patch.Replace Nothing (Just _)) = doLink ( 1) p t fl' -- | Adding a child
add fl' (NgramsTerm t, Patch.Replace (Just _) Nothing) = doLink (-1) p t fl' add' fl' (NgramsTerm t, Patch.Replace Nothing (Just _)) = doLink ( 1) p t fl'
add _ _ = panic "addScorePatch: Error should not happen" -- | Removing a child
add' fl' (NgramsTerm t, Patch.Replace (Just _) Nothing) = doLink (-1) p t fl'
toList :: Ord a => PatchMSet a -> [(a,AddRem)]
toList = Map.toList . unPatchMap . unPatchMSet
-- | Maybe TODO
add' _ _ = panic "[G.C.T.L.S.P.addScorePatch] This case should not happen"
-- | Inserting a new Ngrams -- | Inserting a new Ngrams
addScorePatch fl (NgramsTerm t, NgramsReplace Nothing (Just nre)) = addScorePatch fl (NgramsTerm t, NgramsReplace Nothing (Just nre)) =
...@@ -96,22 +98,43 @@ addScorePatch fl (NgramsTerm t, NgramsReplace (Just old_nre) maybe_new_nre) = ...@@ -96,22 +98,43 @@ addScorePatch fl (NgramsTerm t, NgramsReplace (Just old_nre) maybe_new_nre) =
Nothing -> fl' Nothing -> fl'
Just new_nre -> addScorePatch fl' (NgramsTerm t, NgramsReplace Nothing (Just new_nre)) Just new_nre -> addScorePatch fl' (NgramsTerm t, NgramsReplace Nothing (Just new_nre))
------------------------------------------------------------------------------- addScorePatch fl (NgramsTerm _, NgramsReplace Nothing Nothing) = fl
childrenScore n parent children fl = -------------------------------------------------------------------------------
foldl' add fl $ unMSet children -- | Utils
childrenScore :: Int
-> Text
-> MSet NgramsTerm
-> FlowCont Text FlowListScores
-> FlowCont Text FlowListScores
childrenScore n parent children' fl =
foldl' add' fl $ unMSet children'
where where
add fl' (NgramsTerm t) = doLink n parent t fl' add' fl' (NgramsTerm t) = doLink n parent t fl'
------------------------------------------------------------------------
doLink :: Ord a
=> Int
-> Text
-> a
-> FlowCont a FlowListScores
-> FlowCont a FlowListScores
doLink n parent child fl' = fl' & flc_scores . at child %~ (score fls_parents parent n)
doLink n parent child fl' = fl' & flc_scores . at child %~ (score fls_parents parent n) score :: (Monoid a, At m, Semigroup (IxValue m))
-- score :: ListType -> Int -> Maybe FlowListScores -> Maybe FlowListScores => ((m -> Identity m) -> a -> Identity b)
-> Index m -> IxValue m -> Maybe a -> Maybe b
score field list n m = (Just mempty <> m) score field list n m = (Just mempty <> m)
& _Just & _Just
. field . field
. at list . at list
%~ (<> Just n) %~ (<> Just n)
------------------------------------------------------------------------
patchMSet_toList :: Ord a => PatchMSet a -> [(a,AddRem)]
patchMSet_toList = Map.toList . unPatchMap . unPatchMSet
unMSet :: MSet a -> [a] unMSet :: MSet a -> [a]
unMSet (MSet a) = Map.keys a unMSet (MSet a) = Map.keys a
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