You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mail-yubao/scripts/patch-split-segment-kinds.ts

109 lines
3.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import fs from "fs";
const p = "src/services/parse/split-instructions.ts";
let t = fs.readFileSync(p, "utf8");
t = t.replace(
`import {
ATTACHMENT_HINT_RE,
KEYWORD_RULES,
SEGMENT_BOUNDARY_PATTERNS,
SEGMENT_START_LINE_RE,
matchKeywordRules,
type InstructionUiKind,
} from "@/services/parse/instruction-lexicon";`,
`import {
ATTACHMENT_HINT_RE,
BODY_ACTION_RE,
KEYWORD_RULES,
SEGMENT_BOUNDARY_PATTERNS,
SEGMENT_START_LINE_RE,
isNonInstructionSegment,
matchKeywordRules,
type InstructionUiKind,
} from "@/services/parse/instruction-lexicon";`,
);
const oldDetect = `export function detectUiKindsFromText(
subject: string,
body: string,
filenames: string[] = [],
): InstructionUiKind[] {
const kinds = new Set<InstructionUiKind>();
for (const h of matchKeywordRules(subject, "subject")) kinds.add(h.uiKind);
for (const h of matchKeywordRules(body, "body")) kinds.add(h.uiKind);
for (const f of filenames) {
const whereHits = matchKeywordRules(f, "attachment");
for (const h of whereHits) kinds.add(h.uiKind);
if (isDoAttachmentFilename(f)) kinds.add("do_upload");
if (ATTACHMENT_HINT_RE.test(f) && /换标|贴标|覆盖贴|操作指令/.test(f)) {
kinds.add("work_order");
}
}
return UI_KIND_ORDER.filter((k) => kinds.has(k));
}`;
const newDetect = `export function detectUiKindsFromText(
subject: string,
body: string,
filenames: string[] = [],
): InstructionUiKind[] {
const kinds = new Set<InstructionUiKind>();
for (const h of matchKeywordRules(subject, "subject")) kinds.add(h.uiKind);
for (const h of matchKeywordRules(body, "body")) kinds.add(h.uiKind);
for (const f of filenames) {
for (const h of matchKeywordRules(f, "attachment")) kinds.add(h.uiKind);
if (isDoAttachmentFilename(f)) kinds.add("do_upload");
if (ATTACHMENT_HINT_RE.test(f) && /换标|贴标|覆盖贴|操作指令/.test(f)) {
kinds.add("work_order");
}
}
return UI_KIND_ORDER.filter((k) => kinds.has(k));
}
/** 单段:正文有动作词时,忽<EFBC8C>?Re: 链路上的历史「新增预报」主<E3808D>?*/
export function detectKindsForSegment(
segSubject: string,
segBody: string,
): InstructionUiKind[] {
if (isNonInstructionSegment(segBody)) return [];
const bodyHits = matchKeywordRules(segBody, "body");
const useSubject =
!BODY_ACTION_RE.test(segBody) ||
/新增预报|请查收新增预<E5A29E>?.test(segBody);
const subjectHits = useSubject
? matchKeywordRules(segSubject, "subject")
: [];
const kinds = new Set<InstructionUiKind>([
...bodyHits.map((h) => h.uiKind),
...subjectHits.map((h) => h.uiKind),
]);
return UI_KIND_ORDER.filter((k) => kinds.has(k));
}`;
if (!t.includes(oldDetect)) {
console.error("detectUiKindsFromText block not found");
process.exit(1);
}
t = t.replace(oldDetect, newDetect);
t = t.replace(
`segments.forEach((seg, i) => {
const segSubject = extractSegmentSubject(seg, "");
const kinds = detectUiKindsFromText(segSubject || subject, seg, []);`,
`segments.forEach((seg, i) => {
if (isNonInstructionSegment(seg)) return;
const segSubject = extractSegmentSubject(seg, "");
const kinds = detectKindsForSegment(segSubject, seg);`,
);
fs.writeFileSync(p, t, "utf8");
console.log(
JSON.stringify({
ok:
t.includes("detectKindsForSegment") &&
t.includes("isNonInstructionSegment") &&
t.includes("BODY_ACTION_RE"),
}),
);