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

nearest-neighbor

v0.0.3

Published

Nearest neighbor algorithm to find the most similar object within an array of objects.

Downloads

132

Readme

Node Nearest Neighbor

Nearest neighbor algorithm to find the most similar object within an array of objects.

Installation

$ npm install nearest-neighbor

Usage

var nn = require('nearest-neighbor');
nn.findMostSimilar(query, items, fields, function(nearestNeighbor, probability) {
  console.log(nearestNeighbor);
  console.log(probability);
});

items is an array of objects that acts as the haystack that should be searched for the most similar query object. The fields array includes all the keys of the object the similarity should be calculated of and a comparison method to perform the similarity check. The callback returns the most similar object from the items array and the probability of the match between 1 and 0.

Example

nn = require('nearest-neighbor');

var items = [
  { name: "Bill", age: 10, pc: "Mac", ip: "68.23.13.8" },
  { name: "Alice", age: 22, pc: "Windows", ip: "193.186.11.3" },
  { name: "Bob", age: 12, pc: "Windows", ip: "56.89.22.1" }
];

var query = { name: "Bob", age: 12, pc: "Windows", ip: "68.23.13.10" };

var fields = [
  { name: "name", measure: nn.comparisonMethods.word },
  { name: "age", measure: nn.comparisonMethods.number },
  { name: "pc", measure: nn.comparisonMethods.word }, 
  { name: "ip", measure: nn.comparisonMethods.ip }
];

nn.findMostSimilar(query, items, fields, function(nearestNeighbor, probability) {
  console.log(query);
  console.log(nearestNeighbor);
  console.log(probability);
});

Output

The most similar object from the items array compared to the query object and the probability ([0..1]) of the match found.

{ name: 'Bob', age: 12, pc: 'Windows', ip: '68.23.13.10' }
{ name: 'Bob', age: 12, pc: 'Windows', ip: '56.89.22.1' }
0.9764705882352941

Comparison Methods

There are some basic comparison methods shipped with this module. Every comparison method has two parameters and returns a number between 1 and 0. a is the current item from the items array b is always the query object

// example comparison function
function(a, b) {
  return 0.5;
};

The predefined comparison functions are available through the nn.comparisonMethods object. E.g. nn.comparisonMethods.exact.

  • exact: Checks if a and b have the exact same value (using ===).
  • word: Calculates the similarity of two words.
  • wordArray: Calculates the similarity of an array of words.
  • ip: Calculates the similarity of two IP addresses.
  • ipArray: Calculates the similarity of an array of IP addresses.
  • number: Calculates the similarity two numbers.

Add your custom comparison method

You can also roll your own comparison method if you like. Just add your custom method to the nn.comparisonMethods object.

// define items and query as shown before

nn.comparisonMethods.custom = function(a, b) {
	// compare something...
	var value = ...
  	return value; // between 0 and 1
};

// then use your custom method for one of the comparison fields
var fields = [
  { name: "name", measure: nn.comparisonMethods.custom },
  { name: "age", measure: nn.comparisonMethods.number },
  { name: "pc", measure: nn.comparisonMethods.word }, 
  { name: "ip", measure: nn.comparisonMethods.ip }
];

// use nn.findMostSimilar as shown before

Tests

$ npm test

{ name: 'Bob', age: 12, pc: 'Windows', ip: '68.23.13.10' }
{ name: 'Bob', age: 12, pc: 'Windows', ip: '56.89.22.1' }
0.9764705882352941

All tests OK

Contributing

  • Create something awesome, make the code better, add some functionality, whatever (this is the hardest part).
  • Fork it
  • Create new branch to make your changes
  • Commit all your changes to your branch
  • Submit a pull request

Contact

Feel free to get in touch.

Licence

Copyright (C) 2013 Alexander Schuch

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.