const counts: Record = { 一: 1, 二: 2, 两: 2, 三: 3, 四: 4, 五: 5, 六: 6, 七: 7, 八: 8, 九: 9, 十: 10, }; const count = "([1-9]\\d?|[一二两三四五六七八九十])"; const number = (text: string) => counts[text] ?? Number(text); export function inferCanvasTaskRequirements(text: string, selectedCount = 0) { const compact = text.replace(/\s+/g, ""); const source = compact.match(new RegExp(`${count}张(?:商品|产品|参考)?图`)); const per = compact.match( new RegExp(`每(?:张|款|个)(?:图)?(?:出|做|生成|制作)?${count}(?:张|版)`), ); const total = compact.match(new RegExp(`(?:总共|共计|最终|一共)(?:出|做|生成)?${count}张`)) ?? (!per ? compact.match(new RegExp(`(?:生成|做|出)${count}张`)) : null); const expectedImageCount = total ? number(total[1]) : per && (selectedCount || source) ? number(per[1]) * (selectedCount || number(source![1])) : undefined; const ratios = Array.from( new Set( Array.from(compact.matchAll(/(\d+(?:\.\d+)?)[::](\d+(?:\.\d+)?)/g)) .filter((match) => Number(match[1]) > 0 && Number(match[2]) > 0) .map((match) => `${match[1]}:${match[2]}`), ), ); return { expectedImageCount, aspectRatio: ratios.length === 1 ? ratios[0] : undefined }; }