基于BM25的快速全文搜索
基于BM25的wink-bm25-text-search - 用于文档检索的P Robabilistic R高架算法是一个全文搜索包,用于在Node.js或浏览器环境中开发应用程序。它从输入JSON文档中构建了内存搜索索引,该文档已针对尺寸和速度进行了优化。
探索Wink BM25文本搜索示例以深入研究:
它的代码可在Showcase-BM25-Text-Search Repo以及详细的博客文章中获得。
通过以下方式为搜索添加语义风味很容易:
为字段分配不同的数值权重。每当发生与该字段的匹配时,负字段重量都会降低文档的分数。
使用Wink-NLP的丰富文本处理功能,例如否定检测,茎,诱饵,停止单词检测和命名实体检测以执行智能搜索。
为字段和查询文本分别定义不同的文本准备任务。
使用NPM安装:
npm install wink-bm25-text-search --save // Load wink-bm25-text-search
var bm25 = require ( 'wink-bm25-text-search' ) ;
// Create search engine's instance
var engine = bm25 ( ) ;
// Load sample data (load any other JSON data instead of sample)
var docs = require ( 'wink-bm25-text-search/sample-data/demo-data-for-wink-bm25.json' ) ;
// Load wink nlp and its model
const winkNLP = require ( 'wink-nlp' ) ;
// Use web model
const model = require ( 'wink-eng-lite-web-model' ) ;
const nlp = winkNLP ( model ) ;
const its = nlp . its ;
const prepTask = function ( text ) {
const tokens = [ ] ;
nlp . readDoc ( text )
. tokens ( )
// Use only words ignoring punctuations etc and from them remove stop words
. filter ( ( t ) => ( t . out ( its . type ) === 'word' && ! t . out ( its . stopWordFlag ) ) )
// Handle negation and extract stem of the word
. each ( ( t ) => tokens . push ( ( t . out ( its . negationFlag ) ) ? '!' + t . out ( its . stem ) : t . out ( its . stem ) ) ) ;
return tokens ;
} ;
// Contains search query.
var query ;
// Step I: Define config
// Only field weights are required in this example.
engine . defineConfig ( { fldWeights : { title : 1 , body : 2 } } ) ;
// Step II: Define PrepTasks pipe.
// Set up 'default' preparatory tasks i.e. for everything else
engine . definePrepTasks ( [ prepTask ] ) ;
// Step III: Add Docs
// Add documents now...
docs . forEach ( function ( doc , i ) {
// Note, 'i' becomes the unique id for 'doc'
engine . addDoc ( doc , i ) ;
} ) ;
// Step IV: Consolidate
// Consolidate before searching
engine . consolidate ( ) ;
// All set, start searching!
query = 'not studied law' ;
// `results` is an array of [ doc-id, score ], sorted by score
var results = engine . search ( query ) ;
// Print number of results.
console . log ( '%d entries found.' , results . length ) ;
// -> 1 entries found.
// results[ 0 ][ 0 ] i.e. the top result is:
console . log ( docs [ results [ 0 ] [ 0 ] ] . body ) ;
// -> George Walker Bush (born July 6, 1946) is an...
// -> ... He never studied Law...
// Whereas if you search for `law` then multiple entries will be
// found except the above entry! 笔记:
winknlp需要Node.js 16或18版。
Wink-NLP-Utils仍可用于支持旧版代码。有关wink-nlp-util示例,请参阅Wink-BM25-Text-Search版本3.0.1。
defineConfig( config )从config对象定义配置。该对象定义以下3个属性:
fldWeights (强制性)是一个对象,其中每个密钥是文档的字段名称,值是数值权重,即该字段的重要性。
bm25Params (可选)也是一个最多定义3个键的对象。 k1 , b和k 。它们的默认值分别0.75 1.2和1 。注意: k1控制TF饱和度; b控制归一度的程度, k管理IDF。
ovFldNames (可选)是一个包含字段名称的数组,必须保留其原始值。这对于使用search() API调用过滤器降低搜索空间很有用。
definePrepTasks( tasks [, field ] )定义文本准备tasks ,以将原始的传入文本转换为addDoc()和search()操作期间所需的一系列令牌。它返回tasks计数。
tasks应该是一系列功能。此数组中的第一个功能必须接受字符串作为输入;最后一个功能必须返回一系列令牌作为JavaScript字符串。每个函数必须接受一个输入参数并返回一个值。
第二个参数 - field是可选的。它定义了将要定义tasks的文档field ;在没有这个论点的情况下, tasks成为其他所有内容的默认值。该配置必须在此调用之前通过defineConfig()定义。
addDoc( doc, uniqueId )将doc与BM25模型的Doc添加到uniqueId 。在添加文档之前,必须调用defineConfig()和definePrepTasks() 。它接受结构化的JSON文档作为创建模型的输入。以下是此软件包中包含的示例数据的示例文档结构:
{
title: 'Barack Obama',
body: 'Barack Hussein Obama II born August 4, 1961 is an American politician...'
tags: 'democratic nobel peace prize columbia michelle...'
}
样本数据是使用Wikipedia文章(例如Barack Obama上的一篇文章)创建的。
它具有一个别名learn( doc, uniqueId ) ,可维持各种眨眼软件包(例如wink-nive-nive-bayes-text-clalerifier)的API水平均匀性。
consolidate( fp )合并所有添加文档的BM25模型。 fp定义了存储术语频率值的精度。默认值为4,在大多数情况下都足够好。它是search()的先决条件,并且在合并后无法添加文档。
search( text [, limit, filter, params ] )搜索text并返回结果的limit数量。 filter应是必须基于params返回true或false的函数。将其视为JavaScript数组的滤波器功能。它收到两个参数。 (a)一个包含字段名称/值对通过ovFldNames定义的defineConfig()和(b) params 。
最后三个参数limit , filter和params是可选的。 limit的默认值为10 。
结果是在relevanceScore上排序的一系列[ uniqueId, relevanceScore ] 。
与addDoc()一样,它也具有一个别名predict( doc, uniqueId )可在各种眨眼软件包中保持API水平均匀性,例如眨眼的bayes-bayes-text-clalerifier。
exportJSON()可以将BM25模型导出为可以保存在文件中的JSON文本。在合并之前导出JSON是一个好主意,并在需要添加更多文档时使用相同的主意;而在合并后导出的JSON仅适用于搜索操作。
importJSON( json )现有的JSON BM25模型可以导入搜索。在尝试搜索之前,必须调用definePrepTasks() 。
reset()它通过重新定位所有变量来完全重置BM25模型,除了准备任务。
它提供以下访问者方法:
getDocs()返回每个文档的术语频率和长度。getTokens()返回token: index映射。getIDF()为每个令牌返回IDF。通过其数值索引引用令牌,该索引通过getTokens()访问。getConfig()返回由defineConfig()设置的BM25F配置。getTotalCorpusLength()返回所有添加的文档中的令牌总数。getTotalDocs()返回添加了总文档。注意:这些配件暴露了一些内部数据结构,并且必须避免修改它。它是专门用于仅阅读目的的。任何有意或无意的修改都可能导致包装的严重故障。
如果您发现一个错误尚未报告,请提出新问题或考虑修复它并发送拉动请求。
Winkjs是一个用于自然语言处理,统计分析和机器学习的开源软件包家族。该代码已彻底记录在人类的理解中,并具有约100%的测试覆盖范围,以建立生产等级解决方案。
Wink-BM25-Text-Search是版权2017-22 Graype Systems Private Limited。
它是根据MIT许可证的条款获得许可的。