一款支援中英文混合模糊搜尋的文字搜尋引擎
中文自述文件
基於動態規劃的文字搜尋引擎,支援中英文混合模糊搜索,返回權重最高的匹配結果。
如果您有興趣,請查看此線上演示。
npm i text-search-engine
支援Node.js
和Web
環境。
import { search } from 'text-search-engine'
const source = 'nonode'
search ( source , 'no' ) //[[0, 1]]
// Matches 'no', continuous characters have higher weight
search ( source , 'nod' ) // [[2, 4]]
search ( source , 'noe' ) // [[0, 1], [5, 5]]
search ( source , 'oo' ) // [[1, 1],[3, 3]]
search('nonode', 'noe')
匹配結果:不點點頭e
import { search } from 'text-search-engine'
const source = '地表最强前端监控平台'
search ( source , 'jk' ) // [[6, 7]]
search ( source , 'qianduapt' ) // [[4, 5],[8, 9]]
search('地表最强前端监控平台', 'qianduapt')
匹配結果:地表最強前端監控平台
import { search } from 'text-search-engine'
search ( 'Node.js 最强监控平台 V9' , 'nodejk' ) //[[0, 3],[10, 11]]
const source_2 = 'a_nd你你的就是我的'
search ( source_2 , 'nd' ) //[[2, 3]]
// Matches '你你的'
search ( source_2 , 'nnd' ) //[[4, 6]]
// Matches 'a_'n'd你你的就'是我的'
search ( source_2 , 'nshwode' ) //[[2, 2],[8, 10]]
search('Node.js 最强监控平台 V9', 'nodejk')
匹配結果:節點.js 最強監控平台V9
添加空格使每個術語獨立。每個術語從頭開始匹配,匹配的術語將被刪除,因此下一個術語從頭開始匹配並忽略先前匹配的術語。
const source_1 = 'Node.js 最强监控平台 V9'
search ( source_1 , 'jknode' ) // undefined
search ( source_1 , 'jk node' ) // [[10, 11],[0, 3]]
search('Node.js 最强监控平台 V9', 'jk node')
匹配結果:節點.js 最強監控平台V9
const source_1 = 'zxhxo zhx'
search ( source_1 , 'zh' ) //[[6, 7]])
// Even though the weight of 'zh' is higher, but the next term 'o' is not matched, so hit the previous one
search ( source_1 , 'zho' ) //[[0, 0],[2, 2],[4, 4]])
此API用於快速驗證文字匹配突出顯示。它會傳回 ANSI 轉義碼,可以在 Web 和 Node.js 環境中使用 console.log 輸出這些程式碼以查看突出顯示的文字。
import { highlightMatches } from 'text-search-engine'
console . log ( highlightMatches ( 'Node.js 最强监控平台 V9' , 'nodev9' ) )
控制台會輸出:節點.js最強監控平台V9
預設值: false
const source = 'chrome 应用商店'
search ( source , 'meyinyon' ) //[[4, 5], [7, 8]])
// would merge blank spaces between each index of the matched term
search ( source , 'meyinyon' , { mergeSpaces : true } ) //[[4, 8]])
預設值: undefined
const source = 'Node.js 最强监控平台 V8'
search ( source , 'nozjk' ) //[[0, 1], [8, 8], [10, 11]]
// When the strictnessCoefficient is 0.5 and nozjk is five characters long, Math.ceil(5 * 0.5) equals 3. If the match is less than or equal to 3 characters, it will return normally.
search ( source , 'nozjk' , { strictnessCoefficient : 0.5 } ) //[[0, 1], [8, 8], [10, 11]]
search ( source , 'nozjk' , { strictnessCoefficient : 0.4 } ) //undefined
看看 CodeSandbox 線上演示
import { HighlightWithTarget } from 'text-search-engine/react'
function Test ( ) {
return < HighlightWithTarget source = 'Node.js 最强监控平台 V9' target = 'nodejk' / >
}
import { HighlightWithRanges } from 'text-search-engine/react'
import { search } from 'text-search-engine'
export default function DemoForHighlightWithTarget ( ) {
const ranges = search ( 'Node.js 最强监控平台 V9' , 'nodejk' )
return < HighlightWithRanges source = 'Node.js 最强监控平台 V9' hitRanges = { ranges } / >
}
時間複雜度 | 空間複雜度 | |
---|---|---|
最好的 | O(M(源)) | O(M(源)) |
最差 | O(M(源) * N(目標)) | O(M(源) * N(目標)) |
請參閱貢獻指南以了解更多資訊。
非常感謝我們所有出色的貢獻者❤️
歡迎加入並發送 PR!
歡迎提出問題,如果您有好的建議,可以透過wx或電子郵件與我聯繫(註:text-search-engine)