import qualified Data.Vector.Storable as Vec
import qualified IGraph as Igraph
import qualified IGraph.Algorithms.Layout as Layout
--- import qualified Data.Vector.Storable as Vec
--- import qualified Data.Map as Map
--- import qualified Data.List as List
--- import Debug.Trace (trace)
--- import qualified Data.HashMap.Strict as HashMap
-------------------------------------------------------------
$ mat2map
$ measure distance
$ case distance of
- Conditional -> map2mat Triangular 0 tiSize
- Distributional -> map2mat Square 0 tiSize
+ Conditional -> map2mat Triangle 0 tiSize
+ Distributional -> map2mat Square 0 tiSize
$ Map.filter (> 1) myCooc'
where
data PartitionMethod = Louvain | Spinglass
+-- | coocurrences graph computation
cooc2graphWith :: PartitionMethod
-> Distance
-> Threshold
-> Double
-> Map (t, t) Int
-> Map (Index, Index) Double
-cooc2graph'' distance threshold myCooc = neighbouMap
+cooc2graph'' distance threshold myCooc = neighbourMap
where
(ti, _) = createIndices myCooc
myCooc' = toIndex ti myCooc
- matCooc = mat2map
- $ measure distance
- $ case distance of
- Conditional -> map2mat Triangular 0 (Map.size ti)
- Distributional -> map2mat Square 0 (Map.size ti)
- $ Map.filter (> 1) myCooc'
- neighbouMap = filterByNeighbours threshold matCooc
+ matCooc = map2mat Triangle 0 (Map.size ti) $ Map.filter (> 1) myCooc'
+ distanceMat = measure distance matCooc
+ neighbourMap = filterByNeighbours threshold
+ $ mat2map distanceMat
-- Quentin
in List.take (round threshold) selected
) indexes
-cooc2graphWith' :: ToComId a
- => Partitions a
- -> Distance
+
+doDistanceMap :: Distance
-> Threshold
-> HashMap (NgramsTerm, NgramsTerm) Int
- -> IO Graph
-cooc2graphWith' doPartitions distance threshold myCooc = do
- let
+ -> (Map (Int,Int) Double, Map (Index, Index) Int, Map NgramsTerm Index)
+doDistanceMap distance threshold myCooc = (distanceMap, myCooc', ti)
+ where
-- TODO remove below
theMatrix = Map.fromList
$ HashMap.toList myCooc
tiSize = Map.size ti
myCooc' = toIndex ti theMatrix
matCooc = case distance of -- Shape of the Matrix
- Conditional -> map2mat Triangular 0 tiSize
+ Conditional -> map2mat Triangle 0 tiSize
Distributional -> map2mat Square 0 tiSize
$ case distance of -- Removing the Diagonal ?
Conditional -> Map.filterWithKey (\(a,b) _ -> a /= b)
similarities = measure distance matCooc
links = round (let n :: Double = fromIntegral tiSize in n * log n)
- distanceMap = Map.fromList
- $ List.take links
- $ List.sortOn snd
- $ Map.toList
- $ case distance of
- Conditional -> Map.filter (> threshold)
- Distributional -> Map.filter (> 0)
- $ mat2map similarities
+
+ distanceMap = Map.fromList $ List.take links
+ $ List.sortOn snd
+ $ Map.toList
+ $ case distance of
+ Conditional -> Map.filter (> threshold)
+ Distributional -> Map.filter (> 0)
+ $ mat2map similarities
+
+cooc2graphWith' :: ToComId a
+ => Partitions a
+ -> Distance
+ -> Threshold
+ -> HashMap (NgramsTerm, NgramsTerm) Int
+ -> IO Graph
+cooc2graphWith' doPartitions distance threshold myCooc = do
+ let
+ (distanceMap, myCooc', ti) = doDistanceMap distance threshold myCooc
nodesApprox :: Int
nodesApprox = n'
n' = Set.size $ Set.fromList $ as <> bs
ClustersParams rivers _level = clustersParams nodesApprox
+{- -- Debug
+ saveAsFileDebug "debug/distanceMap" distanceMap
printDebug "similarities" similarities
+-}
partitions <- if (Map.size distanceMap > 0)
then doPartitions distanceMap
myCooc' bridgeness' confluence' partitions
-
--- cooc2graph :: Distance
--- -> Threshold
--- -> (Map (Text, Text) Int)
--- -> IO Graph
--- cooc2graph distance threshold myCooc = do
--- printDebug "cooc2graph" distance
--- let
--- -- TODO remove below
--- theMatrix = Map.fromList $ HashMap.toList myCooc
--- (ti, _) = createIndices theMatrix
--- myCooc' = toIndex ti theMatrix
--- matCooc = map2mat 0 (Map.size ti)
--- $ Map.filterWithKey (\(a,b) _ -> a /= b)
--- $ Map.filter (> 1) myCooc'
--- distanceMat = measure distance matCooc
--- distanceMap = Map.filter (> threshold) $ mat2map distanceMat
-
--- nodesApprox :: Int
--- nodesApprox = n'
--- where
--- (as, bs) = List.unzip $ Map.keys distanceMap
--- n' = Set.size $ Set.fromList $ as <> bs
--- ClustersParams rivers _level = clustersParams nodesApprox
-
--- printDebug "Start" ("partitions" :: Text)
--- partitions <- if (Map.size distanceMap > 0)
--- -- then iLouvainMap 100 10 distanceMap
--- -- then hLouvain distanceMap
--- then doPartitions distanceMap
--- else panic "Text.Flow: DistanceMap is empty"
--- printDebug "End" ("partitions" :: Text)
-
--- let
--- -- bridgeness' = distanceMap
--- bridgeness' = trace ("Rivers: " <> show rivers)
--- $ bridgeness rivers partitions distanceMap
--- confluence' = confluence (Map.keys bridgeness') 3 True False
-
--- pure $ data2graph (Map.toList $ Map.mapKeys unNgramsTerm ti)
--- myCooc' bridgeness' confluence' partitions
-
------------------------------------------------------------------------
------------------------------------------------------------------------
data ClustersParams = ClustersParams { bridgness :: Double