From cec5074dcede0491576d9da7fcb16d24401403ef Mon Sep 17 00:00:00 2001 From: kwaroran Date: Fri, 26 May 2023 03:38:02 +0900 Subject: [PATCH] [feat] better regex --- src/lang/en.ts | 29 +++++++++++++++++------------ src/lang/ko.ts | 22 ++++++++++++++-------- src/ts/process/scripts.ts | 22 +++++++++++----------- 3 files changed, 42 insertions(+), 31 deletions(-) diff --git a/src/lang/en.ts b/src/lang/en.ts index c1588aa0..e9de7f78 100644 --- a/src/lang/en.ts +++ b/src/lang/en.ts @@ -51,13 +51,19 @@ export const languageEnglish = { + "\n- if the key starts with **$**, the key's value will more likely to change." + "\n\nwhen the image is first generated, you can only change it by modifying 'Current Image Generation Data' in below.", - regexScript: "Regex Script is a custom script that replaces string that matches IN to OUT.\n\nThere are three type options." - + "- **Modify Input** modifys user's input" - + "- **Modify Output** modifys character's output" - + "- **Modify Request Data** modifys current chat data when sent.\n\nIN must be a regex without flags and without slashes in start and end.\n\nOUT is a normal string." - + "\n\n If OUT has {{data}} in string, it replaces to matched string." - + "\n\n If OUT starts with **@@**, it doesn't replaces the string, but instead does a special effect if matching string founds." - + "\n\n- @@emo (emotion name)\n\n if character is Emotion Images mode, sets (emotion name) as emotion and prevents default.", + regexScript: "Regex Script is a custom script that replaces string that matches IN to OUT.\n\nThere four type options." + + "\n\n- **Modify Input** modifys user's input" + + "\n\n- **Modify Output** modifys character's output" + + "\n\n- **Modify Request Data** modifys current chat data when sent." + + "\n\n- **Modify Display** just modifies the text when shown without modifying chat data." + + "\n\nIN must be a regex without flags and without slashes in start and end.\n\nOUT is a string which can include replacement patterns. these are the patterns:" + + "\n\n- $$\n\n - inserts $" + + "\n\n- $&\n\n - inserts the matched substring." + + "\n\n- $`\n\n - inserts the portion of the string that precedes the matched substring." + + "\n\n- $1\n\n - inserts the first matching group. works with other number like 2, 3..." + + "\n\n- $(name)\n\n - inserts the named group" + + "\n\nIf OUT starts with **@@**, it doesn't replaces the string, but instead does a special effect if matching string founds." + + "\n\n- @@emo (emotion name)\n\n - if character is Emotion Images mode, sets (emotion name) as emotion and prevents default.", experimental: "This is a experimental setting. it might be unstable.", oogaboogaURL: "If your WebUI supports older version of api, your url should look *like https:.../run/textgen*\n\n" + "If your WebUI supports newVersion of api, your url should look like *https://.../api/v1/generate* and use the api server as host, and add --api to arguments.", @@ -224,9 +230,9 @@ export const languageEnglish = { sayNothing:"Input 'say nothing' when no string inputed", regexScript: "Regex Script", type: "Type", - editInput: "Modfiy Input", - editOutput: "Modfiy Output", - editProcess: "Modfiy Request Data", + editInput: "Modify Input", + editOutput: "Modify Output", + editProcess: "Modify Request Data", loadLatest: "Load Latest Backup", loadOthers: "Load Other Backups", exampleMessage: "Example Message", @@ -271,6 +277,5 @@ export const languageEnglish = { replaceGlobalNote: "Global Note Replacement", charLoreBook: 'Character Lorebook', globalLoreBook: 'Global Lorebook', - globalRegexScript: "Regex Script", - + globalRegexScript: "Global Regex", } \ No newline at end of file diff --git a/src/lang/ko.ts b/src/lang/ko.ts index 1822faef..5ebbd3ff 100644 --- a/src/lang/ko.ts +++ b/src/lang/ko.ts @@ -180,13 +180,19 @@ export const languageKorean = { + "\n- 키의 이름이 **$** 로 시작할 시, 값은 더 자주 변합니다." + "\n\n이미지가 처음 생성된 이후부터는 '현재 이미지 생성 데이터'를 수정하여 변경할 수 있습니다.", experimental: "실험적 기능입니다. 불안정할 수 있습니다.", - regexScript: "정규식 스크립트는 IN의 조건에 맞는 문자열을 OUT으로 변경합니다.\n\n타입은 세가지가 있습니다." - + "- **입력문 수정** 유저의 입력문을 수정합니다" - + "- **출력문 수정** 캐릭터의 출력문을 수정합니다" - + "- **리퀘스트 데이터 수정** 리퀘스트를 보낼 때 채팅 데이터를 수정합니다.\n\nIN은 flag가 없고, 양끝에 슬레시가 없는 Regex여야 합니다.\n\nOUT은 일반 문자열입니다." - + "\n\n 만약 OUT 문자열에 {{data}}가 있으면, 매칭된 문자열로 바뀝니다." - + "\n\n 만약 OUT이 @@로 시작된다면, 특수한 효과를 냅니다" - + "\n\n- @@emo (emotion name)\n\n 감정 이미지 모드일 시 (emotion name)을 감정으로 정하고 감정 처리를 하지 않습니다.", + regexScript: "정규식 스크립트는 IN에 매칭되는 텍스트를 OUT으로 변경하는 스크립트입니다.\n\n네가지 옵션이 있습니다." + + "\n\n- **입력문 수정** 유저의 입력을 수정합니다" + + "\n\n- **출력문 수정** 캐릭터의 출력을 수정합니다" + + "\n\n- **리퀘스트 데이터 수정** 리퀘스트 시 데이터를 수정합니다" + + "\n\n- **디스플레이 수정** 실제 데이터를 건들지 않고, 표시되는 것만 수정합니다." + + "\n\nIN은 슬래시와 플레그가 없는 정규식이여야 합니다.\n\nOUT은 특수한 패턴이 포함될수 있는 문자열입니다. 패턴은 다음과 같습니다:" + + "\n\n- $$\n\n - inserts $" + + "\n\n- $&\n\n - inserts the matched substring." + + "\n\n- $`\n\n - inserts the portion of the string that precedes the matched substring." + + "\n\n- $1\n\n - inserts the first matching group. works with other number like 2, 3..." + + "\n\n- $(name)\n\n - inserts the named group" + + "\n\nOUT이 **@@**로 시작 시, 문자열을 교체하지 않고, 특수한 효과를 냅니다. 효과는 다음과 같습니다:" + + "\n\n- @@emo (emotion name)\n\n - 캐릭터가 감정 이미지 모드일시, 감정을 (emotion name)으로 정하고 감정을 더이상 교체하지 않습니다.", }, setup: { chooseProvider: "AI 제공자를 선택해 주세요", @@ -250,5 +256,5 @@ export const languageKorean = { replaceGlobalNote: "글로벌 노트 덮어쓰기", charLoreBook: '캐릭터 로어북', globalLoreBook: '글로벌 로어북', - globalRegexScript: "글로별 정규식 스크립트", + globalRegexScript: "글로별 정규식", } \ No newline at end of file diff --git a/src/ts/process/scripts.ts b/src/ts/process/scripts.ts index 0a82f2e0..dc98cd61 100644 --- a/src/ts/process/scripts.ts +++ b/src/ts/process/scripts.ts @@ -3,6 +3,7 @@ import { CharEmotion, selectedCharID } from "../stores"; import { DataBase, type character } from "../database"; const dreg = /{{data}}/g +const randomness = /\|\|\|/g type ScriptMode = 'editinput'|'editoutput'|'editprocess'|'editdisplay' @@ -17,15 +18,9 @@ export function processScriptFull(char:character, data:string, mode:ScriptMode){ for (const script of scripts){ if(script.type === mode){ const reg = new RegExp(script.in,'g') - data = data.replace(reg, (v) => { - const outScript = script.out.replace(dreg, v) + const outScript = script.out + if(outScript.startsWith('@@') && reg.test(data)){ if(outScript.startsWith('@@emo ')){ - if(char.viewScreen !== 'emotion'){ - return v - } - if(emoChanged){ - return v - } const emoName = script.out.substring(6).trim() let charemotions = get(CharEmotion) let tempEmotion = charemotions[char.chaId] @@ -45,10 +40,15 @@ export function processScriptFull(char:character, data:string, mode:ScriptMode){ break } } - return v } - return outScript - }) + } + else{ + if(randomness.test(data)){ + const list = data.split('|||') + data = list[Math.floor(Math.random()*list.length)]; + } + data = data.replace(reg, outScript.replace(dreg, "$&")) + } } } return {data, emoChanged}