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

clustring

v0.0.10

Published

Algorithms for clustering strings

Downloads

21

Readme

clustring

Sort groups of strings into buckets.

Inspired by OpenRefine's clustering.

API

The unit of analysis is a bucket. It looks like this:

const bucket = {
	"commonWord": 3,
	"CommonWord": 20,
	"SuperRareWord": 1
}

Binning

strcluster can break your bucket into bins by computing a bin key for each string. Here's a code sample, using the bucket from above:

import { clusterByKey } from 'clustring'
import fingerprint from 'clustring/key/fingerprint'

const clusterer = clusterByKey(bucket, fingerprint())

clusterer.cluster()
  .then(bins => { ... })
// bins is:
// [
//   {
//     "name": "CommonWord",
//     "key": "commonword",
//     "count": 23,
//     "bucket": { "commonWord": 3, "CommonWord": 20 }
//   }
// ]

KNN

strcluster can also break your bucket into bins using a distance function to compare two strings.

Distance functions aren't cheap. A block-based approach avoids comparisons by grouping strings into "blocks" that all contain the same N sequence of characters. (Effectively, this skips comparisons by assuming infinite distance if there is no such sequence). TODO: implement this

Here's some sample code:

import { clusterByKnn } from 'clustring'
import levenshtein from 'clustring/knn/levenshtein'

// levenshtein(2) is an optimization of levenshtein() that returns 0, 1, 2, or
// Infinity. You may use levenshtein(), but it's not recommended.
const clusterer = clusterByKnn(bucket, levenshtein(2), 2, { blockSize: 5 })
clusterer.cluster()
  .then(bins => { ... })
// bins will be same as in previous example, minus "key"

Progress reporting

cluster() returns a Promise immediately and processes in the background (in the current thread). It cedes control to the event loop every few milliseconds so your app remains responsive.

To track progress, try something like this:

const clusterer = clusterByKey(bucket, fingerprint(), { tickMs: 8 })

let timeout = null
function reportProgressAndReschedule () {
  console.log('Progress: ', clusterer.progress)
  timeout = setTimeout(reportProgressAndReschedule, 1)
}
// start progress-report loop
timeout = setTimeout(reportProgressAndReschedule, 1)

clusterer.cluster()
  .then(bins => {
    clearTimeout(timeout)
    // ... handle bins
  })

During cluster(), clustring will periodically check whether it has blocked the main thread for more than tickMs milliseconds. if it has, it will cede control to the event loop for one event-loop "tick" before resuming. Your setTimeout() callback will only be called once cluster() cedes control, even though it requests to be called after 1 millisecond.

Cancellation

If you wish to stop clustering, run clusterer.cancel(). Of course, you can only execute clusterer.cancel() during a tick, so consider your tickMs.

Developing

npm install
npm test -- --watch             # runs tests continuously
npm run-script build -- --watch # builds continuously

Pick a feature; write a test; make it pass; commit.

Deploying

  1. Update version in package.json
  2. npm install to update package-lock.json
  3. git commit -am 'vx.x.x && git tag vx.x.x && git push && git push origin vx.x.x
  4. npm publish