Loading...
Loading...
Guides users to integrate text-search-engine SDK for Chinese/English fuzzy search with Pinyin support. Invoke when user wants to add fuzzy search, pinyin search, or text matching.
npx skill4agent add cjinhuo/text-search-engine text-search-enginenpm i text-search-engineNode.jsWebsearch(source, query, options?)[start, end][]undefinedimport { 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]] - 可以匹配!| 选项 | 默认值 | 说明 |
|---|---|---|
| | 将匹配结果中的空格合并为连续范围 |
| | 严格系数(0-1),匹配字符数 ≤ |
| | 要求匹配的字符必须连续 |
| | 区分大小写匹配 |
// 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 }) // undefinedhighlightMatches(source, query)import { highlightMatches } from 'text-search-engine'
console.log(highlightMatches('Node.js 最强监控平台 V9', 'nodev9'))
// 输出带高亮的文本import { HighlightWithTarget } from 'text-search-engine/react'
function SearchResult() {
return <HighlightWithTarget source='Node.js 最强监控平台 V9' target='nodejk' />
}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} />
}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>
)
}boundaryDataimport {
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'])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) |