Gathering detailed insights and metrics for content-based-recommender-ts
Gathering detailed insights and metrics for content-based-recommender-ts
Gathering detailed insights and metrics for content-based-recommender-ts
Gathering detailed insights and metrics for content-based-recommender-ts
A simple content-based recommender implemented in JavaScript with TypeScript support.
npm install content-based-recommender-ts
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
5 Stars
82 Commits
7 Branches
1 Contributors
Updated on May 29, 2024
Latest Version
2.1.0
Package Id
content-based-recommender-ts@2.1.0
Unpacked Size
39.18 kB
Size
9.49 kB
File Count
13
NPM Version
6.14.13
Node Version
14.17.3
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
This is a fork from the content-based-recommender package with support for TS.
This is a simple content-based recommender implemented in javascript to illustrate the concept of content-based recommendation. Content-based recommender is a popular recommendation technique to show similar items to users, especially useful to websites for e-commerce, news content, etc.
After the recommender is trained by an array of documents, it can tell the list of documents which are more similar to the input document.
The training process involves 3 main steps:
Special thanks to the library natural helps a lot by providing a lot of NLP functionalities, such as tf-idf and word stemming.
⚠️ Note:
I haven't tested how this recommender is performing with a large dataset.
npm install content-based-recommender
or
yarn add content-based-recommender
Then import the ContentBasedRecommender class
1const ContentBasedRecommender = require('content-based-recommender')
Check the CHANGELOG.
1const ContentBasedRecommender = require('content-based-recommender') 2const recommender = new ContentBasedRecommender({ 3 minScore: 0.1, 4 maxSimilarDocs: 100 5}) 6 7// prepare documents data 8const documents = [ 9 { id: '1000001', content: 'Why studying javascript is fun?' }, 10 { id: '1000002', content: 'The trend for javascript in machine learning' }, 11 { id: '1000003', content: 'The most insightful stories about JavaScript' }, 12 { id: '1000004', content: 'Introduction to Machine Learning' }, 13 { id: '1000005', content: 'Machine learning and its application' }, 14 { id: '1000006', content: 'Python vs Javascript, which is better?' }, 15 { id: '1000007', content: 'How Python saved my life?' }, 16 { id: '1000008', content: 'The future of Bitcoin technology' }, 17 { id: '1000009', content: 'Is it possible to use javascript for machine learning?' } 18] 19 20// start training 21recommender.train(documents) 22 23//get top 10 similar items to document 1000002 24const similarDocuments = recommender.getSimilarDocuments('1000002', 0, 10) 25 26console.log(similarDocuments) 27/* 28 the higher the score, the more similar the item is 29 documents with score < 0.1 are filtered because options minScore is set to 0.1 30 [ 31 { id: '1000004', score: 0.5114304586412038 }, 32 { id: '1000009', score: 0.45056313558918837 }, 33 { id: '1000005', score: 0.37039308109283564 }, 34 { id: '1000003', score: 0.10896767690747626 } 35 ] 36*/
This example shows how to automatically match posts with related tags
1const ContentBasedRecommender = require('content-based-recommender') 2 3const posts = [ 4 { 5 id: '1000001', 6 content: 'Why studying javascript is fun?' 7 }, 8 { 9 id: '1000002', 10 content: 'The trend for javascript in machine learning' 11 }, 12 { 13 id: '1000003', 14 content: 'The most insightful stories about JavaScript' 15 }, 16 { 17 id: '1000004', 18 content: 'Introduction to Machine Learning' 19 }, 20 { 21 id: '1000005', 22 content: 'Machine learning and its application' 23 }, 24 { 25 id: '1000006', 26 content: 'Python vs Javascript, which is better?' 27 }, 28 { 29 id: '1000007', 30 content: 'How Python saved my life?' 31 }, 32 { 33 id: '1000008', 34 content: 'The future of Bitcoin technology' 35 }, 36 { 37 id: '1000009', 38 content: 'Is it possible to use javascript for machine learning?' 39 } 40] 41 42const tags = [ 43 { 44 id: '1', 45 content: 'Javascript' 46 }, 47 { 48 id: '2', 49 content: 'machine learning' 50 }, 51 { 52 id: '3', 53 content: 'application' 54 }, 55 { 56 id: '4', 57 content: 'introduction' 58 }, 59 { 60 id: '5', 61 content: 'future' 62 }, 63 { 64 id: '6', 65 content: 'Python' 66 }, 67 { 68 id: '7', 69 content: 'Bitcoin' 70 } 71] 72 73const tagMap = tags.reduce((acc, tag) => { 74 acc[tag.id] = tag 75 76 return acc 77}, {}) 78 79const recommender = new ContentBasedRecommender() 80 81recommender.trainBidirectional(posts, tags) 82 83for (const post of posts) { 84 const relatedTags = recommender.getSimilarDocuments(post.id) 85 const tags = relatedTags.map(t => tagMap[t.id].content) 86 console.log(post.content, 'related tags:', tags) 87} 88 89 90/* 91Why studying javascript is fun? related tags: [ 'Javascript' ] 92The trend for javascript in machine learning related tags: [ 'machine learning', 'Javascript' ] 93The most insightful stories about JavaScript related tags: [ 'Javascript' ] 94Introduction to Machine Learning related tags: [ 'machine learning', 'introduction' ] 95Machine learning and its application related tags: [ 'machine learning', 'application' ] 96Python vs Javascript, which is better? related tags: [ 'Python', 'Javascript' ] 97How Python saved my life? related tags: [ 'Python' ] 98The future of Bitcoin technology related tags: [ 'future', 'Bitcoin' ] 99Is it possible to use javascript for machine learning? related tags: [ 'machine learning', 'Javascript' ] 100*/ 101
To create the recommender instance
Supported options:
To tell the recommender about your documents and then it will start training itself.
Works like the normal train function, but it creates recommendations between two different collections instead of within one collection.
To get an array of similar items with document id
It returns an array of objects, with fields id and score (ranging from 0 to 1)
To export the recommender as json object.
1const recommender = new ContentBasedRecommender() 2recommender.train(documents) 3 4const object = recommender.export() 5//can save the object to disk, database or otherwise
To update the recommender by importing from a json object, exported by the export() method
1const recommender = new ContentBasedRecommender() 2recommender.import(object) // object can be loaded from disk, database or otherwise
1npm i 2npm run test
No vulnerabilities found.
No security vulnerabilities found.