Made the check words system much more useable, without the user's device being spammed with garbage requests.

This commit is contained in:
yuanhau 2025-05-30 01:04:51 +08:00
parent 20006fff15
commit b716a0ed5c
2 changed files with 24 additions and 8 deletions

View file

@ -147,17 +147,20 @@ const findRel = async (title: string) => {
const req = await fetch("/api/sort"); const req = await fetch("/api/sort");
}; };
// Check words
const checkIfEmptyArray = [];
const useArgFindRel = (title, newsOrg) => { const useArgFindRel = (title, newsOrg) => {
const targetVector = tf(title); const targetVector = tf(title);
const similarities = []; const similarities = [];
for (const item of contentArray.value) { for (const item of contentArray.value) {
if (item.title !== title && item.contentType === "GENERAL" && item.publisher === newsOrg) { if (
console.log(item.title); item.title !== title &&
item.contentType === "GENERAL" &&
item.publisher === newsOrg
) {
const itemVector = tf(item.title); const itemVector = tf(item.title);
console.log(itemVector);
const similarity = jaccardSimilarity(targetVector, itemVector); const similarity = jaccardSimilarity(targetVector, itemVector);
console.log(similarity);
if (similarity > 0.1) { if (similarity > 0.1) {
similarities.push({ similarities.push({
title: item.title, title: item.title,
@ -165,12 +168,22 @@ const useArgFindRel = (title, newsOrg) => {
item: item, item: item,
}); });
} }
console.log(similarities);
} }
} }
const idx = checkIfEmptyArray.findIndex((x) => x.title === title);
if (idx !== -1) checkIfEmptyArray.splice(idx, 1);
checkIfEmptyArray.push({
title: title,
contains: similarities.length === 0,
});
return similarities.sort((a, b) => b.similarity - a.similarity).slice(0, 3); return similarities.sort((a, b) => b.similarity - a.similarity).slice(0, 3);
}; };
const checkIfEmpty = (item) => {
const found = checkIfEmptyArray.find((key) => key.title === item);
return found ? found.contains : false;
};
const openNews = (url: string, titleName: string) => { const openNews = (url: string, titleName: string) => {
emit("openArticles", url, titleName); emit("openArticles", url, titleName);
}; };
@ -273,6 +286,7 @@ const openPublisher = (text: string) => {
<div> <div>
<h3 class="text-lg">類似文章</h3> <h3 class="text-lg">類似文章</h3>
<div <div
class="space-y-2" class="space-y-2"
> >
<div <div
@ -280,7 +294,6 @@ const openPublisher = (text: string) => {
:key="similar.item.id" :key="similar.item.id"
class="p-2 bg-gray-100 rounded text-sm cursor-pointer hover:bg-gray-200" class="p-2 bg-gray-100 rounded text-sm cursor-pointer hover:bg-gray-200"
@click="openNews(similar.item.url.hash, item.title)" @click="openNews(similar.item.url.hash, item.title)"
v-if="similar"
> >
<div class="font-medium">{{ similar.title }}</div> <div class="font-medium">{{ similar.title }}</div>
<div class="text-gray-500 text-xs"> <div class="text-gray-500 text-xs">
@ -288,8 +301,9 @@ const openPublisher = (text: string) => {
{{ similar.item.publisher }} {{ similar.item.publisher }}
</div> </div>
</div> </div>
<div v-else class="text-gray-500 text-sm">找不到類似文章</div>
</div> </div>
<div v-if="checkIfEmpty(item.title)" class="text-gray-500 text-sm">找不到類似文章</div>
</div> </div>
<!--<div v-for="item in findRel(item.title)"> <!--<div v-for="item in findRel(item.title)">
{{ item }} {{ item }}

View file

@ -451,11 +451,13 @@ const openArticles = async (slug: string, titleName?: string) => {
} else { } else {
titleNameFinal = t("app.newsview"); titleNameFinal = t("app.newsview");
} }
console.log(titleName);
findAndOpenWindow("newsView", titleName); findAndOpenWindow("newsView", titleName);
console.log("t2: " + titleName);
setTimeout(() => { setTimeout(() => {
openingAppViaAnApp.value = false; openingAppViaAnApp.value = false;
passedValues.value = null; passedValues.value = null;
console.log("wiping values");
}, 1000); }, 1000);
}; };