Commit 7fef5813 authored by Alexandre Delanoë's avatar Alexandre Delanoë

[REFACT] SocialList clean funs

parent eef6a43a
...@@ -83,9 +83,6 @@ flowSocialListByMode listIds nt ngrams' = do ...@@ -83,9 +83,6 @@ flowSocialListByMode listIds nt ngrams' = do
pure r pure r
------------------------------------------------------------------------ ------------------------------------------------------------------------
-- TODO: maybe use social groups too -- TODO: maybe use social groups too
-- | TODO what if equality ? -- | TODO what if equality ?
...@@ -121,6 +118,18 @@ toSocialList1_testIsTrue = result == (Just MapTerm, Set.singleton token) ...@@ -121,6 +118,18 @@ toSocialList1_testIsTrue = result == (Just MapTerm, Set.singleton token)
------------------------------------------------------------------------ ------------------------------------------------------------------------
-- | Tools -- | Tools
------------------------------------------------------------------------
termsByList :: ListType -> (Map (Maybe ListType) (Set Text)) -> Set Text
termsByList CandidateTerm m = Set.unions
$ map (\lt -> fromMaybe Set.empty $ Map.lookup lt m)
[ Nothing, Just CandidateTerm ]
termsByList l m =
fromMaybe Set.empty $ Map.lookup (Just l) m
------------------------------------------------------------------------
unions :: (Ord a, Semigroup a, Semigroup b, Ord b) unions :: (Ord a, Semigroup a, Semigroup b, Ord b)
=> [Map a (Set b)] -> Map a (Set b) => [Map a (Set b)] -> Map a (Set b)
unions = invertBack . Map.unionsWith (<>) . map invertForw unions = invertBack . Map.unionsWith (<>) . map invertForw
......
...@@ -35,15 +35,6 @@ import qualified Data.List as List ...@@ -35,15 +35,6 @@ import qualified Data.List as List
import qualified Data.Map as Map import qualified Data.Map as Map
import qualified Data.Set as Set import qualified Data.Set as Set
------------------------------------------------------------------------
termsByList :: ListType -> (Map (Maybe ListType) (Set Text)) -> Set Text
termsByList CandidateTerm m = Set.unions
$ map (\lt -> fromMaybe Set.empty $ Map.lookup lt m)
[ Nothing, Just CandidateTerm ]
termsByList l m =
fromMaybe Set.empty $ Map.lookup (Just l) m
------------------------------------------------------------------------ ------------------------------------------------------------------------
-- | [ListId] does not merge the lists (it is for Master and User lists -- | [ListId] does not merge the lists (it is for Master and User lists
-- here we need UserList only -- here we need UserList only
...@@ -53,16 +44,14 @@ countFilterList :: RepoCmdM env err m ...@@ -53,16 +44,14 @@ countFilterList :: RepoCmdM env err m
-> m (Map Text (Map ListType Int)) -> m (Map Text (Map ListType Int))
countFilterList st nt ls input = countFilterList st nt ls input =
foldM' (\m l -> countFilterList' st nt [l] m) input ls foldM' (\m l -> countFilterList' st nt [l] m) input ls
where
countFilterList' :: RepoCmdM env err m
countFilterList' :: RepoCmdM env err m => Set Text -> NgramsType -> [ListId]
=> Set Text -> NgramsType -> [ListId] -> Map Text (Map ListType Int)
-> Map Text (Map ListType Int) -> m (Map Text (Map ListType Int))
-> m (Map Text (Map ListType Int)) countFilterList' st nt ls input = do
countFilterList' st nt ls input = do ml <- toMapTextListType <$> getListNgrams ls nt
ml <- toMapTextListType <$> getListNgrams ls nt pure $ Set.foldl' (\m t -> countList t ml m) input st
-- printDebug "countFilterList'" ml
pure $ Set.foldl' (\m t -> countList t ml m) input st
------------------------------------------------------------------------ ------------------------------------------------------------------------
-- FIXME children have to herit the ListType of the parent -- FIXME children have to herit the ListType of the parent
...@@ -72,74 +61,6 @@ toMapTextListType m = Map.fromListWith (<>) ...@@ -72,74 +61,6 @@ toMapTextListType m = Map.fromListWith (<>)
$ map (toList m) $ map (toList m)
$ Map.toList m $ Map.toList m
----------------------
-- | Tools to inherit groupings
----------------------
type Parent = Text
parentUnionsMerge :: (Ord a, Ord b, Num c)
=> [Map a (Map b c)]
-> Map a (Map b c)
parentUnionsMerge = Map.unionsWith (Map.unionWith (+))
-- This Parent union is specific
-- [Private, Shared, Public]
-- means the following preferences:
-- Private > Shared > Public
-- if data have not been tagged privately, then use others tags
-- This unions behavior takes first key only and ignore others
parentUnionsExcl :: Ord a
=> [Map a b]
-> Map a b
parentUnionsExcl = Map.unions
hasParent :: Text
-> Map Text (Map Parent Int)
-> Maybe Parent
hasParent t m = case Map.lookup t m of
Nothing -> Nothing
Just m' -> (fst . fst) <$> Map.maxViewWithKey m'
toMapTextParent :: Set Text
-> Map Text (Map Parent Int)
-> [Map Text NgramsRepoElement]
-> Map Text (Map Parent Int)
toMapTextParent ts = foldl' (toMapTextParent' ts)
where
toMapTextParent' :: Set Text
-> Map Text (Map Parent Int)
-> Map Text NgramsRepoElement
-> Map Text (Map Parent Int)
toMapTextParent' ts' to from = Set.foldl' (toMapTextParent'' ts' from) to ts'
toMapTextParent'' :: Set Text
-> Map Text NgramsRepoElement
-> Map Text (Map Parent Int)
-> Text
-> Map Text (Map Parent Int)
toMapTextParent'' ss from to t = case Map.lookup t from of
Nothing -> to
Just nre -> case _nre_parent nre of
Just (NgramsTerm p') -> if Set.member p' ss
then Map.alter (addParent p') t to
else to
where
addParent p'' Nothing = Just $ addCountParent p'' Map.empty
addParent p'' (Just ps) = Just $ addCountParent p'' ps
addCountParent :: Parent -> Map Parent Int -> Map Parent Int
addCountParent p m = Map.alter addCount p m
where
addCount Nothing = Just 1
addCount (Just n) = Just $ n + 1
_ -> to
------------------------------------------------------------------------ ------------------------------------------------------------------------
toList :: Map Text NgramsRepoElement -> (Text, NgramsRepoElement) -> [(Text, ListType)] toList :: Map Text NgramsRepoElement -> (Text, NgramsRepoElement) -> [(Text, ListType)]
toList m (t, nre@(NgramsRepoElement _ _ _ _ (MSet children))) = toList m (t, nre@(NgramsRepoElement _ _ _ _ (MSet children))) =
......
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