[NGRAMS-REPO] sum occurrencies

parent a101ea49
...@@ -28,7 +28,7 @@ import Data.Foldable (class Foldable, foldMap, foldl, foldr) ...@@ -28,7 +28,7 @@ import Data.Foldable (class Foldable, foldMap, foldl, foldr)
import Data.FoldableWithIndex (class FoldableWithIndex, foldMapWithIndex, foldlWithIndex, foldrWithIndex) import Data.FoldableWithIndex (class FoldableWithIndex, foldMapWithIndex, foldlWithIndex, foldrWithIndex)
import Data.FunctorWithIndex (class FunctorWithIndex, mapWithIndex) import Data.FunctorWithIndex (class FunctorWithIndex, mapWithIndex)
import Data.Newtype (class Newtype) import Data.Newtype (class Newtype)
import Data.Lens (Iso', Lens', (%~), (.=), (^.), (^..)) import Data.Lens (Iso', Lens', (%~), (.=), (^.), (^..), to)
import Data.Lens.Common (_Just) import Data.Lens.Common (_Just)
import Data.Lens.At (class At, at) import Data.Lens.At (class At, at)
import Data.Lens.Index (class Index, ix) import Data.Lens.Index (class Index, ix)
...@@ -39,6 +39,7 @@ import Data.List as List ...@@ -39,6 +39,7 @@ import Data.List as List
import Data.Map (Map) 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.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)
...@@ -100,6 +101,10 @@ newtype NgramsElement = NgramsElement ...@@ -100,6 +101,10 @@ newtype NgramsElement = NgramsElement
_parent = prop (SProxy :: SProxy "parent") _parent = prop (SProxy :: SProxy "parent")
_children :: forall row. Lens' { children :: Set NgramsTerm | row } (Set NgramsTerm) _children :: forall row. Lens' { children :: Set NgramsTerm | row } (Set NgramsTerm)
_children = prop (SProxy :: SProxy "children") _children = prop (SProxy :: SProxy "children")
_occurrences :: forall row. Lens' { occurrences :: Int | row } Int
_occurrences = prop (SProxy :: SProxy "occurrences")
_list :: forall a row. Lens' { list :: a | row } a
_list = prop (SProxy :: SProxy "list")
derive instance newtypeNgramsElement :: Newtype NgramsElement _ derive instance newtypeNgramsElement :: Newtype NgramsElement _
...@@ -283,7 +288,7 @@ applyNgramsPatch :: NgramsPatch -> NgramsElement -> NgramsElement ...@@ -283,7 +288,7 @@ applyNgramsPatch :: NgramsPatch -> NgramsElement -> NgramsElement
applyNgramsPatch (NgramsPatch p) (NgramsElement e) = NgramsElement applyNgramsPatch (NgramsPatch p) (NgramsElement e) = NgramsElement
{ ngrams: e.ngrams { ngrams: e.ngrams
, list: applyReplace p.patch_list e.list , list: applyReplace p.patch_list e.list
, occurrences: e.occurrences -- TODO: is this correct ? , occurrences: e.occurrences
, parent: e.parent , parent: e.parent
, children: applyPatchSet p.patch_children e.children , children: applyPatchSet p.patch_children e.children
} }
...@@ -614,9 +619,9 @@ ngramsTableSpec = simpleSpec performAction render ...@@ -614,9 +619,9 @@ ngramsTableSpec = simpleSpec performAction render
Nothing -> true) Nothing -> true)
|| -- 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 { occurrences, list })) = convertRow (Tuple ngrams ngramsElement) =
{ row: { row:
renderNgramsItem { ngramsTable, ngrams, occurrences, ngramsParent, termList: list, dispatch } renderNgramsItem { ngramsTable, ngrams, ngramsParent, ngramsElement, dispatch }
, delete: false , delete: false
} }
...@@ -681,6 +686,14 @@ tree params@{ngramsTable, ngramsStyle, ngramsClick} label = ...@@ -681,6 +686,14 @@ tree params@{ngramsTable, ngramsStyle, ngramsClick} label =
forest = ul [] <<< map (tree params) <<< List.toUnfoldable forest = ul [] <<< map (tree params) <<< List.toUnfoldable
sumOccurrences' :: NgramsTable -> NgramsTerm -> Additive Int
sumOccurrences' ngramsTable label =
ngramsTable ^. ix label <<< to (sumOccurrences ngramsTable)
sumOccurrences :: NgramsTable -> NgramsElement -> Additive Int
sumOccurrences ngramsTable (NgramsElement {occurrences, children}) =
Additive occurrences <> children ^. folded <<< to (sumOccurrences' ngramsTable)
renderNgramsTree :: { ngrams :: NgramsTerm renderNgramsTree :: { ngrams :: NgramsTerm
, ngramsTable :: NgramsTable , ngramsTable :: NgramsTable
, ngramsStyle :: Array DOM.Props , ngramsStyle :: Array DOM.Props
...@@ -693,12 +706,11 @@ renderNgramsTree { ngramsTable, ngrams, ngramsStyle, ngramsClick } = ...@@ -693,12 +706,11 @@ renderNgramsTree { ngramsTable, ngrams, ngramsStyle, ngramsClick } =
renderNgramsItem :: { ngrams :: NgramsTerm renderNgramsItem :: { ngrams :: NgramsTerm
, ngramsTable :: NgramsTable , ngramsTable :: NgramsTable
, occurrences :: Int , ngramsElement :: NgramsElement
, termList :: TermList
, ngramsParent :: Maybe NgramsTerm , ngramsParent :: Maybe NgramsTerm
, dispatch :: Action -> Effect Unit , dispatch :: Action -> Effect Unit
} -> Array ReactElement } -> Array ReactElement
renderNgramsItem { ngramsTable, ngrams, occurrences, termList, ngramsParent, dispatch } = renderNgramsItem { ngramsTable, ngrams, ngramsElement, ngramsParent, dispatch } =
[ checkbox GraphTerm [ checkbox GraphTerm
, checkbox StopTerm , checkbox StopTerm
, if ngramsParent == Nothing , if ngramsParent == Nothing
...@@ -711,6 +723,8 @@ renderNgramsItem { ngramsTable, ngrams, occurrences, termList, ngramsParent, dis ...@@ -711,6 +723,8 @@ renderNgramsItem { ngramsTable, ngrams, occurrences, termList, ngramsParent, dis
, text $ show occurrences , text $ show occurrences
] ]
where where
Additive occurrences = sumOccurrences ngramsTable ngramsElement
termList = ngramsElement ^. _NgramsElement <<< _list
ngramsStyle = [termStyle termList] ngramsStyle = [termStyle termList]
ngramsClick = Just <<< dispatch <<< SetParentResetChildren <<< Just ngramsClick = Just <<< dispatch <<< SetParentResetChildren <<< Just
checkbox termList' = checkbox termList' =
......
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