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-skip-shell-segments.ts

97 lines
3.0 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 lexPath = "src/services/parse/instruction-lexicon.ts";
let lex = fs.readFileSync(lexPath, "utf8");
if (!lex.includes("isForwardShellSegment")) {
lex = lex.replace(
`export function isNonInstructionSegment(text: string): boolean {
const compact = text.replace(/\\s+/g, " ").trim();
if (compact.length < 6) return true;`,
`/** QQ/中间人纯转发壳,无客<E697A0>?Dear 正文 */
export function isForwardShellSegment(text: string): boolean {
const compact = text.replace(/\\s+/g, " ").trim();
if (/发自我的iPhone/.test(compact)) return true;
if (
/date@usasinogroup\\.com/i.test(compact) &&
!/Dear[,,]/.test(compact) &&
!BODY_ACTION_RE.test(compact)
) {
return true;
}
return false;
}
export function isNonInstructionSegment(text: string): boolean {
const compact = text.replace(/\\s+/g, " ").trim();
if (compact.length < 6) return true;
if (isForwardShellSegment(text)) return true;`,
);
fs.writeFileSync(lexPath, lex, "utf8");
}
const splitPath = "src/services/parse/split-instructions.ts";
let sp = fs.readFileSync(splitPath, "utf8");
if (!sp.includes("bodyForKindDetect")) {
sp = sp.replace(
`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));
}`,
`/** 去掉段内「主<E3808C>? Re:Re:新增预报」历史主题污<E9A298>?*/
function bodyForKindDetect(segBody: string): string {
if (!BODY_ACTION_RE.test(segBody)) return segBody;
return segBody
.split(/\\n/)
.filter(
(line) =>
!/(?:主题|主旨|Subject)\\s*[<5B>?].*(?:新增预报|Fw:|转发)/i.test(line),
)
.join("\\n");
}
export function detectKindsForSegment(
segSubject: string,
segBody: string,
): InstructionUiKind[] {
if (isNonInstructionSegment(segBody)) return [];
const bodyText = bodyForKindDetect(segBody);
const bodyHits = matchKeywordRules(bodyText, "body");
const useSubject =
!BODY_ACTION_RE.test(segBody) ||
/请查收新增预<E5A29E>?.test(bodyText);
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));
}`,
);
fs.writeFileSync(splitPath, sp, "utf8");
}
console.log(
JSON.stringify({
lex: fs.readFileSync(lexPath, "utf8").includes("isForwardShellSegment"),
split: fs.readFileSync(splitPath, "utf8").includes("bodyForKindDetect"),
}),
);