GithubHelp home page GithubHelp logo

lunr-chinese's Introduction

lunr-chinese

Lunr addon, do words segment with nodejieba locally.

Description

Build based on lunr v0.72 and nodejieba, add chinese tokenize and search available to lunr.

The lib not use the lunr v2+ is for the lunr v2+ changes the tokenize method mainly, and which is an improve of the performance for English only, not suitable for Chinese.

new method lunr.init(idx, data, path)

return lunr instance with generated lunr index lunr.init(generatedIndex)
return lunr instance with lunr.init(idx, data(array))
generate lunr index into file lunr.init(idx, data(array), path)

search with lunrInstance.search('例子')

Install

npm i lunr-chinese --save-dev

For front-end use the build file lunr-chinese, lunr-chinese.min.js in the project, which will return the lunr instance for use.

Usage

const lunr = require('lunr-chinese')()

let idx = lunr(function() {
  this.ref('id')

  this.field('title', { boost: 10 })
  this.field('categories')
  this.field('body')
})

const postContents = contents.map(content => idx.add(content))

// get the Lunr instance(use locally)
const lunrCn = lunr.init(idx, postContents)
lunrCn.search('例子')

// generate the Lunr Index file
lunr.init(idx, postContents, 'path/lunrCnIndexs.json')

Load and work with generated index file.

// load `lunr` from lunr-chinese.js

var lunrCnIndexs = JSON.parse(lunrCnIndexsString)
var chineseLunr = lunr.init(lunrCnIndexs)

chineseLunr.search('例子')

To use custom dictionary like this

const lunr = require('lunr-chinese')({
  dict: nodejieba.DEFAULT_DICT,
  hmmDict: nodejieba.DEFAULT_HMM_DICT,
  userDict: './test/testdata/userdict.utf8',
  idfDict: nodejieba.DEFAULT_IDF_DICT,
  stopWordDict: nodejieba.DEFAULT_STOP_WORD_DICT,
  method: 'cutForSearch' //optional [default is `cut`] (call the specific method for `nodejieba`)
});

Method values

'小明硕士毕业于**科学院计算所'

  • cut: [ '小明', '硕士', '毕业', '于', '**科学院', '计算所' ]
  • cutAll: [ '小', '明', '硕士', '毕业', '于', '**', '**科学院', '科学', '科学院', '学院', '计算', '计算所' ]
  • cutHMM: [ '小明', '硕士', '毕业于', '**', '科学院', '计算', '所' ]
  • cutForSearch: [ '小明', '硕士', '毕业', '于', '**', '科学', '学院', '科学院', '**科学院', '计算', '计算所' ]
  • cutSmall: [ '小', '明', '硕', '士', '毕', '业', '于', '中', '国', '科', '学', '院', '计', '算', '所' ]

examples

an example of the custom doc file

抹茶 n
星冰乐 n
拿铁 n

for more details check CppJieba 字典

lunr-chinese's People

Contributors

ole3021 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.