Commit 16138dfa authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

[nGrams] normalization for the selected text

parent 040119a1
...@@ -15,6 +15,7 @@ import Prelude ...@@ -15,6 +15,7 @@ import Prelude
import Data.Lens ((^?), _Just) import Data.Lens ((^?), _Just)
import Data.Lens.At (at) import Data.Lens.At (at)
import Data.Maybe ( Maybe(..), maybe, maybe' ) import Data.Maybe ( Maybe(..), maybe, maybe' )
import Data.String.Regex as R
import Data.String as S import Data.String as S
import Data.Tuple ( Tuple(..) ) import Data.Tuple ( Tuple(..) )
import Data.Tuple.Nested ( (/\) ) import Data.Tuple.Nested ( (/\) )
...@@ -27,7 +28,7 @@ import Reactix.SyntheticEvent as E ...@@ -27,7 +28,7 @@ import Reactix.SyntheticEvent as E
import Gargantext.Types ( TermList ) import Gargantext.Types ( TermList )
import Gargantext.Components.Annotation.Utils ( termBootstrapClass ) import Gargantext.Components.Annotation.Utils ( termBootstrapClass )
import Gargantext.Components.NgramsTable.Core ( NgramsTerm, NgramsTable(..), _NgramsElement, _list, highlightNgrams ) import Gargantext.Components.NgramsTable.Core ( NgramsTerm, NgramsTable(..), _NgramsElement, _list, highlightNgrams, nGramsRegex )
import Gargantext.Components.Annotation.Menu ( AnnotationMenu, annotationMenu, MenuType(..) ) import Gargantext.Components.Annotation.Menu ( AnnotationMenu, annotationMenu, MenuType(..) )
import Gargantext.Utils.Selection as Sel import Gargantext.Utils.Selection as Sel
...@@ -79,9 +80,10 @@ maybeShowMenu setMenu setTermList ngrams event = do ...@@ -79,9 +80,10 @@ maybeShowMenu setMenu setTermList ngrams event = do
sel' -> do sel' -> do
let x = E.clientX event let x = E.clientX event
y = E.clientY event y = E.clientY event
list = findNgram ngrams sel' sel'' = S.trim $ R.replace nGramsRegex " " sel'
list = findNgram ngrams sel''
setList t = do setList t = do
setTermList sel' list t setTermList sel'' list t
setMenu (const Nothing) setMenu (const Nothing)
E.preventDefault event E.preventDefault event
setMenu (const $ Just { x, y, list, menuType: NewNgram, setList }) setMenu (const $ Just { x, y, list, menuType: NewNgram, setList })
......
...@@ -16,6 +16,7 @@ module Gargantext.Components.NgramsTable.Core ...@@ -16,6 +16,7 @@ module Gargantext.Components.NgramsTable.Core
, VersionedNgramsTable , VersionedNgramsTable
, CoreState , CoreState
, LoadedNgramsTableProps , LoadedNgramsTableProps
, nGramsRegex
, highlightNgrams , highlightNgrams
, initialPageParams , initialPageParams
, loadNgramsTable , loadNgramsTable
...@@ -210,6 +211,14 @@ instance decodeJsonNgramsTable :: DecodeJson NgramsTable where ...@@ -210,6 +211,14 @@ instance decodeJsonNgramsTable :: DecodeJson NgramsTable where
f e@(NgramsElement e') = Tuple e'.ngrams e f e@(NgramsElement e') = Tuple e'.ngrams e
----------------------------------------------------------------------------------- -----------------------------------------------------------------------------------
nGramWordBoundaries = "[ .,;:!?'\\{}()]"
nGramsRegex = case R.regex ("(" <> nGramWordBoundaries <> ")") (R.global <> R.multiline) of
Left e -> unsafePartial $ crashWith e
Right r -> r
nGramsRegex2 = case R.regex ("(" <> nGramWordBoundaries <> ")\\1") (R.global <> R.multiline) of
Left e -> unsafePartial $ crashWith e
Right r -> r
-- TODO: while this function works well with word boundaries, -- TODO: while this function works well with word boundaries,
-- it inserts too many spaces. -- it inserts too many spaces.
highlightNgrams :: NgramsTable -> String -> Array (Tuple String (Maybe TermList)) highlightNgrams :: NgramsTable -> String -> Array (Tuple String (Maybe TermList))
...@@ -218,21 +227,14 @@ highlightNgrams (NgramsTable table) input0 = ...@@ -218,21 +227,14 @@ highlightNgrams (NgramsTable table) input0 =
let sN = unsafePartial (foldl goFold {i0: 0, s: input, l: Nil} ixs) in let sN = unsafePartial (foldl goFold {i0: 0, s: input, l: Nil} ixs) in
A.reverse (A.fromFoldable (consNonEmpty (undb (init sN.s)) sN.l)) A.reverse (A.fromFoldable (consNonEmpty (undb (init sN.s)) sN.l))
where where
spR x = " " <> R.replace theRegex "$1$1" x <> " " spR x = " " <> R.replace nGramsRegex "$1$1" x <> " "
reR = R.replace theRegex " " reR = R.replace nGramsRegex " "
db = S.replace (S.Pattern " ") (S.Replacement " ") db = S.replace (S.Pattern " ") (S.Replacement " ")
sp x = " " <> db x <> " " sp x = " " <> db x <> " "
undb = R.replace theRegex2 "$1" undb = R.replace nGramsRegex2 "$1"
init x = S.take (S.length x - 1) x init x = S.take (S.length x - 1) x
input = spR input0 input = spR input0
pats = A.fromFoldable (Map.keys table) pats = A.fromFoldable (Map.keys table)
word_boundaries = "[ .,;:!?'\\{}()]"
theRegex = case R.regex ("(" <> word_boundaries <> ")") (R.global <> R.multiline) of
Left e -> unsafePartial $ crashWith e
Right r -> r
theRegex2 = case R.regex ("(" <> word_boundaries <> ")\\1") (R.global <> R.multiline) of
Left e -> unsafePartial $ crashWith e
Right r -> r
ixs = indicesOfAny (sp <$> pats) (S.toLower $ reR input) ixs = indicesOfAny (sp <$> pats) (S.toLower $ reR input)
consOnJustTail s xs@(Tuple _ (Just _) : _) = consOnJustTail s xs@(Tuple _ (Just _) : _) =
......
...@@ -28,7 +28,8 @@ endConfig = endConfig' V10 ...@@ -28,7 +28,8 @@ endConfig = endConfig' V10
endConfig' :: ApiVersion -> EndConfig endConfig' :: ApiVersion -> EndConfig
endConfig' v = { front : frontRelative endConfig' v = { front : frontRelative
, back : backLocal v --, back : backLocal v
, back: backDev v
, static : staticRelative , static : staticRelative
} }
-- , back : backDemo v } -- , back : backDemo v }
......
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