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", "太小"], }; });