[NGRAMS] local sort over the sum of occurrences

parent 40e5cc4b
...@@ -49,6 +49,7 @@ import Data.Map (Map) ...@@ -49,6 +49,7 @@ import Data.Map (Map)
import Data.Map as Map import Data.Map as Map
import Data.Maybe (Maybe(..), maybe) import Data.Maybe (Maybe(..), maybe)
import Data.Monoid.Additive (Additive(..)) import Data.Monoid.Additive (Additive(..))
import Data.Ord.Down (Down(..))
import Data.Traversable (class Traversable, traverse, traverse_, sequence) import Data.Traversable (class Traversable, traverse, traverse_, sequence)
import Data.TraversableWithIndex (class TraversableWithIndex, traverseWithIndex) import Data.TraversableWithIndex (class TraversableWithIndex, traverseWithIndex)
import Data.Set (Set) import Data.Set (Set)
...@@ -57,7 +58,7 @@ import Data.String as S ...@@ -57,7 +58,7 @@ import Data.String as S
import Data.String.Regex as R import Data.String.Regex as R
import Data.String.Regex.Flags as R import Data.String.Regex.Flags as R
import Data.Symbol (SProxy(..)) import Data.Symbol (SProxy(..))
import Data.Tuple (Tuple(..)) import Data.Tuple (Tuple(..), snd)
import Effect (Effect) import Effect (Effect)
import Effect.Aff (Aff) import Effect.Aff (Aff)
import Foreign.Object as FO import Foreign.Object as FO
...@@ -716,7 +717,16 @@ ngramsTableSpec = simpleSpec performAction render ...@@ -716,7 +717,16 @@ ngramsTableSpec = simpleSpec performAction render
setParams params = setParams params =
loaderDispatch $ Loader.SetPath $ pageParams {params = params} loaderDispatch $ Loader.SetPath $ pageParams {params = params}
ngramsTable = applyNgramsTablePatch ngramsTablePatch initTable ngramsTable = applyNgramsTablePatch ngramsTablePatch initTable
rows = convertRow <$> Map.toUnfoldable (Map.filter displayRow (ngramsTable ^. _NgramsTable)) orderWith =
case convOrderBy <$> pageParams.params.orderBy of
Just ScoreAsc -> A.sortWith \x -> (snd x) ^. _NgramsElement <<< _occurrences
Just ScoreDesc -> A.sortWith \x -> Down $ (snd x) ^. _NgramsElement <<< _occurrences
_ -> identity -- the server ordering is enough here
rows = convertRow <$> orderWith (addOcc <$> Map.toUnfoldable (Map.filter displayRow (ngramsTable ^. _NgramsTable)))
addOcc (Tuple ne ngramsElement) =
let Additive occurrences = sumOccurrences ngramsTable ngramsElement in
Tuple ne (ngramsElement # _NgramsElement <<< _occurrences .~ occurrences)
ngramsParentRoot :: Maybe String ngramsParentRoot :: Maybe String
ngramsParentRoot = ngramsParentRoot =
...@@ -734,11 +744,15 @@ ngramsTableSpec = simpleSpec performAction render ...@@ -734,11 +744,15 @@ ngramsTableSpec = simpleSpec performAction render
|| -- Unless they are scheduled to be removed. || -- Unless they are scheduled to be removed.
ngramsChildren ^. at ngrams == Just false ngramsChildren ^. at ngrams == Just false
convertRow (Tuple ngrams ngramsElement) = convertRow (Tuple ngrams ngramsElement) =
{ row: { row: renderNgramsItem { ngramsTable, ngrams, ngramsParent, ngramsElement, dispatch}
renderNgramsItem { ngramsTable, ngrams, ngramsParent, ngramsElement, dispatch }
, delete: false , delete: false
} }
convOrderBy (T.ASC (T.ColumnName "Score (Occurrences)")) = ScoreAsc
convOrderBy (T.DESC (T.ColumnName "Score (Occurrences)")) = ScoreDesc
convOrderBy (T.ASC _) = TermAsc
convOrderBy (T.DESC _) = TermDesc
loadNgramsTable :: PageParams -> Aff VersionedNgramsTable loadNgramsTable :: PageParams -> Aff VersionedNgramsTable
loadNgramsTable { nodeId, listIds, termListFilter, termSizeFilter loadNgramsTable { nodeId, listIds, termListFilter, termSizeFilter
, searchQuery, tabType, params: {offset, limit, orderBy}} = , searchQuery, tabType, params: {offset, limit, orderBy}} =
...@@ -749,11 +763,6 @@ loadNgramsTable { nodeId, listIds, termListFilter, termSizeFilter ...@@ -749,11 +763,6 @@ loadNgramsTable { nodeId, listIds, termListFilter, termSizeFilter
, searchQuery , searchQuery
}) })
(Just nodeId) (Just nodeId)
where
convOrderBy (T.ASC (T.ColumnName "Score (Occurrences)")) = ScoreAsc
convOrderBy (T.DESC (T.ColumnName "Score (Occurrences)")) = ScoreDesc
convOrderBy (T.ASC _) = TermAsc
convOrderBy (T.DESC _) = TermDesc
ngramsLoaderClass :: Loader.LoaderClass PageParams VersionedNgramsTable ngramsLoaderClass :: Loader.LoaderClass PageParams VersionedNgramsTable
ngramsLoaderClass = Loader.createLoaderClass "NgramsTableLoader" loadNgramsTable ngramsLoaderClass = Loader.createLoaderClass "NgramsTableLoader" loadNgramsTable
...@@ -842,10 +851,9 @@ renderNgramsItem { ngramsTable, ngrams, ngramsElement, ngramsParent, dispatch } ...@@ -842,10 +851,9 @@ renderNgramsItem { ngramsTable, ngrams, ngramsElement, ngramsParent, dispatch }
[ i [className "fas fa-plus"] [] [ i [className "fas fa-plus"] []
, span ngramsStyle [text $ " " <> ngrams] , span ngramsStyle [text $ " " <> ngrams]
] ]
, text $ show occurrences , text $ show (ngramsElement ^. _NgramsElement <<< _occurrences)
] ]
where where
Additive occurrences = sumOccurrences ngramsTable ngramsElement
termList = ngramsElement ^. _NgramsElement <<< _list termList = ngramsElement ^. _NgramsElement <<< _list
ngramsStyle = [termStyle termList] ngramsStyle = [termStyle termList]
ngramsClick = Just <<< dispatch <<< SetParentResetChildren <<< Just <<< view _ngrams ngramsClick = Just <<< dispatch <<< SetParentResetChildren <<< Just <<< view _ngrams
......
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