From 5af6e2ac5f943d510e79d776142352fafef648df Mon Sep 17 00:00:00 2001 From: DIYgod Date: Tue, 25 Jul 2023 23:18:59 +0100 Subject: [PATCH] feat: finer word count filtering --- data/filter.json | 2 +- src/app/api/score/route.ts | 3 +- src/components/home/HomeFeed.tsx | 15 --- src/lib/character-count.ts | 15 +++ src/lib/utils.ts | 13 --- src/models/home.model.ts | 187 +++++++++++++++++++------------ 6 files changed, 132 insertions(+), 103 deletions(-) create mode 100644 src/lib/character-count.ts diff --git a/data/filter.json b/data/filter.json index 2f49437d..3e93621a 100644 --- a/data/filter.json +++ b/data/filter.json @@ -1,7 +1,7 @@ { "latest": [ 151, 53994, 54570, 54481, 54537, 54302, 53944, 55768, 55855, 56592, 54986, - 56875, 56873, 56666, 55996 + 56875, 56873, 56666, 55996, 57407 ], "comment": [54986] } diff --git a/src/app/api/score/route.ts b/src/app/api/score/route.ts index 6270d996..97d59d75 100644 --- a/src/app/api/score/route.ts +++ b/src/app/api/score/route.ts @@ -1,5 +1,6 @@ import AsyncLock from "async-lock" +import countCharacters from "~/lib/character-count" import { toGateway } from "~/lib/ipfs-parser" import prisma from "~/lib/prisma.server" import { cacheGet } from "~/lib/redis.server" @@ -15,7 +16,7 @@ const getOriginalScore = async (cid: string) => { content = content.slice(0, 5000) } - if (content?.length > 200) { + if (countCharacters(content) > 300) { console.time(`fetching score ${cid}`) const prompt = `According to rule 1 not too short content, rule 2 good originality and innovation, and rule 3 good fun or logic, give this article a score in the range of 0-100 and explain the reason: diff --git a/src/components/home/HomeFeed.tsx b/src/components/home/HomeFeed.tsx index 25e8d15b..f242122e 100644 --- a/src/components/home/HomeFeed.tsx +++ b/src/components/home/HomeFeed.tsx @@ -16,7 +16,6 @@ import { useIsMobileLayout } from "~/hooks/useMobileLayout" import { useTranslation } from "~/lib/i18n/client" import { getStorage, setStorage } from "~/lib/storage" import { ExpandedNote } from "~/lib/types" -import { getStringLength } from "~/lib/utils" import type { FeedType, SearchType } from "~/models/home.model" import { useGetFeed } from "~/queries/home" @@ -139,25 +138,11 @@ export const HomeFeed = ({ type }: { type?: FeedType }) => { }, [] as ExpandedNote[]) .filter((post) => { if ( - new Date(post.metadata?.content?.date_published || "") > - new Date() - ) { - return false - } else if ( aiFiltering && post.metadata?.content?.score?.number !== undefined && post.metadata.content.score.number <= 60 ) { return false - } else if ( - post.toNote?.metadata?.content?.tags?.includes("comment") - ) { - return false - } else if ( - !post.metadata?.content?.summary || - getStringLength(post.metadata.content.summary) < 6 - ) { - return false } else { return true } diff --git a/src/lib/character-count.ts b/src/lib/character-count.ts new file mode 100644 index 00000000..b8eb67ad --- /dev/null +++ b/src/lib/character-count.ts @@ -0,0 +1,15 @@ +import removeMarkdown from "remove-markdown" + +export default function countCharacters(str: string) { + str = removeMarkdown(str) + let count = 0 + for (let i = 0; i < str.length; i++) { + var chr = str.charAt(i) + if (/[\u4e00-\u9fa5]/.test(chr)) { + count += 2 + } else { + count += 1 + } + } + return count +} diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 57609c46..8e92278a 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -72,19 +72,6 @@ export const scrollTo = (hash: string, notUserContent?: boolean) => { }) } -export function getStringLength(str: string) { - let len = 0 - for (let i = 0; i < str.length; i++) { - const code = str.charCodeAt(i) - if (code >= 0 && code <= 128) { - len += 1 - } else { - len += 2 - } - } - return len -} - export const isMobileDevice = () => { if (isServerSide()) { return false diff --git a/src/models/home.model.ts b/src/models/home.model.ts index 8c51da04..c89fa3ae 100644 --- a/src/models/home.model.ts +++ b/src/models/home.model.ts @@ -1,8 +1,10 @@ +import type { NoteEntity } from "crossbell" import dayjs from "dayjs" import { indexer } from "@crossbell/indexer" import { gql } from "@urql/core" +import countCharacters from "~/lib/character-count" import { expandCrossbellNote } from "~/lib/expand-unit" import { ExpandedNote } from "~/lib/types" import { client } from "~/queries/graphql" @@ -76,6 +78,15 @@ export async function getFeed({ } ` + let resultAll: { + list: ExpandedNote[] + cursor?: string | null + count?: number + } = { + list: [], + count: 0, + } + switch (type) { case "latest": { const result = await client @@ -119,18 +130,16 @@ export async function getFeed({ .toPromise() const list = await Promise.all( - result?.data?.notes.map(async (page: any) => { - const expand = await expandCrossbellNote({ + result?.data?.notes.map((page: NoteEntity) => + expandCrossbellNote({ note: page, useScore: true, useHTML, - }) - delete expand.metadata?.content.content - return expand - }), + }), + ), ) - return { + resultAll = { list, cursor: list?.length ? `${list[list.length - 1]?.characterId}_${list[list.length - 1] @@ -138,6 +147,7 @@ export async function getFeed({ : undefined, count: list?.length || 0, } + break } case "comments": { const result = await client @@ -198,10 +208,10 @@ export async function getFeed({ const list = await Promise.all( result?.data?.notes - .filter((page: any) => { + .filter((page: NoteEntity) => { return !page.toNote?.metadata?.content?.tags?.includes("comment") }) - .map(async (page: any) => { + .map(async (page: NoteEntity) => { const expand = await expandCrossbellNote({ note: page, useHTML, @@ -212,12 +222,11 @@ export async function getFeed({ useHTML, }) } - delete expand.metadata?.content.content return expand }), ) - return { + resultAll = { list, cursor: list?.length ? `${list[list.length - 1]?.characterId}_${list[list.length - 1] @@ -225,6 +234,7 @@ export async function getFeed({ : undefined, count: list?.length || 0, } + break } case "following": { if (!characterId) { @@ -246,21 +256,20 @@ export async function getFeed({ ) const list = await Promise.all( - result.list.map(async (page: any) => { - const expand = await expandCrossbellNote({ + result.list.map((page: NoteEntity) => + expandCrossbellNote({ note: page, useHTML, - }) - delete expand.metadata?.content.content - return expand - }), + }), + ), ) - return { + resultAll = { list, cursor: result.cursor, count: result.count, } + break } } case "topic": { @@ -353,17 +362,15 @@ export async function getFeed({ .toPromise() const list = await Promise.all( - result?.data?.notes.map(async (page: any) => { - const expand = await expandCrossbellNote({ + result?.data?.notes.map((page: NoteEntity) => + expandCrossbellNote({ note: page, useHTML, - }) - delete expand.metadata?.content.content - return expand - }), + }), + ), ) - return { + resultAll = { list: list, cursor: list?.length ? `${list[list.length - 1]?.characterId}_${list[list.length - 1] @@ -371,6 +378,7 @@ export async function getFeed({ : undefined, count: list?.length || 0, } + break } else { const excludeString = [ ...(info.excludeKeywords?.map( @@ -429,17 +437,15 @@ export async function getFeed({ .toPromise() const list = await Promise.all( - result?.data?.notes.map(async (page: any) => { - const expand = await expandCrossbellNote({ + result?.data?.notes.map((page: NoteEntity) => + expandCrossbellNote({ note: page, useHTML, - }) - delete expand.metadata?.content.content - return expand - }), + }), + ), ) - return { + resultAll = { list: list, cursor: list?.length ? `${list[list.length - 1]?.characterId}_${list[list.length - 1] @@ -447,6 +453,7 @@ export async function getFeed({ : undefined, count: list?.length || 0, } + break } } case "hottest": { @@ -516,20 +523,28 @@ export async function getFeed({ .toPromise() let list: ExpandedNote[] = await Promise.all( - result?.data?.notes.map(async (page: any) => { - if (daysInterval) { - const secondAgo = dayjs().diff(dayjs(page.createdAt), "second") - page.stat.hotScore = - page.stat.viewDetailCount / Math.max(Math.log10(secondAgo), 1) - } + result?.data?.notes.map( + async ( + page: NoteEntity & { + stat: { + viewDetailCount: number + hotScore?: number + } + }, + ) => { + if (daysInterval) { + const secondAgo = dayjs().diff(dayjs(page.createdAt), "second") + page.stat.hotScore = + page.stat.viewDetailCount / Math.max(Math.log10(secondAgo), 1) + } - const expand = await expandCrossbellNote({ - note: page, - useHTML, - }) - delete expand.metadata?.content.content - return expand - }), + const expand = await expandCrossbellNote({ + note: page, + useHTML, + }) + return expand + }, + ), ) if (daysInterval) { @@ -544,11 +559,12 @@ export async function getFeed({ .slice(0, 24) } - return { + resultAll = { list: list, cursor: "", count: list?.length || 0, } + break } case "featured": { const result = await client @@ -625,18 +641,26 @@ export async function getFeed({ .toPromise() let list = await Promise.all( - result?.data?.notes.map(async (page: any) => { - const secondAgo = dayjs().diff(dayjs(page.createdAt), "second") - page.stat.hotScore = - page.stat.viewDetailCount / Math.max(Math.log10(secondAgo), 1) + result?.data?.notes.map( + async ( + page: NoteEntity & { + stat: { + viewDetailCount: number + hotScore?: number + } + }, + ) => { + const secondAgo = dayjs().diff(dayjs(page.createdAt), "second") + page.stat.hotScore = + page.stat.viewDetailCount / Math.max(Math.log10(secondAgo), 1) - const expand = await expandCrossbellNote({ - note: page, - useHTML, - }) - delete expand.metadata?.content.content - return expand - }), + const expand = await expandCrossbellNote({ + note: page, + useHTML, + }) + return expand + }, + ), ) const cursor = list?.length @@ -652,11 +676,12 @@ export async function getFeed({ } }) - return { + resultAll = { list, cursor, count: list?.length || 0, } + break } case "search": { const result = await indexer.search.notes(searchKeyword!, { @@ -669,32 +694,32 @@ export async function getFeed({ }) const list = await Promise.all( - result.list.map(async (page: any) => { - const expand = await expandCrossbellNote({ + result.list.map((page: NoteEntity) => + expandCrossbellNote({ note: page, useStat: false, useScore: false, keyword: searchKeyword, useHTML, - }) - delete expand.metadata?.content.content - return expand - }), + }), + ), ) - return { + resultAll = { list, cursor: result.cursor, count: result.count, } + break } case "tag": { if (!tag) { - return { + resultAll = { list: [], cursor: "", count: 0, } + break } let result = await indexer.note.getMany({ @@ -707,25 +732,41 @@ export async function getFeed({ } as any) const list = await Promise.all( - result.list.map(async (page: any) => { - const expand = await expandCrossbellNote({ + result.list.map((page: NoteEntity) => + expandCrossbellNote({ note: page, useStat: false, useScore: true, useHTML, - }) - delete expand.metadata?.content.content - return expand - }), + }), + ), ) - return { + resultAll = { list, cursor: result.cursor, count: result.count, } + break } } + + resultAll.list = resultAll.list + .filter( + (post) => + countCharacters(post?.metadata?.content?.content || "") > + (post?.metadata?.content?.tags?.[0] === "comment" ? 6 : 300) && + !( + new Date(post.metadata?.content?.date_published || "") > new Date() + ) && + !post.toNote?.metadata?.content?.tags?.includes("comment"), + ) + .map((post) => { + delete post.metadata?.content.content + return post + }) + + return resultAll } export const getShowcase = async () => {