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

[FIX] ListType working now with history patch

parent 69c6ee20
Pipeline #1301 failed with stage
...@@ -101,9 +101,18 @@ buildNgramsOthersList user uCid groupParams (nt, MapListSize mapListSize) = do ...@@ -101,9 +101,18 @@ buildNgramsOthersList user uCid groupParams (nt, MapListSize mapListSize) = do
(List.cycle [mempty]) (List.cycle [mempty])
) )
if nt == Authors
then printDebug "flowSocialList" socialLists
else printDebug "flowSocialList" ""
let let
groupedWithList = toGroupedTree groupParams socialLists allTerms groupedWithList = toGroupedTree groupParams socialLists allTerms
if nt == Authors
then printDebug "groupedWithList" groupedWithList
else printDebug "groupedWithList" ""
let let
(stopTerms, tailTerms) = Map.partition ((== Just StopTerm) . viewListType) (stopTerms, tailTerms) = Map.partition ((== Just StopTerm) . viewListType)
$ view flc_scores groupedWithList $ view flc_scores groupedWithList
......
...@@ -32,7 +32,7 @@ import qualified Data.Map as Map ...@@ -32,7 +32,7 @@ import qualified Data.Map as Map
groupWithScores' :: (Eq a, Ord a, Monoid a) groupWithScores' :: (Eq a, Ord a, Monoid a)
=> FlowCont Text FlowListScores => FlowCont Text FlowListScores
-> (Text -> a) -- Map Text (a) -> (Text -> a) -- Map Text (a)
-> FlowCont Text (GroupedTreeScores (a)) -> FlowCont Text (GroupedTreeScores a)
groupWithScores' flc scores = FlowCont groups orphans groupWithScores' flc scores = FlowCont groups orphans
where where
-- parent/child relation is inherited from social lists -- parent/child relation is inherited from social lists
...@@ -44,11 +44,12 @@ groupWithScores' flc scores = FlowCont groups orphans ...@@ -44,11 +44,12 @@ groupWithScores' flc scores = FlowCont groups orphans
orphans = toGroupedTree orphans = toGroupedTree
$ toMapMaybeParent scores $ toMapMaybeParent scores
$ view flc_cont flc $ view flc_cont flc
------------------------------------------------------------------------ ------------------------------------------------------------------------
toMapMaybeParent :: (Eq a, Ord a, Monoid a) toMapMaybeParent :: (Eq a, Ord a, Monoid a)
=> (Text -> a) => (Text -> a)
-> Map Text FlowListScores -> Map Text FlowListScores
-> Map (Maybe Parent) (Map Text (GroupedTreeScores (a))) -> Map (Maybe Parent) (Map Text (GroupedTreeScores a))
toMapMaybeParent f = Map.fromListWith (<>) toMapMaybeParent f = Map.fromListWith (<>)
. (map (fromScores'' f)) . (map (fromScores'' f))
. Map.toList . Map.toList
...@@ -56,7 +57,7 @@ toMapMaybeParent f = Map.fromListWith (<>) ...@@ -56,7 +57,7 @@ toMapMaybeParent f = Map.fromListWith (<>)
fromScores'' :: (Eq a, Ord a, Monoid a) fromScores'' :: (Eq a, Ord a, Monoid a)
=> (Text -> a) => (Text -> a)
-> (Text, FlowListScores) -> (Text, FlowListScores)
-> (Maybe Parent, Map Text (GroupedTreeScores (a))) -> (Maybe Parent, Map Text (GroupedTreeScores a))
fromScores'' f' (t, fs) = ( maybeParent fromScores'' f' (t, fs) = ( maybeParent
, Map.fromList [( t, set gts'_score (f' t) , Map.fromList [( t, set gts'_score (f' t)
$ set gts'_listType maybeList mempty $ set gts'_listType maybeList mempty
...@@ -66,17 +67,18 @@ fromScores'' f' (t, fs) = ( maybeParent ...@@ -66,17 +67,18 @@ fromScores'' f' (t, fs) = ( maybeParent
maybeParent = keyWithMaxValue $ view fls_parents fs maybeParent = keyWithMaxValue $ view fls_parents fs
maybeList = keyWithMaxValue $ view fls_listType fs maybeList = keyWithMaxValue $ view fls_listType fs
------------------------------------------------------------------------
toGroupedTree :: Eq a toGroupedTree :: Eq a
=> Map (Maybe Parent) (Map Text (GroupedTreeScores (a))) => Map (Maybe Parent) (Map Text (GroupedTreeScores a))
-> Map Parent (GroupedTreeScores (a)) -> Map Parent (GroupedTreeScores a)
toGroupedTree m = case Map.lookup Nothing m of toGroupedTree m = case Map.lookup Nothing m of
Nothing -> mempty Nothing -> mempty
Just m' -> toGroupedTree' m m' Just m' -> toGroupedTree' m m'
toGroupedTree' :: Eq a => Map (Maybe Parent) (Map Text (GroupedTreeScores (a))) toGroupedTree' :: Eq a => Map (Maybe Parent) (Map Text (GroupedTreeScores a))
-> (Map Text (GroupedTreeScores (a))) -> (Map Text (GroupedTreeScores a))
-> Map Parent (GroupedTreeScores (a)) -> Map Parent (GroupedTreeScores a)
toGroupedTree' m notEmpty toGroupedTree' m notEmpty
| notEmpty == mempty = mempty | notEmpty == mempty = mempty
| otherwise = Map.mapWithKey (addGroup m) notEmpty | otherwise = Map.mapWithKey (addGroup m) notEmpty
...@@ -89,9 +91,3 @@ toGroupedTree' m notEmpty ...@@ -89,9 +91,3 @@ toGroupedTree' m notEmpty
) )
v v
...@@ -86,39 +86,39 @@ flowSocialList flowPriority user nt flc = ...@@ -86,39 +86,39 @@ flowSocialList flowPriority user nt flc =
-> FlowCont Text FlowListScores -> FlowCont Text FlowListScores
-> [ListId] -> [ListId]
-> m (FlowCont Text FlowListScores) -> m (FlowCont Text FlowListScores)
flowSocialListByModeWith nt'' flc'' ns = flowSocialListByModeWith nt'' flc'' listes =
mapM (\l -> getListNgrams [l] nt'') ns getHistoryScores History_User nt'' flc'' listes
{-
mapM (\l -> getListNgrams [l] nt'') listes
>>= pure >>= pure
. toFlowListScores (keepAllParents nt'') flc'' . toFlowListScores (keepAllParents nt'') flc''
-}
----------------------------------------------------------------- -----------------------------------------------------------------
getHistoryScores :: ( RepoCmdM env err m
getHistory :: ( RepoCmdM env err m
, CmdM env err m , CmdM env err m
, HasNodeError err , HasNodeError err
, HasTreeError err , HasTreeError err
) )
=> History => History
-> NgramsType -> NgramsType
-> FlowCont Text FlowListScores
-> [ListId] -> [ListId]
-> m (Map NgramsType (Map ListId [Map NgramsTerm NgramsPatch])) -> m (FlowCont Text FlowListScores)
getHistory hist nt listes = getHistoryScores hist nt fl listes = do
history hist [nt] listes <$> getRepo hist' <- addScorePatches nt listes fl <$> getHistory hist nt listes
-- printDebug "hist" hist'
pure hist'
getHistoryScores :: ( RepoCmdM env err m getHistory :: ( RepoCmdM env err m
, CmdM env err m , CmdM env err m
, HasNodeError err , HasNodeError err
, HasTreeError err , HasTreeError err
) )
=> History => History
-> NgramsType -> NgramsType
-> FlowCont Text FlowListScores
-> [ListId] -> [ListId]
-> m (FlowCont Text FlowListScores) -> m (Map NgramsType (Map ListId [Map NgramsTerm NgramsPatch]))
getHistoryScores hist nt fl listes = getHistory hist nt listes =
addScorePatches nt listes fl <$> getHistory hist nt listes history hist [nt] listes <$> getRepo
...@@ -65,7 +65,6 @@ Children are not modified in this specific case. ...@@ -65,7 +65,6 @@ Children are not modified in this specific case.
-- | Old list get -1 score -- | Old list get -1 score
-- 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
addScorePatch fl (NgramsTerm t, (NgramsPatch children' (Patch.Replace old_list new_list))) = addScorePatch fl (NgramsTerm t, (NgramsPatch children' (Patch.Replace old_list new_list))) =
-- | Adding New Children score -- | Adding New Children score
addScorePatch fl' (NgramsTerm t, NgramsPatch children' Patch.Keep) addScorePatch fl' (NgramsTerm t, NgramsPatch children' Patch.Keep)
...@@ -73,27 +72,30 @@ addScorePatch fl (NgramsTerm t, (NgramsPatch children' (Patch.Replace old_list n ...@@ -73,27 +72,30 @@ addScorePatch fl (NgramsTerm t, (NgramsPatch children' (Patch.Replace old_list n
-- | Adding New ListType score -- | Adding New ListType score
fl' = fl & flc_scores . at t %~ (score fls_listType old_list (-1)) 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))
& flc_cont %~ (Map.delete t)
-- | Patching existing Ngrams with children -- | Patching existing Ngrams with children
addScorePatch fl (NgramsTerm p, NgramsPatch children' Patch.Keep) = addScorePatch fl (NgramsTerm p, NgramsPatch children' Patch.Keep) =
foldl' add' fl $ patchMSet_toList children' foldl' addChild fl $ patchMSet_toList children'
where where
-- | Adding a child -- | Adding a child
add' fl' (NgramsTerm t, Patch.Replace Nothing (Just _)) = doLink ( 1) p t fl' addChild fl' (NgramsTerm t, Patch.Replace Nothing (Just _)) = doLink ( 1) p t fl'
-- | Removing a child -- | Removing a child
add' fl' (NgramsTerm t, Patch.Replace (Just _) Nothing) = doLink (-1) p t fl' addChild fl' (NgramsTerm t, Patch.Replace (Just _) Nothing) = doLink (-1) p t fl'
-- | This case should not happen: does Nothing -- | This case should not happen: does Nothing
add' fl' _ = fl' addChild fl' _ = fl'
-- | Inserting a new Ngrams -- | Inserting a new Ngrams
addScorePatch fl (NgramsTerm t, NgramsReplace Nothing (Just nre)) = addScorePatch fl (NgramsTerm t, NgramsReplace Nothing (Just nre)) =
childrenScore 1 t (nre ^. nre_children) childrenScore 1 t (nre ^. nre_children)
$ fl & flc_scores . at t %~ (score fls_listType $ nre ^. nre_list) 1 $ fl & flc_scores . at t %~ (score fls_listType $ nre ^. nre_list) 1
& flc_cont %~ (Map.delete t)
addScorePatch fl (NgramsTerm t, NgramsReplace (Just old_nre) maybe_new_nre) = addScorePatch fl (NgramsTerm t, NgramsReplace (Just old_nre) maybe_new_nre) =
let fl' = childrenScore (-1) t (old_nre ^. nre_children) let fl' = childrenScore (-1) t (old_nre ^. nre_children)
$ fl & flc_scores . at t %~ (score fls_listType $ old_nre ^. nre_list) (-1) $ fl & flc_scores . at t %~ (score fls_listType $ old_nre ^. nre_list) (-1)
& flc_cont %~ (Map.delete t)
in case maybe_new_nre of in case maybe_new_nre of
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))
......
...@@ -25,6 +25,7 @@ import Data.Semigroup (Semigroup(..)) ...@@ -25,6 +25,7 @@ import Data.Semigroup (Semigroup(..))
import Data.Text (Text) import Data.Text (Text)
import GHC.Generics (Generic) import GHC.Generics (Generic)
import Gargantext.API.Ngrams.Types import Gargantext.API.Ngrams.Types
import Gargantext.Core.Text.Metrics.Freq (getMaxFromMap)
import Gargantext.Core.Types.Main import Gargantext.Core.Types.Main
import Gargantext.Prelude import Gargantext.Prelude
import qualified Data.Map as Map import qualified Data.Map as Map
...@@ -96,9 +97,7 @@ parentUnionsExcl :: Ord a ...@@ -96,9 +97,7 @@ parentUnionsExcl :: Ord a
-> Map a b -> Map a b
parentUnionsExcl = Map.unions parentUnionsExcl = Map.unions
------------------------------------------------------------------------ ------------------------------------------------------------------------
hasParent :: Text hasParent :: Text
-> Map Text (Map Parent Int) -> Map Text (Map Parent Int)
-> Maybe Parent -> Maybe Parent
...@@ -114,14 +113,21 @@ hasParent t m = case Map.lookup t m of ...@@ -114,14 +113,21 @@ hasParent t m = case Map.lookup t m of
-- Just 'z' -- Just 'z'
-- >>> keyWithMaxValue $ DM.fromList $ zip (['a'..'z'] :: [Char]) ([-1,-2..]::[Int]) -- >>> keyWithMaxValue $ DM.fromList $ zip (['a'..'z'] :: [Char]) ([-1,-2..]::[Int])
-- Nothing -- Nothing
keyWithMaxValue :: (Ord b, Num b) => Map a b -> Maybe a keyWithMaxValue :: (Ord a, Ord b, Num b) => Map a b -> Maybe a
keyWithMaxValue m = do keyWithMaxValue m = do
(k,a) <- fst <$> Map.maxViewWithKey m k <- headMay $ getMaxFromMap m
if a > 0 maxValue <- Map.lookup k m
then Just k if maxValue > 0
then pure k
else Nothing else Nothing
findMax :: (Ord b, Num b) => Map a b -> Maybe (a,b)
findMax m = case Map.null m of
True -> Nothing
False -> Just $ Map.findMax m
------------------------------------------------------------------------ ------------------------------------------------------------------------
unPatchMap :: Ord a => PatchMap a b -> Map a b unPatchMap :: Ord a => PatchMap a b -> Map a b
unPatchMap = Map.fromList . PatchMap.toList unPatchMap = Map.fromList . PatchMap.toList
......
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