Commit 226db2c5 authored by Alexandre Delanoë's avatar Alexandre Delanoë

[REFACT] toGroupedTree done for Ngrams Terms

parent f73a9d90
...@@ -153,9 +153,9 @@ buildNgramsTermsList user uCid mCid groupParams (nt, mapListSize)= do ...@@ -153,9 +153,9 @@ buildNgramsTermsList user uCid mCid groupParams (nt, mapListSize)= do
$ Map.filter (not . ((==) Map.empty) . (view fls_parents)) $ Map.filter (not . ((==) Map.empty) . (view fls_parents))
$ view flc_scores socialLists' $ view flc_scores socialLists'
-}
let let
groupedWithList = toGroupedTree groupParams socialLists' allTerms groupedWithList = toGroupedTree groupParams socialLists' allTerms
-}
-- TODO remove -- TODO remove
-- 8<-- 8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<-- -- 8<-- 8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--8<--
......
...@@ -53,14 +53,13 @@ instance ToGroupedTree (Map Text (Set NodeId)) (Set NodeId) ...@@ -53,14 +53,13 @@ instance ToGroupedTree (Map Text (Set NodeId)) (Set NodeId)
-> FlowCont Text (GroupedTreeScores (Set NodeId)) -> FlowCont Text (GroupedTreeScores (Set NodeId))
toGroupedTree groupParams flc scores = {-view flc_scores-} flow2 toGroupedTree groupParams flc scores = {-view flc_scores-} flow2
where where
flow1 = groupWithScores'' flc scoring flow1 = groupWithScores' flc scoring
scoring t = fromMaybe Set.empty $ Map.lookup t scores scoring t = fromMaybe Set.empty $ Map.lookup t scores
flow2 = case (view flc_cont flow1) == Map.empty of flow2 = case (view flc_cont flow1) == Map.empty of
True -> flow1 True -> flow1
False -> groupWithStem' groupParams flow1 False -> groupWithStem' groupParams flow1
{-
instance ToGroupedTree (Map Text Double) Double instance ToGroupedTree (Map Text Double) Double
where where
toGroupedTree :: GroupParams toGroupedTree :: GroupParams
...@@ -70,13 +69,12 @@ instance ToGroupedTree (Map Text Double) Double ...@@ -70,13 +69,12 @@ instance ToGroupedTree (Map Text Double) Double
-> FlowCont Text (GroupedTreeScores Double) -> FlowCont Text (GroupedTreeScores Double)
toGroupedTree groupParams flc scores = {-view flc_scores-} flow2 toGroupedTree groupParams flc scores = {-view flc_scores-} flow2
where where
flow1 = groupWithScores'' flc scoring flow1 = groupWithScores' flc scoring
scoring t = fromMaybe mempty $ Map.lookup t scores scoring t = fromMaybe mempty $ Map.lookup t scores
flow2 = case (view flc_cont flow1) == Map.empty of flow2 = case (view flc_cont flow1) == Map.empty of
True -> flow1 True -> flow1
False -> groupWithStem' groupParams flow1 False -> groupWithStem' groupParams flow1
-}
------------------------------------------------------------------------ ------------------------------------------------------------------------
------------------------------------------------------------------------ ------------------------------------------------------------------------
......
...@@ -19,7 +19,7 @@ import Control.Lens (makeLenses, view, set, over) ...@@ -19,7 +19,7 @@ import Control.Lens (makeLenses, view, set, over)
import Data.Semigroup import Data.Semigroup
import Data.Set (Set) import Data.Set (Set)
import Data.Map (Map) import Data.Map (Map)
import Data.Monoid (mempty) import Data.Monoid (Monoid, mempty)
import Data.Maybe (catMaybes, fromMaybe) import Data.Maybe (catMaybes, fromMaybe)
import Data.Text (Text) import Data.Text (Text)
import Gargantext.Core.Types (ListType(..)) import Gargantext.Core.Types (ListType(..))
...@@ -31,23 +31,12 @@ import qualified Data.Map as Map ...@@ -31,23 +31,12 @@ import qualified Data.Map as Map
import qualified Data.Set as Set import qualified Data.Set as Set
------------------------------------------------------------------------ ------------------------------------------------------------------------
------------------------------------------------------------------------
class GroupWithScore a where
groupWithScores'' :: FlowCont Text FlowListScores
-> (Text -> a) -- Map Text (Set NodeId)
-> FlowCont Text (GroupedTreeScores a)
------------------------------------------------------------------------ ------------------------------------------------------------------------
-- | Main function -- | Main function
groupWithScores' :: (Eq a, Ord a, Monoid a)
instance GroupWithScore (Set NodeId) where => FlowCont Text FlowListScores
groupWithScores'' = groupWithScores' -> (Text -> a) -- Map Text (a)
-> FlowCont Text (GroupedTreeScores (a))
groupWithScores' :: FlowCont Text FlowListScores
-> (Text -> Set NodeId) -- Map Text (Set NodeId)
-> FlowCont Text (GroupedTreeScores (Set NodeId))
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
...@@ -60,16 +49,18 @@ groupWithScores' flc scores = FlowCont groups orphans ...@@ -60,16 +49,18 @@ groupWithScores' flc scores = FlowCont groups orphans
$ toMapMaybeParent scores $ toMapMaybeParent scores
$ view flc_cont flc $ view flc_cont flc
------------------------------------------------------------------------ ------------------------------------------------------------------------
toMapMaybeParent :: (Text -> Set NodeId) toMapMaybeParent :: (Eq a, Ord a, Monoid a)
=> (Text -> a)
-> Map Text FlowListScores -> Map Text FlowListScores
-> Map (Maybe Parent) (Map Text (GroupedTreeScores (Set NodeId))) -> 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
fromScores'' :: (Text -> Set NodeId) fromScores'' :: (Eq a, Ord a, Monoid a)
=> (Text -> a)
-> (Text, FlowListScores) -> (Text, FlowListScores)
-> (Maybe Parent, Map Text (GroupedTreeScores (Set NodeId))) -> (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
...@@ -79,15 +70,17 @@ fromScores'' f' (t, fs) = ( maybeParent ...@@ -79,15 +70,17 @@ 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 :: Map (Maybe Parent) (Map Text (GroupedTreeScores (Set NodeId))) toGroupedTree :: Eq a
-> Map Parent (GroupedTreeScores (Set NodeId)) => Map (Maybe Parent) (Map Text (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' :: Map (Maybe Parent) (Map Text (GroupedTreeScores (Set NodeId)))
-> (Map Text (GroupedTreeScores (Set NodeId))) toGroupedTree' :: Eq a => Map (Maybe Parent) (Map Text (GroupedTreeScores (a)))
-> Map Parent (GroupedTreeScores (Set NodeId)) -> (Map Text (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
......
...@@ -35,6 +35,8 @@ import GHC.Err.Located (undefined) ...@@ -35,6 +35,8 @@ import GHC.Err.Located (undefined)
import GHC.Real (round) import GHC.Real (round)
import Data.Map (Map, lookup) import Data.Map (Map, lookup)
import Data.Maybe (isJust, fromJust, maybe) import Data.Maybe (isJust, fromJust, maybe)
import Data.Monoid (Monoid, mempty)
import Data.Semigroup (Semigroup, (<>))
import Data.Text (Text) import Data.Text (Text)
import Data.Typeable (Typeable) import Data.Typeable (Typeable)
import Protolude ( Bool(True, False), Int, Int64, Double, Integer import Protolude ( Bool(True, False), Int, Int64, Double, Integer
...@@ -306,12 +308,18 @@ lookup2 a b m = do ...@@ -306,12 +308,18 @@ lookup2 a b m = do
m' <- lookup a m m' <- lookup a m
lookup b m' lookup b m'
----------------------------------------------- -----------------------------------------------------------------------
foldM' :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a foldM' :: (Monad m) => (a -> b -> m a) -> a -> [b] -> m a
foldM' _ z [] = return z foldM' _ z [] = return z
foldM' f z (x:xs) = do foldM' f z (x:xs) = do
z' <- f z x z' <- f z x
z' `seq` foldM' f z' xs z' `seq` foldM' f z' xs
-----------------------------------------------------------------------
instance Monoid Double where
mempty = 0
instance Semigroup Double where
(<>) a b = a * b
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