The code creates a database using Fuse.js for tokenized search and a custom search function levSearch
, and exports functions for database creation, querying, and cell lookup. If Fuse.js is unavailable, it uses the custom search function instead.
npm run import -- "search jupter notebooks for questions and answers"
var path = require('path')
try {
var Fuse = require('fuse.js')
} catch (e) {
if(!e.message.includes('Cannot find module')) {
throw e
}
}
var FUSE_CONFIG = {
caseSensitive: false,
findAllMatches: true,
distance: 50,
threshold: 0.5,
tokenize: true,
shouldSort: true,
keys: ['2.questions'],
id: '1'
}
var token, fuse
function createDatabase(cache) {
if(typeof Fuse !== 'undefined') {
token = new Fuse(cache, FUSE_CONFIG)
fuse = new Fuse(cache, Object.assign({}, FUSE_CONFIG, {
tokenize: false
}))
return
}
var {
importNotebook, getCells
} = require('../Core')
var searchFunc = getCells(path.resolve(__dirname, '../Utilities/levenshtein.ipynb'))
.filter(cell => cell.source.join('').includes('function levSearch'))
.map(cell => path.join(__dirname, '../Utilities', cell.id))
var levSearch = importNotebook("searchFunc").levSearch
// TODO: turn this in to `levSearch()` function
token = ({search: levSearch.bind(null, cache, FUSE_CONFIG)})
fuse = ({search: levSearch.bind(null, cache, FUSE_CONFIG)})
}
function queryDatabase(search) {
if(typeof fuse === 'undefined') {
throw new Error(`database not ready! ${search}`)
}
var tokenResults = token.search(search)
var fuseResults = fuse.search(search)
return fuseResults
.filter(s => tokenResults.includes(s))
.concat(tokenResults.filter(s => fuseResults.includes(s)))
}
function lookupCell(cacheId, cacheCells) {
const filename = cacheId.replace(/\.ipynb\[[0-9]+\].*/ig, '.ipynb')
const fresh = cacheCells(filename)
const cell = fresh.filter(cell => cell.id === path.basename(cacheId))[0]
return cell
}
module.exports.createDatabase = createDatabase
module.exports.queryDatabase = queryDatabase
module.exports.lookupCell = lookupCell
const path = require('path');
const Fuse = require('fuse.js');
path
module for path manipulation and tries to require the fuse.js
module. If it fails, it catches the error and continues execution if the error message does not indicate a module not found error.FUSE_CONFIG
is defined with settings for the Fuse.js library. The configuration includes options for search behavior, such as case sensitivity, finding all matches, distance, threshold, tokenization, and sorting.createDatabase
function takes a cache object as input and creates a database by executing the following steps:
levSearch
and binds it to the cache object and FUSE_CONFIG object.levSearch
.queryDatabase
function takes a search query as input and returns the results of the Fuse.js search with the tokenized search results filtered out.lookupCell
function takes a cache ID and a cache cells object as input and returns the cell object corresponding to the cache ID.createDatabase
, queryDatabase
, and lookupCell
functions as modules.