61 lines
1.7 KiB
TypeScript
61 lines
1.7 KiB
TypeScript
const CHINESE_NUMBER_VALUES: Record<string, number> = {
|
|
一: 1,
|
|
两: 2,
|
|
二: 2,
|
|
三: 3,
|
|
四: 4,
|
|
五: 5,
|
|
六: 6,
|
|
七: 7,
|
|
八: 8,
|
|
九: 9,
|
|
十: 10,
|
|
};
|
|
|
|
const COUNT_TOKEN = "([一两二三四五六七八九十]|[1-9]\\d?)";
|
|
const COUNT_UNIT = "(?:张|幅|版|个|组)";
|
|
|
|
function normalizeCountText(text: string) {
|
|
return String(text || "")
|
|
.toLowerCase()
|
|
.replace(/\s+/g, "")
|
|
.trim();
|
|
}
|
|
|
|
function parseCountToken(token?: string | null) {
|
|
const value = String(token || "").trim();
|
|
if (!value) return null;
|
|
if (/^\d+$/.test(value)) {
|
|
const numericValue = Number(value);
|
|
return Number.isFinite(numericValue) ? numericValue : null;
|
|
}
|
|
return CHINESE_NUMBER_VALUES[value] || null;
|
|
}
|
|
|
|
function clampImageCount(value: number, maxCount: number) {
|
|
return Math.min(Math.max(Math.round(value), 1), Math.max(1, maxCount));
|
|
}
|
|
|
|
export function inferAgentImageCountFromText(text: string, maxCount = 12) {
|
|
const normalizedText = normalizeCountText(text);
|
|
if (!normalizedText) return null;
|
|
|
|
const explicitCountPatterns = [
|
|
new RegExp(
|
|
`(?:再来|再出|再做|再给我|补|多来|继续生成|继续出|继续做)${COUNT_TOKEN}${COUNT_UNIT}?`
|
|
),
|
|
new RegExp(
|
|
`(?:生成|创作|创建|制作|设计|做|出|画|绘制)${COUNT_TOKEN}${COUNT_UNIT}(?:图|图片|海报|头像|插画|场景|主图|产品图|商品图|电商图|壁纸|封面|logo|图标)?`
|
|
),
|
|
new RegExp(`(?:来|出|做)${COUNT_TOKEN}${COUNT_UNIT}`),
|
|
];
|
|
|
|
for (const pattern of explicitCountPatterns) {
|
|
const match = normalizedText.match(pattern);
|
|
const count = parseCountToken(match?.[1]);
|
|
if (count !== null && count > 0) return clampImageCount(count, maxCount);
|
|
}
|
|
|
|
return null;
|
|
}
|