Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
purescript-gargantext
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Grégoire Locqueville
purescript-gargantext
Commits
f72fd28b
Commit
f72fd28b
authored
Apr 09, 2019
by
James Laver
Committed by
Alexandre Delanoë
Apr 28, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add Gargantext.Text.BreakWords for word breaking
parent
2a5de574
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
88 additions
and
0 deletions
+88
-0
BreakWords.js
src/Gargantext/Text/BreakWords.js
+4
-0
BreakWords.purs
src/Gargantext/Text/BreakWords.purs
+84
-0
No files found.
src/Gargantext/Text/BreakWords.js
0 → 100644
View file @
f72fd28b
exports
.
_wordRegex
=
/
[
a-z
]
+/gi
;
exports
.
_cloneRegex
=
function
(
r
)
{
return
new
RegExp
(
r
.
source
,
r
.
flags
);
};
exports
.
_getRegexLastIndex
=
function
(
r
)
{
return
r
.
lastIndex
;
};
exports
.
_execRegex
=
function
(
r
,
s
)
{
return
r
.
exec
(
s
);
};
src/Gargantext/Text/BreakWords.purs
0 → 100644
View file @
f72fd28b
-- | Break a string into words and spaces
-- | It uses a simple algorithm of searching for word characters incrementally
-- | Punctuation is considered whitespace, so it's best used in a sentence or
-- | for highlighting purposes
module Gargantext.Text.BreakWords (BrokenWord(..), breakWords) where
import Prelude
import Data.Traversable (traverse_)
import Effect (Effect)
import Data.Maybe (Maybe(..))
import Data.Unit (Unit, unit)
import Effect.Uncurried (EffectFn2, runEffectFn2)
import Data.Function.Uncurried (Fn1, runFn1)
import Data.String.CodeUnits (length, slice) -- TODO: double check i'm the right choice
import Data.Nullable (Nullable, toMaybe)
import Data.String.Regex (Regex)
import Gargantext.Utils.Regex
import Gargantext.Utils.Array (push)
data BrokenWord = Word String | Space String
breakWords :: String -> Effect (Array BrokenWord)
breakWords s = loop $ break s
where loop b = breakNext b >>= (h b)
h :: Breaking -> Boolean -> Effect (Array BrokenWord)
h b cont
| cont = loop b
| otherwise = pure b.results
-- Implementation
-- Returns whether to continue
breakNext :: Breaking -> Effect Boolean
breakNext b = checkStatic b $ lastIndex b
where checkStatic b origin
| origin == length b.source = pure false
| otherwise = search b >>= next' origin
next' origin Nothing = finish b origin
next' origin (Just w) = next b origin w
next :: Breaking -> Int -> String -> Effect Boolean
next b origin word =
do traverse_ (pushSpace b) $ preceding b origin word
pushWord b word
pure true
preceding :: Breaking -> Int -> String -> Maybe String
preceding b origin word = p $ (lastIndex b) - (length word)
where p o
| o == origin = Nothing
| otherwise = slice origin o b.source
finish :: Breaking -> Int -> Effect Boolean
finish b origin =
do let last = slice origin (-1) b.source
traverse_ (pushSpace b) last
pure false
type Breaking = { source :: String, wordRegex :: Regex, results :: Array BrokenWord }
-- almost `pure`
break :: String -> Breaking
break s = { source, wordRegex, results }
where source = s
wordRegex = cloneRegex _wordRegex
results = []
search :: Breaking -> Effect (Maybe String)
search b = execRegex b.wordRegex b.source
lastIndex :: Breaking -> Int
lastIndex b = getRegexLastIndex b.wordRegex
pushResult :: Breaking -> BrokenWord -> Effect Unit
pushResult b = push b.results
pushSpace :: Breaking -> String -> Effect Unit
pushSpace b = pushResult b <<< Space
pushWord :: Breaking -> String -> Effect Unit
pushWord b = pushResult b <<< Word
foreign import _wordRegex :: Regex
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment