From 2495911a81c6cef85cc142c14c11cfb257770bca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B3=E5=85=83=E7=9A=93?= Date: Mon, 19 May 2025 14:38:59 +0800 Subject: [PATCH] Make a demo? --- .../checks/checkKidUnfriendlyContent.ts | 17 +++++++++++++--- components/newsAnalyzer.ts | 20 +++++++++++++++++++ pages/demo.vue | 14 +++++++++++++ .../api/contentcheck/kidunfriendlycontent.ts | 2 +- 4 files changed, 49 insertions(+), 4 deletions(-) create mode 100644 components/newsAnalyzer.ts create mode 100644 pages/demo.vue diff --git a/components/checks/checkKidUnfriendlyContent.ts b/components/checks/checkKidUnfriendlyContent.ts index df07910..13c2fbf 100644 --- a/components/checks/checkKidUnfriendlyContent.ts +++ b/components/checks/checkKidUnfriendlyContent.ts @@ -1,6 +1,17 @@ -async function checkUnsafeContent() { - const req = await fetch("/api/contentcheck/kidunfriendlycontent"); - const res = await req.json(); +import NewsAnalyzer from "~/components/newsAnalyzer"; +const newsAnalyzer = new NewsAnalyzer(); +async function checkUnsafeContent(title: string) { + try { + const req = await fetch("/api/contentcheck/kidunfriendlycontent"); + const res = await req.json(); + const patterns = res.words.map((word) => new RegExp(word, "i")); + console.log(patterns); + newsAnalyzer.setSensitivePatterns(patterns); + const kidfriendly = newsAnalyzer.isKidFriendly(title); + return kidfriendly; + } catch (e) { + console.log(e); + } } export default checkUnsafeContent; diff --git a/components/newsAnalyzer.ts b/components/newsAnalyzer.ts new file mode 100644 index 0000000..51431d5 --- /dev/null +++ b/components/newsAnalyzer.ts @@ -0,0 +1,20 @@ +// News Analyzer Class +class NewsAnalyzer { + private sensitivePatterns: RegExp[]; + constructor() { + this.sensitivePatterns = []; + } + + isKidFriendly(title) { + for (let pattern of this.sensitivePatterns) { + if (pattern.test(title)) return false; + } + return true; + } + + public setSensitivePatterns(patterns: RegExp[]): void { + this.sensitivePatterns = patterns; + } +} + +export default NewsAnalyzer; diff --git a/pages/demo.vue b/pages/demo.vue new file mode 100644 index 0000000..f87b889 --- /dev/null +++ b/pages/demo.vue @@ -0,0 +1,14 @@ + + diff --git a/server/api/contentcheck/kidunfriendlycontent.ts b/server/api/contentcheck/kidunfriendlycontent.ts index c3878d8..58d6bad 100644 --- a/server/api/contentcheck/kidunfriendlycontent.ts +++ b/server/api/contentcheck/kidunfriendlycontent.ts @@ -1,6 +1,6 @@ import sql from "~/server/components/postgres"; export default defineEventHandler(async (event) => { return { - words: ["violence"], + words: ["violence", "kill", "太小"], }; });