Commit 800f255f authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

[ngrams] some more fixes and refactorings

parent df4a810f
...@@ -83,7 +83,7 @@ module Gargantext.API.Ngrams ...@@ -83,7 +83,7 @@ module Gargantext.API.Ngrams
where where
import Control.Concurrent import Control.Concurrent
import Control.Lens ((.~), view, (^.), (^..), (+~), (%~), (.~), sumOf, at, _Just, Each(..), (%%~), mapped, ifolded, withIndex) import Control.Lens ((.~), view, (^.), (^..), (+~), (%~), (.~), sumOf, at, _Just, Each(..), (%%~), mapped, ifolded, withIndex, over)
import Control.Monad.Reader import Control.Monad.Reader
import Data.Aeson hiding ((.=)) import Data.Aeson hiding ((.=))
import Data.Either (Either(..)) import Data.Either (Either(..))
...@@ -535,6 +535,11 @@ getTableNgrams _nType nId tabType listId limit_ offset ...@@ -535,6 +535,11 @@ getTableNgrams _nType nId tabType listId limit_ offset
minSize' = maybe (const True) (<=) minSize minSize' = maybe (const True) (<=) minSize
maxSize' = maybe (const True) (>=) maxSize maxSize' = maybe (const True) (>=) maxSize
rootOf tableMap ne = maybe ne (\r -> fromMaybe (panic "getTableNgrams: invalid root")
(tableMap ^. at r)
)
(ne ^. ne_root)
selected_node n = minSize' s selected_node n = minSize' s
&& maxSize' s && maxSize' s
&& searchQuery (n ^. ne_ngrams) && searchQuery (n ^. ne_ngrams)
...@@ -553,30 +558,32 @@ getTableNgrams _nType nId tabType listId limit_ offset ...@@ -553,30 +558,32 @@ getTableNgrams _nType nId tabType listId limit_ offset
--------------------------------------- ---------------------------------------
filteredNodes :: Map NgramsTerm NgramsElement -> [NgramsElement] filteredNodes :: Map NgramsTerm NgramsElement -> [NgramsElement]
filteredNodes tableMap = rootOf <$> list & filter selected_node filteredNodes tableMap = roots <> inners
-- rootOf <$> list & filter selected_node
where where
rootOf ne = maybe ne (\r -> fromMaybe (panic "getTableNgrams: invalid root")
(tableMap ^. at r)
)
(ne ^. ne_root)
list = tableMap ^.. each list = tableMap ^.. each
selected_nodes = list & filter selected_node
roots = rootOf tableMap <$> selected_nodes
rootSet = Set.fromList (_ne_ngrams <$> roots)
inners = list & filter (selected_inner rootSet)
--------------------------------------- ---------------------------------------
selectAndPaginate :: Map NgramsTerm NgramsElement -> [NgramsElement] -- selectAndPaginate :: Map NgramsTerm NgramsElement -> [NgramsElement]
selectAndPaginate tableMap = roots <> inners -- selectAndPaginate tableMap = roots <> inners
where -- where
list = tableMap ^.. each -- list = tableMap ^.. each
rootOf ne = maybe ne (\r -> fromMaybe (panic "getTableNgrams: invalid root") -- selected_nodes = list & take limit_
(tableMap ^. at r) -- . drop offset'
) -- . filter selected_node
(ne ^. ne_root) -- . sortOnOrder orderBy
selected_nodes = list & take limit_ -- roots = rootOf tableMap <$> selected_nodes
. drop offset' -- rootsSet = Set.fromList (_ne_ngrams <$> roots)
. filter selected_node -- inners = list & filter (selected_inner rootsSet)
. sortOnOrder orderBy
roots = rootOf <$> selected_nodes paginate :: [NgramsElement] -> [NgramsElement]
rootsSet = Set.fromList (_ne_ngrams <$> roots) paginate = take limit_
inners = list & filter (selected_inner rootsSet) . drop offset'
. sortOnOrder orderBy
--------------------------------------- ---------------------------------------
...@@ -590,16 +597,17 @@ getTableNgrams _nType nId tabType listId limit_ offset ...@@ -590,16 +597,17 @@ getTableNgrams _nType nId tabType listId limit_ offset
tableMap2 <- getNgramsTable' nId listId ngramsType orderBy tableMap2 <- getNgramsTable' nId listId ngramsType orderBy
-- TODO Refactor: `fltr` and `tableMap3` use very similar functions fltr <- tableMap2 & v_data %%~ fmap NgramsTable
let fmapScores = fmap NgramsTable . (setNgramsTableScores nId listId ngramsType (not scoresNeeded))
. (setNgramsTableScores nId listId ngramsType (not scoresNeeded)) . filteredNodes
printDebug "[getNgramsTable] fltr" fltr
fltr <- tableMap2 & v_data %%~ fmapScores . filteredNodes
let fltrCount = length $ fltr ^. v_data . _NgramsTable let fltrCount = length $ fltr ^. v_data . _NgramsTable
t2 <- getTime t2 <- getTime
tableMap3 <- tableMap2 & v_data %%~ fmapScores . selectAndPaginate --tableMap3 <- tableMap2 & v_data %%~ fmapScores . selectAndPaginate :: m (Versioned NgramsTable)
--let tableMap3 = fltr & v_data . _NgramsTable . each %%~ selectAndPaginate' :: Versioned NgramsTable
let tableMap3 = over (v_data . _NgramsTable) paginate fltr
t3 <- getTime t3 <- getTime
liftBase $ do liftBase $ do
hprint stderr hprint stderr
......
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