Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
open-webui
Commits
8e2c377a
Commit
8e2c377a
authored
Jun 22, 2024
by
Timothy J. Baek
Browse files
refac: extractSentences
parent
6ee94c5e
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
3 deletions
+22
-3
src/lib/utils/index.ts
src/lib/utils/index.ts
+22
-3
No files found.
src/lib/utils/index.ts
View file @
8e2c377a
...
...
@@ -511,12 +511,31 @@ export const removeFormattings = (str) => {
};
export
const
extractSentences
=
(
text
)
=>
{
// Split the paragraph into sentences based on common punctuation marks
const
sentences
=
text
.
split
(
/
(?<
=
[
.!?
])\s
+/
);
// This regular expression matches code blocks marked by triple backticks
const
codeBlockRegex
=
/```
[\s\S]
*
?
```/g
;
let
codeBlocks
=
[];
let
index
=
0
;
// Temporarily replace code blocks with placeholders and store the blocks separately
text
=
text
.
replace
(
codeBlockRegex
,
(
match
)
=>
{
let
placeholder
=
`\u0000
${
index
}
\u0000`
;
// Use a unique placeholder
codeBlocks
[
index
++
]
=
match
;
return
placeholder
;
});
// Split the modified text into sentences based on common punctuation marks, avoiding these blocks
let
sentences
=
text
.
split
(
/
(?<
=
[
.!?
])\s
+/
);
// Restore code blocks and process sentences
sentences
=
sentences
.
map
((
sentence
)
=>
{
// Check if the sentence includes a placeholder for a code block
return
sentence
.
replace
(
/
\u
0000
(\d
+
)\u
0000/g
,
(
_
,
idx
)
=>
codeBlocks
[
idx
]);
});
return
sentences
.
map
((
sentence
)
=>
removeFormattings
(
removeEmojis
(
sentence
.
trim
())))
.
filter
((
sentence
)
=>
sentence
!==
''
);
.
filter
((
sentence
)
=>
sentence
);
};
export
const
extractSentencesForAudio
=
(
text
)
=>
{
...
...
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