Cooc.hs 3.02 KB
Newer Older
Quentin Lobbé's avatar
Quentin Lobbé committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
{-|
Module      : Gargantext.Viz.Phylo.Tools
Description : Phylomemy Tools to build/manage it
Copyright   : (c) CNRS, 2017-Present
License     : AGPL + CECILL v3
Maintainer  : team@gargantext.org
Stability   : experimental
Portability : POSIX


-}

{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE FlexibleContexts  #-}
{-# LANGUAGE OverloadedStrings #-}

module Gargantext.Viz.Phylo.Aggregates.Cooc
  where

Quentin Lobbé's avatar
Quentin Lobbé committed
20 21
import Data.List        (union,concat,nub)
import Data.Map         (Map,elems,adjust,filterWithKey)
Alexandre Delanoë's avatar
Alexandre Delanoë committed
22
import Gargantext.Prelude
Quentin Lobbé's avatar
Quentin Lobbé committed
23 24 25 26 27 28
import Gargantext.Viz.Phylo
import Gargantext.Viz.Phylo.Tools
import qualified Data.Map    as Map
import qualified Data.Set    as Set


Quentin Lobbé's avatar
Quentin Lobbé committed
29 30 31 32
-- | To transform the Fis into a full coocurency Matrix in a Phylo
fisToCooc :: Map (Date, Date) [PhyloFis] -> Phylo -> Map (Int, Int) Double
fisToCooc m p = map (/docs)
              $ foldl (\mem x -> adjust (+1) x mem) cooc
Quentin Lobbé's avatar
Quentin Lobbé committed
33
              $ concat
Quentin Lobbé's avatar
Quentin Lobbé committed
34
              $ map (\x -> listToDirectedCombiWith (\y -> getIdxInRoots y p) $ (Set.toList . getClique) x)
Quentin Lobbé's avatar
Quentin Lobbé committed
35 36 37 38
              $ (concat . elems) m
  where
    --------------------------------------
    fisNgrams :: [Ngrams]
39
    fisNgrams = foldl (\mem x -> union mem $ (Set.toList . getClique) x) [] $ (concat . elems) m
Quentin Lobbé's avatar
Quentin Lobbé committed
40 41
    --------------------------------------
    docs :: Double
42
    docs = fromIntegral $ foldl (\mem x -> mem + (getSupport x)) 0 $ (concat . elems) m
Quentin Lobbé's avatar
Quentin Lobbé committed
43 44
    --------------------------------------
    cooc :: Map (Int, Int) (Double)
Quentin Lobbé's avatar
Quentin Lobbé committed
45
    cooc = Map.fromList $ map (\x -> (x,0)) (listToDirectedCombiWith (\y -> getIdxInRoots y p) fisNgrams)
46 47 48
    --------------------------------------


Quentin Lobbé's avatar
Quentin Lobbé committed
49 50 51 52 53 54

-- | To transform a tuple of group's information into a coocurency Matrix
toCooc :: [([Int],Double)] -> Map (Int, Int) Double
toCooc l = map (/docs)
         $ foldl (\mem x -> adjust (+1) x mem) cooc
         $ concat
Quentin Lobbé's avatar
Quentin Lobbé committed
55
         $ map (\x -> listToFullCombi $ fst x) l
56 57
  where
    --------------------------------------
Quentin Lobbé's avatar
Quentin Lobbé committed
58 59
    idx :: [Int]
    idx = nub $ concat $ map fst l
60 61
    --------------------------------------
    docs :: Double
Quentin Lobbé's avatar
Quentin Lobbé committed
62
    docs = sum $ map snd l
63 64
    --------------------------------------
    cooc :: Map (Int, Int) (Double)
Quentin Lobbé's avatar
Quentin Lobbé committed
65
    cooc = Map.fromList $ map (\x -> (x,0)) $ listToFullCombi idx
Quentin Lobbé's avatar
Quentin Lobbé committed
66 67 68 69 70 71 72 73 74 75 76 77 78
    --------------------------------------    


-- | To reduce a coocurency Matrix to some keys
getSubCooc :: [Int] -> Map (Int, Int) Double -> Map (Int, Int) Double
getSubCooc idx cooc = filterWithKey (\k _ -> (elem (fst k) idx)
                                          && (elem (snd k) idx)) cooc


-- | To get a coocurency Matrix related to a given list of Periods
getCooc :: [PhyloPeriodId] -> Phylo -> Map (Int, Int) Double
getCooc prds p = toCooc $ map (\g -> (getGroupNgrams g,getGroupMeta "support" g)) gs
  where
79
    --------------------------------------
Quentin Lobbé's avatar
Quentin Lobbé committed
80 81 82 83
    -- | Here we need to go back to the level 1 (aka : the Fis level)
    gs :: [PhyloGroup]
    gs = filter (\g -> elem (getGroupPeriod g) prds ) $ getGroupsWithLevel 1 p
    -------------------------------------- 
84 85


86
-- phyloCooc :: Map (Int, Int) Double
87
-- phyloCooc = fisToCooc phyloFis phylo1_0_1