text-search-engine

Text Search Engine 接入指南

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "text-search-engine" with this command: npx skills add cjinhuo/text-search-engine/cjinhuo-text-search-engine-text-search-engine

Text Search Engine 接入指南

此 skill 帮助你将 text-search-engine SDK 集成到项目中。该 SDK 是一个基于动态规划的文本搜索引擎,支持中英文混合模糊搜索,返回权重最高的匹配结果。

安装

npm i text-search-engine

同时支持 Node.js 和 Web 环境。

核心 API

  1. search(source, query, options?)
  • 主搜索函数

返回匹配位置的索引范围数组 [start, end][] ,无匹配时返回 undefined 。

基本用法

import { search } from 'text-search-engine'

// 纯英文搜索 search('nonode', 'no') // [[0, 1]] - 匹配 'no' search('nonode', 'nod') // [[2, 4]] - 匹配 'nod' search('nonode', 'noe') // [[0, 1], [5, 5]] - 匹配 'no' + 'e'

// 纯中文搜索(支持拼音) search('地表最强前端监控平台', 'jk') // [[6, 7]] - 匹配 '监控' search('地表最强前端监控平台', 'qianduapt') // [[4, 5], [8, 9]] - 匹配 '前端' + '平台'

// 中英文混合搜索 search('Node.js 最强监控平台 V9', 'nodejk') // [[0, 3], [10, 11]] - 匹配 'Node' + '监控'

空格分隔搜索

添加空格使每个词独立匹配,从头开始:

search('Node.js 最强监控平台 V9', 'jknode') // undefined search('Node.js 最强监控平台 V9', 'jk node') // [[10, 11], [0, 3]] - 可以匹配!

  1. 配置选项

选项 默认值 说明

mergeSpaces

true

将匹配结果中的空格合并为连续范围

strictnessCoefficient

undefined

严格系数(0-1),匹配字符数 ≤ ceil(query.length * coefficient) 时返回 undefined

isCharConsecutive

false

要求匹配的字符必须连续

strictCase

false

区分大小写匹配

示例

// mergeSpaces - 合并空格 search('chrome 应用商店', 'meyinyon', { mergeSpaces: false }) // [[4, 5], [7, 8]] search('chrome 应用商店', 'meyinyon', { mergeSpaces: true }) // [[4, 8]]

// strictnessCoefficient - 严格系数 search('Node.js 最强监控平台 V8', 'nozjk', { strictnessCoefficient: 0.5 }) // [[0, 1], [8, 8], [10, 11]] search('Node.js 最强监控平台 V8', 'nozjk', { strictnessCoefficient: 0.4 }) // undefined

// isCharConsecutive - 连续字符 search('Chinese@中国 People-人', 'chie') // [[0, 2], [4, 4]] search('Chinese@中国 People-人', 'chie', { isCharConsecutive: true }) // undefined

// strictCase - 大小写敏感 search('Hello World', 'hello') // [[0, 4]] search('Hello World', 'hello', { strictCase: true }) // undefined

  1. highlightMatches(source, query)
  • 快速高亮

返回 ANSI 转义码,用于控制台输出:

import { highlightMatches } from 'text-search-engine'

console.log(highlightMatches('Node.js 最强监控平台 V9', 'nodev9')) // 输出带高亮的文本

React 组件

HighlightWithTarget

自动匹配并高亮:

import { HighlightWithTarget } from 'text-search-engine/react'

function SearchResult() { return <HighlightWithTarget source='Node.js 最强监控平台 V9' target='nodejk' /> }

HighlightWithRanges

手动提供匹配范围:

import { HighlightWithRanges } from 'text-search-engine/react' import { search } from 'text-search-engine'

function SearchResult() { const ranges = search('Node.js 最强监控平台 V9', 'nodejk') return <HighlightWithRanges source='Node.js 最强监控平台 V9' hitRanges={ranges} /> }

常见集成模式

  1. 带高亮的搜索列表

import { search } from 'text-search-engine' import { HighlightWithRanges } from 'text-search-engine/react'

function SearchList({ items, query }) { const results = items .map(item => ({ item, ranges: search(item.name, query) })) .filter(({ ranges }) => ranges !== undefined)

return ( <ul> {results.map(({ item, ranges }) => ( <li key={item.id}> <HighlightWithRanges source={item.name} hitRanges={ranges} /> </li> ))} </ul> ) }

  1. 复用 BoundaryData 进行多次搜索

当需要对同一数据源进行多次搜索时,可以预先提取 boundaryData 以提升性能:

import { extractBoundaryMapping, searchSentenceByBoundaryMapping } from 'text-search-engine'

const source = 'Node.js 最强监控平台 V9'

// 预先提取 boundaryData(只需执行一次) const boundaryData = extractBoundaryMapping(source)

// 多次搜索复用同一个 boundaryData function searchMultiple(queries) { return queries.map(query => { const { hitRanges, wordHitRangesMapping } = searchSentenceByBoundaryMapping(boundaryData, query) return { query, hitRanges, wordHitRangesMapping } }).filter(result => result.hitRanges !== undefined) }

// 示例:对同一数据源进行多次搜索 const results = searchMultiple(['node', 'jk', 'v9', 'qianduan'])

这种方式特别适合以下场景:

  • 搜索列表中对每个 item 需要匹配多个关键词

  • 实时搜索建议,用户输入变化时复用已处理的数据

  • 批量搜索任务

  1. Node.js 后端搜索

import { search } from 'text-search-engine'

function searchDocuments(documents, query, options = {}) { const { limit = 20, strictnessCoefficient = 0.6 } = options

const results = []

for (const doc of documents) { const ranges = search(doc.title, query, { strictnessCoefficient }) if (ranges) { results.push({ doc, ranges }) if (results.length >= limit) break } }

return results }

性能

时间复杂度 空间复杂度

最优 O(M),M = 源字符串长度 O(M)

最差 O(M × N),N = 查询字符串长度 O(M × N)

相关资源

  • 在线演示

  • 算法可视化

  • CodeSandbox React 示例

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

General

image-gen

Generate AI images from text prompts. Triggers on: "生成图片", "画一张", "AI图", "generate image", "配图", "create picture", "draw", "visualize", "generate an image".

Archived SourceRecently Updated
General

explainer

Create explainer videos with narration and AI-generated visuals. Triggers on: "解说视频", "explainer video", "explain this as a video", "tutorial video", "introduce X (video)", "解释一下XX(视频形式)".

Archived SourceRecently Updated
General

asr

Transcribe audio files to text using local speech recognition. Triggers on: "转录", "transcribe", "语音转文字", "ASR", "识别音频", "把这段音频转成文字".

Archived SourceRecently Updated