Switch to a different algroithm.

This commit is contained in:
yuanhau 2025-05-21 10:20:33 +08:00
parent 25927ad13c
commit f8879b307c
9 changed files with 72 additions and 16 deletions

View file

@ -1,14 +1,16 @@
import NewsAnalyzer from "~/components/newsAnalyzer";
const newsAnalyzer = new NewsAnalyzer();
// Trying out the ahocorasick algorithm
// Recommended by: https://www.threads.com/@hsinspeng/post/DJ3yVGQxBg7
import AhoCorasick from "ahocorasick";
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;
console.log(res.words);
const ac = new AhoCorasick(res.words);
const kidfriendly = ac.search(title);
console.log(kidfriendly);
return kidfriendly;
} catch (e) {
console.log(e);
}