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

predict-likes

v0.0.4

Published

Predict likes based on a library of attributes

Downloads

12

Readme

Predict Likes

This module relies on a library of objects each with an array of qualities to predict the likes of users.

New in 0.0.4: A control function has been added to generate more meaningful results. See changes in step two and in the default threshold of the predictLike method.

var predict = require('predict-likes');

Step 1: The library is an object with a key-value pair for each member. The setObject method can be used if all of your data is in one place:

var calligraphers = {
	yan:['strength','balance'],
	wang:['elegance','balance','fluidity','strength'],
	su:['strength','balance','fluidity'],
	mi:['fluidity','elegance'],
	huang:['strength','elegance']
};

predict.setObject(calligraphers);

Data can be added to the library with the addObject method, where the first argument is the name of the item and the second argument is an array of its qualities:

predict.setObject('mao',['fluidity','elegance']);

Step 2: To use the control method, first require the control master function.

var predictionControl = require('predict-likes').control();

Then use the getTermFrequency to obtain the frequency of ocurrance for all terms in the library and setTermFrequency to load those values into the library. The functions are separated because calcuating term frequency could take a long time with large libraries, so you might want to run getTermFrequency and store the results in a database for quick retrieval.

predictionControl.getTermFrequency(function(tf) {
  predictionControl.setTermFrequency(tf);
})

You can turn predictionControl off thusly:

predict.useControl(False);

Turning predictionControl off is useful if you just want to check for the proportion of certain qualities in a given users likes and you are not concerned with the prevalence of those qualities in the library.

Step 3: The setQualities method is used to define the qualities that you want to test for. If you want to see what percent of a persons favorites have the quality 'strength' verses the quality 'elegance', you would set like this:

predict.setQualities(['strength', 'elegance']);

Step 4: At this point there are two possibilites. One is to use the process or processArray method to find the prevalence of the set qualities in a given persons likes compared to the prevalence of those qualities in the total library.

The process method takes an object and a callback and the processArray method takes an array and a callback:

//these objects must have an id and a likes array
var john = {id: 'john', likes: ['yan','su','huang']};
var mary = {id: 'mary', likes: ['mi','su', 'huang']};

predict.process(john, function(result) {
	console.log(result);
})
//=> { john: { strength: 50, balance: 16, elegance: -33, fluidity: -33, individuality: 0 } }

predict.processArray([john,mary], function(results) {
	console.log(results);
})
//=> { john: { strength: 50, balance: 16, elegance: -33, fluidity: -33, individuality: 0 }, mary: { strength: 0, balance: -25, elegance: 9, fluidity: 9, individuality: 17 } }

John's strength score of 50 is because 100% of John's likes have the quality of strength, whereas only 50% of the calligraphers in the library have this quality. The individuality score of 0 means that the proportion of calligraphers with the individuality quality in the library matches the proportion in John's likes exactly. Mary gets a negative score on balance because only one out four of her likes has the quality balance, whereas three quarters of calligraphers in the library display this quality.

The last method is predictLike, which takes a person, a list of qualities, an optional threshold and a callback. The algorithm returns all the qualities which occur in the given persons likes at a percentage higher than the threshold, and the threshold defaults to 0 if not given.


var mao = ['fluidity','elegance'];

factor.predictLike(mary, mao, function(result) {
    console.log(result);
})
//=>{ result: true, matches: [ { match: 'fluidity', certainty: 66 }, { match: 'elegance', certainty: 66 } ] }

Here we see that Mary might like Mao's calligraphy given that 66% percent of the calligraphers she likes have the qualities 'fluidity' and 'elegance', both of which are characteristic of Mao's.