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

[nGrams] normalization for the selected text

parent 040119a1
......@@ -15,6 +15,7 @@ import Prelude
import Data.Lens ((^?), _Just)
import Data.Lens.At (at)
import Data.Maybe ( Maybe(..), maybe, maybe' )
import Data.String.Regex as R
import Data.String as S
import Data.Tuple ( Tuple(..) )
import Data.Tuple.Nested ( (/\) )
......@@ -27,7 +28,7 @@ import Reactix.SyntheticEvent as E
import Gargantext.Types ( TermList )
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.Utils.Selection as Sel
......@@ -79,9 +80,10 @@ maybeShowMenu setMenu setTermList ngrams event = do
sel' -> do
let x = E.clientX event
y = E.clientY event
list = findNgram ngrams sel'
sel'' = S.trim $ R.replace nGramsRegex " " sel'
list = findNgram ngrams sel''
setList t = do
setTermList sel' list t
setTermList sel'' list t
setMenu (const Nothing)
E.preventDefault event
setMenu (const $ Just { x, y, list, menuType: NewNgram, setList })
......
......@@ -16,6 +16,7 @@ module Gargantext.Components.NgramsTable.Core
, VersionedNgramsTable
, CoreState
, LoadedNgramsTableProps
, nGramsRegex
, highlightNgrams
, initialPageParams
, loadNgramsTable
......@@ -210,6 +211,14 @@ instance decodeJsonNgramsTable :: DecodeJson NgramsTable where
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,
-- it inserts too many spaces.
highlightNgrams :: NgramsTable -> String -> Array (Tuple String (Maybe TermList))
......@@ -218,21 +227,14 @@ highlightNgrams (NgramsTable table) input0 =
let sN = unsafePartial (foldl goFold {i0: 0, s: input, l: Nil} ixs) in
A.reverse (A.fromFoldable (consNonEmpty (undb (init sN.s)) sN.l))
where
spR x = " " <> R.replace theRegex "$1$1" x <> " "
reR = R.replace theRegex " "
spR x = " " <> R.replace nGramsRegex "$1$1" x <> " "
reR = R.replace nGramsRegex " "
db = S.replace (S.Pattern " ") (S.Replacement " ")
sp x = " " <> db x <> " "
undb = R.replace theRegex2 "$1"
undb = R.replace nGramsRegex2 "$1"
init x = S.take (S.length x - 1) x
input = spR input0
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)
consOnJustTail s xs@(Tuple _ (Just _) : _) =
......
......@@ -28,7 +28,8 @@ endConfig = endConfig' V10
endConfig' :: ApiVersion -> EndConfig
endConfig' v = { front : frontRelative
, back : backLocal v
--, back : backLocal v
, back: backDev v
, static : staticRelative
}
-- , 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