-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from wangfenjin/node
add node example
- Loading branch information
Showing
7 changed files
with
5,899 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,8 @@ bin/ | |
output/ | ||
output-no-jieba/ | ||
chat.db | ||
examples/node/node_modules/ | ||
examples/node/lib/ | ||
|
||
# CLion | ||
.idea/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# node example | ||
|
||
``` | ||
# download lib from github | ||
npm install | ||
# run example | ||
npm run p | ||
# remove build folder | ||
npm run clean | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
const { task, logger } = require('just-scripts') | ||
const download = require('download') | ||
const path = require('path') | ||
|
||
task('install', () => { | ||
return new Promise((resolve, reject) => { | ||
const localPath = path.join(__dirname, 'lib') | ||
var platform = process.env.npm_config_target_platform || process.platform | ||
logger.info(`[install] Target platform: ${platform}`) | ||
if (platform === 'darwin') { | ||
platform = 'osx'; | ||
} else if (platform === 'win32') { | ||
platform = 'windows'; | ||
} | ||
var arch = process.env.npm_config_target_arch || process.arch | ||
logger.info(`[install] Target arch: ${arch}`) | ||
if (platform !== 'windows' && arch === 'x64') { | ||
arch = 'amd64'; | ||
} | ||
const downloadUrl = `https://github.com/wangfenjin/simple/releases/download/v0.0.4/libsimple-${platform}-${arch}.zip` | ||
logger.info(`[install] Download prebuilt binaries from ${downloadUrl}`) | ||
download(downloadUrl, localPath, { | ||
extract: true, strip: 1 | ||
}).then(() => { | ||
resolve() | ||
}).catch(err => { | ||
logger.warn(`[install] Failed to download package from: ${downloadUrl}, err: ${err}`) | ||
reject() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
var path = require("path"); | ||
var sqlite3 = require('sqlite3').verbose(); | ||
var db = new sqlite3.Database(':memory:'); | ||
|
||
db.serialize(function() { | ||
const extension_path = path.resolve("./lib/"); | ||
console.log("extension path: " + extension_path); | ||
// load extension | ||
var platform = process.env.npm_config_target_platform || process.platform | ||
if (platform === 'win32') { | ||
db.loadExtension(path.join(extension_path, "simple")); | ||
} else { | ||
db.loadExtension(path.join(extension_path, "libsimple")); | ||
} | ||
// set the jieba dict file path | ||
db.run("select jieba_dict(?)", path.join(extension_path, "dict")); | ||
// create table | ||
db.run("CREATE VIRTUAL TABLE t1 USING fts5(x, tokenize = 'simple')"); | ||
// insert some data | ||
db.run("insert into t1(x) values ('周杰伦 Jay Chou:我已分不清,你是友情还是错过的爱情'), ('周杰伦 Jay Chou:最美的不是下雨天,是曾与你躲过雨的屋檐'), ('I love China! 我爱中国!我是中华人民共和国公民!'), ('@English &special _characters.\"''bacon-&and''-eggs%')"); | ||
|
||
// with match 周杰伦 | ||
db.each("select rowid as id, simple_highlight(t1, 0, '[', ']') as info from t1 where x match simple_query('zjl')", function(err, row) { | ||
console.log(row.id + ": " + row.info); | ||
}); | ||
// will match 中国 and 中华人民共和国 | ||
db.each("select rowid as id, simple_highlight(t1, 0, '[', ']') as info from t1 where x match simple_query('中国')", function(err, row) { | ||
console.log(row.id + ": " + row.info); | ||
}); | ||
// will match 中国 but not 中华人民共和国 | ||
db.each("select rowid as id, simple_highlight(t1, 0, '[', ']') as info from t1 where x match jieba_query('中国')", function(err, row) { | ||
console.log(row.id + ": " + row.info); | ||
}); | ||
}); | ||
|
||
db.close(); |
Oops, something went wrong.