36 lines
946 B
TypeScript
36 lines
946 B
TypeScript
/**
|
||
* 滚动情报文本处理:转为简体中文,过滤非中文内容
|
||
*/
|
||
import { Converter } from 'opencc-js/t2cn'
|
||
|
||
const t2s = Converter({ from: 'twp', to: 'cn' })
|
||
|
||
/** 简体中文字符范围 */
|
||
const ZH_REGEX = /[\u4e00-\u9fff]/g
|
||
|
||
/** 文本中中文占比是否达标(至少30%) */
|
||
export function isMostlyChinese(text: string): boolean {
|
||
if (!text?.trim()) return false
|
||
const zh = text.match(ZH_REGEX)
|
||
const zhCount = zh ? zh.length : 0
|
||
return zhCount / text.length >= 0.3
|
||
}
|
||
|
||
/** 繁体转简体 */
|
||
export function toSimplifiedChinese(text: string): string {
|
||
if (!text?.trim()) return text
|
||
try {
|
||
return t2s(text)
|
||
} catch {
|
||
return text
|
||
}
|
||
}
|
||
|
||
/** 处理滚动情报项:转为简体,非中文为主则过滤 */
|
||
export function processTickerText(text: string): string | null {
|
||
const t = (text || '').trim()
|
||
if (!t) return null
|
||
if (!isMostlyChinese(t)) return null
|
||
return toSimplifiedChinese(t)
|
||
}
|