Commit a36d62b5 authored by Grégoire Locqueville's avatar Grégoire Locqueville

Replace `occurrencesWith` with a simpler function

`occurrencesWith` takes a function parameter to map over the input
collection before counting occurrences. Every time it's called, though,
it's with `identity`, so I replaced the function with a basic occurence
counting function.

Note that if one needs to apply a function to a collection before counting
occurrences, they can simply `fmap` over the collection first.
parent 45371e41
...@@ -31,7 +31,7 @@ import Data.Text (pack, unpack, toLower) ...@@ -31,7 +31,7 @@ import Data.Text (pack, unpack, toLower)
import Data.Tuple.Extra (both) import Data.Tuple.Extra (both)
import GHC.Generics import GHC.Generics
import Gargantext.Core (Lang(..), allLangs) import Gargantext.Core (Lang(..), allLangs)
import Gargantext.Core.Text.Metrics.Count (occurrencesWith) import Gargantext.Core.Text.Metrics.Count (occurrencesSimple)
import Gargantext.Core.Text.Samples.DE qualified as DE import Gargantext.Core.Text.Samples.DE qualified as DE
import Gargantext.Core.Text.Samples.EN qualified as EN import Gargantext.Core.Text.Samples.EN qualified as EN
import Gargantext.Core.Text.Samples.ES qualified as ES import Gargantext.Core.Text.Samples.ES qualified as ES
...@@ -197,7 +197,7 @@ wordToBook ns n txt = EventBook ef en ...@@ -197,7 +197,7 @@ wordToBook ns n txt = EventBook ef en
where where
chks = allChunks ns n txt chks = allChunks ns n txt
en = DM.fromList $ map (\(n',ns') -> (n', length ns')) $ zip ns chks en = DM.fromList $ map (\(n',ns') -> (n', length ns')) $ zip ns chks
ef = foldl' DM.union DM.empty $ map (occurrencesWith identity) chks ef = foldl' DM.union DM.empty $ map occurrencesSimple chks
op :: (Freq -> Freq -> Freq) -> EventBook -> EventBook -> EventBook op :: (Freq -> Freq -> Freq) -> EventBook -> EventBook -> EventBook
op f (EventBook ef1 en1) op f (EventBook ef1 en1)
......
...@@ -133,14 +133,20 @@ coocOnSingleContext fun ts = xs ...@@ -133,14 +133,20 @@ coocOnSingleContext fun ts = xs
occurrences :: [Terms] -> Map Grouped (Map Terms Int) occurrences :: [Terms] -> Map Grouped (Map Terms Int)
occurrences = occurrencesOn _terms_stem occurrences = occurrencesOn _terms_stem
-- | Constructs the occurence map corresponding to a given collection
occurrencesSimple :: (Foldable f, Ord a, Num n)
=> f a -- ^ The collection whose items will be counted
-> Map a n -- ^ A map whose keys are items of the input
-- collection, and whose values are the number of
-- times those items appear in the input collection
occurrencesSimple collection =
foldl' (\occurenceMap item -> insertWith (+) item 1 occurenceMap)
empty
collection
occurrencesOn :: (Ord a, Ord b) => (a -> b) -> [a] -> Map b (Map a Int) occurrencesOn :: (Ord a, Ord b) => (a -> b) -> [a] -> Map b (Map a Int)
occurrencesOn f = foldl' (\m a -> insertWith (unionWith (+)) (f a) (singleton a 1) m) empty occurrencesOn f = foldl' (\m a -> insertWith (unionWith (+)) (f a) (singleton a 1) m) empty
occurrencesWith :: (Foldable list, Ord k, Num a, Show k, Show a, Show (list b)) => (b -> k) -> list b -> Map k a
occurrencesWith f xs = trace (show (xs,m) :: Text) m
where
m = foldl' (\x y -> insertWith (+) (f y) 1 x) empty xs
-- TODO add groups and filter stops -- TODO add groups and filter stops
sumOcc :: Ord a => [Occ a] -> Occ a sumOcc :: Ord a => [Occ a] -> Occ a
......
...@@ -21,7 +21,7 @@ import Gargantext.API.Ngrams.NgramsTree ( toTree, NgramsTree ) ...@@ -21,7 +21,7 @@ import Gargantext.API.Ngrams.NgramsTree ( toTree, NgramsTree )
import Gargantext.API.Ngrams.Tools ( filterListWithRoot, getListNgrams, getRepo, mapTermListRoot ) import Gargantext.API.Ngrams.Tools ( filterListWithRoot, getListNgrams, getRepo, mapTermListRoot )
import Gargantext.API.Ngrams.Types ( NgramsTerm(NgramsTerm) ) import Gargantext.API.Ngrams.Types ( NgramsTerm(NgramsTerm) )
import Gargantext.Core.NodeStory.Types ( NodeStoryEnv ) import Gargantext.Core.NodeStory.Types ( NodeStoryEnv )
import Gargantext.Core.Text.Metrics.Count (occurrencesWith) import Gargantext.Core.Text.Metrics.Count (occurrencesSimple)
import Gargantext.Core.Text.Ngrams (NgramsType) import Gargantext.Core.Text.Ngrams (NgramsType)
import Gargantext.Core.Types.Main ( ListType ) import Gargantext.Core.Types.Main ( ListType )
import Gargantext.Database.Admin.Types.Node ( NodeType(NodeList), CorpusId, contextId2NodeId ) import Gargantext.Database.Admin.Types.Node ( NodeType(NodeList), CorpusId, contextId2NodeId )
...@@ -43,7 +43,7 @@ histoData cId = do ...@@ -43,7 +43,7 @@ histoData cId = do
$ V.fromList $ V.fromList
$ sortOn fst -- TODO Vector.sortOn $ sortOn fst -- TODO Vector.sortOn
$ toList $ toList
$ occurrencesWith identity dates $ occurrencesSimple dates
pure (Histo ls css) pure (Histo ls css)
......
...@@ -23,7 +23,7 @@ import Data.Matrix hiding (identity) ...@@ -23,7 +23,7 @@ import Data.Matrix hiding (identity)
import Data.Set qualified as Set import Data.Set qualified as Set
import Data.Vector (Vector) import Data.Vector (Vector)
import Data.Vector qualified as Vector import Data.Vector qualified as Vector
import Gargantext.Core.Text.Metrics.Count (occurrencesWith) import Gargantext.Core.Text.Metrics.Count (occurrencesSimple)
import Gargantext.Prelude import Gargantext.Prelude
------------------------------------------------------------------------ ------------------------------------------------------------------------
...@@ -88,7 +88,7 @@ nodesFilter f m = (m', toKeep) ...@@ -88,7 +88,7 @@ nodesFilter f m = (m', toKeep)
toKeep = Set.fromList toKeep = Set.fromList
$ Map.keys $ Map.keys
$ Map.filter f $ Map.filter f
$ occurrencesWith identity $ occurrencesSimple
$ tupleConcat $ tupleConcat
$ List.unzip $ List.unzip
$ Map.keys m $ Map.keys m
......
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