mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Add Handling for Dangling Characters in "trimToEndSentence" - utils.js
New: Considered the presence of whitespace or newline characters preceding the punctuation for accurate trimming.
This commit is contained in:
@ -471,14 +471,18 @@ export function sortByCssOrder(a, b) {
|
|||||||
* trimToEndSentence('Hello, world! I am from'); // 'Hello, world!'
|
* trimToEndSentence('Hello, world! I am from'); // 'Hello, world!'
|
||||||
*/
|
*/
|
||||||
export function trimToEndSentence(input, include_newline = false) {
|
export function trimToEndSentence(input, include_newline = false) {
|
||||||
const punctuation = new Set(['.', '!', '?', '*', '"', ')', '}', '`', ']', '$', '。', '!', '?', '”', ')', '】', '】', '’', '」', '】']); // extend this as you see fit
|
const punctuation = new Set(['.', '!', '?', '*', '"', ')', '}', '`', ']', '$', '。', '!', '?', '”', ')', '】', '’', '」']); // extend this as you see fit
|
||||||
let last = -1;
|
let last = -1;
|
||||||
|
|
||||||
for (let i = input.length - 1; i >= 0; i--) {
|
for (let i = input.length - 1; i >= 0; i--) {
|
||||||
const char = input[i];
|
const char = input[i];
|
||||||
|
|
||||||
if (punctuation.has(char)) {
|
if (punctuation.has(char)) {
|
||||||
|
if (i > 0 && (input[i - 1] === ' ' || input[i - 1] === '\n')) {
|
||||||
|
last = i - 1;
|
||||||
|
} else {
|
||||||
last = i;
|
last = i;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user