From b38c94847e2f14f62f86e242de3b907506db0bb0 Mon Sep 17 00:00:00 2001 From: cherishsince Date: Sat, 6 Apr 2024 20:32:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0contentparse=20=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../framework/ai/midjourney/util/MjUtil.java | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/util/MjUtil.java diff --git a/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/util/MjUtil.java b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/util/MjUtil.java new file mode 100644 index 000000000..19ad63184 --- /dev/null +++ b/yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/util/MjUtil.java @@ -0,0 +1,68 @@ +package cn.iocoder.yudao.framework.ai.midjourney.util; + +import cn.hutool.core.text.CharSequenceUtil; +import cn.iocoder.yudao.framework.ai.midjourney.MjMessage; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * author: fansili + * time: 2024/4/6 19:00 + */ +public class MjUtil { + /** + * content正则匹配prompt和进度. + */ + public static final String CONTENT_REGEX = ".*?\\*\\*(.*?)\\*\\*.+<@\\d+> \\((.*?)\\)"; + public static final String CONTENT_PROGRESS_REGEX = "\\(([^)]*)\\)"; + + + public static MjMessage.Content parseContent(String content) { + // "**南极应该是什么样子? --v 6.0 --style raw** - <@972721304891453450> (32%) (fast, stealth)", + // "**南极应该是什么样子? --v 6.0 --style raw** - <@972721304891453450> (fast, stealth)" + MjMessage.Content mjContent = new MjMessage.Content(); + if (CharSequenceUtil.isBlank(content)) { + return null; + } + if (!content.contains("raw**")) { + return mjContent.setPrompt(content); + } + int rawIndex = content.indexOf("raw**") + 5; + String prompt = content.substring(0, rawIndex).trim(); + String contentTail = content.substring(rawIndex).trim(); + // 检查是否存在进度条 + Pattern pattern = Pattern.compile(CONTENT_PROGRESS_REGEX); + Matcher matcher = pattern.matcher(contentTail); + + if (contentTail.contains("%")) { + if (matcher.find()) { + // 获取第一个(也是此处唯一的)捕获组的内容 + String progress = matcher.group(1); + mjContent.setProgress(progress); + } + if (matcher.find()) { + String status = matcher.group(1); + mjContent.setStatus(status); + } + } else { + if (matcher.find()) { + // 获取第一个(也是此处唯一的)捕获组的内容 + String status = matcher.group(1); + mjContent.setStatus(status); + } + } + mjContent.setPrompt(prompt); + // tip:contentArray + return mjContent; + } + + public static void main(String[] args) { + String content1 = "**南极应该是什么样子? --v 6.0 --style raw** - <@972721304891453450> (32%) (fast, stealth)"; + String content2 = "**南极应该是什么样子? --v 6.0 --style raw** - <@972721304891453450> (fast, stealth)"; + + System.err.println(parseContent(content1));; + System.err.println(parseContent(content2));; + } + +}