npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

vietnamese-text-search

v1.0.25

Published

Text search for vietnamese.

Downloads

17

Readme

vietnamese-search

Text search for Vietnamese.

Install

npm install vietnamese-text-search

Usage

Import

Commonjs

const TextSearch = require('vietnamese-text-search');

ES6

import TextSearch from 'vietnamese-text-search';

Example

Initialize a TextSearch's instance for searching on product's names
import ProductObjs from './products.json';
import TextSearch from 'vietnamese-text-search';

(async () => {
  const options = {
    thresholdScore: 0.5,    // Default: 0.5
    limit: 30,              // Default: 30
    sortOrder: -1,          // Default: -1
    textKeyName: 'id',      // Default: 'textId'
    textValueName: 'name',  // Default: 'text'
    useAddedScore: false,   // Default: false
    autoGenBucket: true     // Default: true
  };
  // Initialize textSearch instance with `ProductObjs` and `options`
  const textSearch = await TextSearch.init(ProductObjs, options);
  ...
})
Add a new product to bucket products
// ...
const product = { id: '123', name: 'mặt nạ siêu thấm hút Aqua', addedScore: 0.1 };
// Because we initialized a `TextSearch`'s instance with {..., textKeyName: 'id', textValueName: 'name'},
// so any other product which added to the bucket later should has format { id: ..., name: ... }
const addResult = await textSearch.addNewTextObj(product, { bucket: 'products' });
console.log(addResult);

// { nAdded: 1, bucket: 'products' }
// ...
Search for mặt nạ Aqua
// ...
// Search with options
const searchOptions = {
  limit: 10,
  thresholdScore: 1,
  useAddedScore: true, // add `addedScore` when ranking text objects by their text score
  buckets: ['products'] // only search on bucket `products`
};
const searchResult = await textSearch.search('mặt nạ Aqua', searchOptions);
console.log(searchResult);

// {
//   data: [
//     // [id, score]
//     [ '123', 4.69999999999... ], 
//     ...
//   ],
//   sortOrder: -1,
//   thresholdScore: 1,
//   offset: 0,
//   limit: 10,
//   total: 100,
//   text: 'mặt nạ Aqua'
// }
// ...
Update the name and addedScore of a product
// ...
const upProduct = { id: '123', name: 'mặt nạ siêu thấm ngược Aqua', addedScore: 0.2 };
const updateResult = await textSearch.updateTextObj(upProduct.id, upProduct, {
  bucket: 'products'
});
console.log(updateResult);

// { nUpserted: 1, bucket: 'products' }
// ...
Remove a product
// ...
const removeResult = await textSearch.removeTextObj('123', { bucket: 'products' });
console.log(removeResult);

// { nRemoved: 1, bucket: 'products' }
// ...

APIs

| API | Description | | ------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | .init(textObjs, options, cb) | Initialize a TextSearch's instance with options.Params:textObjs: Array of text objects (e.g. [{ id: '123, name: 'Son môi siêu thấm hút', addedScore: 0.1 }, ...]).options: {   limit (number, default: 30): Limit number of search results.   sortOrder (-1:Descending, 1: Ascending, default: -1): Order of results by score.   thresholdScore (number, default: 0.5): Only results which have the text score >= this threshold should be returned.   textKeyName (string, default: 'textId'): Field used as key of a text object.   textValueName (string, default: 'text'): Field used as value of a text object.   autoGenBucket (boolean, default: true): When Adding new text objects, Generate new bucket if not exist.   bucket (string, default: 'default'): Bucket which text objects will be added into when initializing a TextSearch's instance with textObjs   useAddedScore (boolean, default: false): addedScore from each text object will be added to its score when ranking results by the score.  }Return: {Promise<TextSearch>} | | .search(text, options, cb) | Search for text.Params:text: Text to search (e.g. son môi aqua).options: {   limit (number, default: 30): Limit number of search results.   sortOrder (-1:Descending, 1: Ascending, default: -1): Order of results by score.   thresholdScore (number, default: 0.5): Only results which have the text score >= this threshold should be returned.   buckets (string, default: [all buckets]): Only search in this buckets.   useAddedScore (boolean, default: false): addedScore from each text object will be added to its score when ranking results by the score.  }Return: {Promise<{ data: [[<textKey>, <score>], ...], total: number, text: string, ...[options] }>} | | .addTextObj(textObj, options, cb) | Add a new text object.Params:textObj: Text object to add (e.g. { id: '123', name: 'Son môi siêu thấm hút' }).options: {   bucket (string, default: 'default'): Add new text object to this bucket.  }Return: {Promise<{ nAdded: number, bucket: string }>} | | .addManyTextObjs(textObjs, options, cb) | Add many text objects.Params:textObjs: Array of text objects to add.options: {   bucket (string, default: 'default'): Add new text objects to this bucket.  }Return: {Promise<{ nAdded: number, details: {*} }>} | | .updateTextObj(textKey, textObj, options, cb) | Update a text object.Params:textKey: Text key of object to update.textObj: This text object will be merged with the old or added to a bucket if option upsert is true.options: {   upsert (boolean, default: false): Add the text object if not exist.   bucket (string, default: 'default'): Update text object in this bucket.  }Return: {Promise<{ nUpserted: number, bucket: string }>} | | .updateManyTextObjs(textObjs, options, cb) | Update many text objects.Params:textObjs: This text objects will be merged with the old or added to a bucket if option upsert is true.(Note: Text objects must contain text key, the text key will be used to find the object need to update (e.g. [{id: '123', name: 'Son môi siêu thấm ngược Alan', addedScore: 0.2}, ...])options: {   upsert (boolean, default: false): Add the text object if not exist.   bucket (string, default: 'default'): Update text objects in this bucket.  }Return: {Promise<{ nUpserted: number, details: {*} }>} | | .removeTextObj(textKey, options, cb) | Remove a text object.Params:textKey: Text key of object to remove.options: {   forceRemove (boolean, default: false): Do not throw an error if textKey not found.    bucket (string, default: 'default'): Remove text object from this bucket.  }Return: {Promise<{ nUpserted: number, bucket: string }>} | | .removeTextObjs(textKeys, options, cb) | Remove many text objects.Params:textKeys: Remove text objects with these text keys.options: {   forceRemove (boolean, default: false): Do not throw an error if textKey not found.    bucket (string, default: 'default'): Remove text object from this bucket.  }Return: {Promise<{ nRemoved: number, details: {*} }>} | | .removeBuckets(buckets) | Remove text bucket(s).Params:buckets: Remove a bucket (e.g. 'products') or remove many buckets (e.g. ['products', 'stores', 'companies']).Return: {{ nRemoved: number, nRemains: number }} | | .getStats() | Get stats of the instance.Return: {{ nObjects: number, nIndices: number }} |

Notes

  1. TextObject should has format like { textId: '123', text: 'Son môi siêu thấm hút', addedScore: 0.1 } if options textKeyName and textValueName are not be set when initializing the instance. Otherwise, the object should has format { [textKeyName]: textKey, [textValueName]: textValue, addedScore: 0.1 }.

  2. addedScore of a text object is 0.0 if a text object not include it.