Skip to content

Commit d86894e

Browse files
committed
Merge branch 'master' of github.com:stanleyfok/content-based-recommender
2 parents d038c1f + e84d5e9 commit d86894e

File tree

8 files changed

+478
-52
lines changed

8 files changed

+478
-52
lines changed

README.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ const ContentBasedRecommender = require('content-based-recommender')
3131

3232
## What's New
3333

34+
#### 1.5.0
35+
36+
* Added `trainBidirectional(collectionA, collectionB)` to allow recommendations between
37+
two different datasets
38+
3439
#### 1.4.0
3540

3641
Upgrade dependencies to fix security alerts
@@ -49,6 +54,8 @@ Update to newer version of [vector-object](https://www.npmjs.com/package/vector-
4954

5055
## Usage
5156

57+
### Single collection
58+
5259
```js
5360
const ContentBasedRecommender = require('content-based-recommender')
5461
const recommender = new ContentBasedRecommender({
@@ -87,6 +94,115 @@ console.log(similarDocuments);
8794
]
8895
*/
8996
```
97+
98+
### Multi collection
99+
100+
This example shows how to automatically match posts with related tags
101+
102+
```js
103+
const ContentBasedRecommender = require('content-based-recommender')
104+
105+
const posts = [
106+
{
107+
id: '1000001',
108+
content: 'Why studying javascript is fun?',
109+
},
110+
{
111+
id: '1000002',
112+
content: 'The trend for javascript in machine learning',
113+
},
114+
{
115+
id: '1000003',
116+
content: 'The most insightful stories about JavaScript',
117+
},
118+
{
119+
id: '1000004',
120+
content: 'Introduction to Machine Learning',
121+
},
122+
{
123+
id: '1000005',
124+
content: 'Machine learning and its application',
125+
},
126+
{
127+
id: '1000006',
128+
content: 'Python vs Javascript, which is better?',
129+
},
130+
{
131+
id: '1000007',
132+
content: 'How Python saved my life?',
133+
},
134+
{
135+
id: '1000008',
136+
content: 'The future of Bitcoin technology',
137+
},
138+
{
139+
id: '1000009',
140+
content: 'Is it possible to use javascript for machine learning?',
141+
},
142+
];
143+
144+
const tags = [
145+
{
146+
id: '1',
147+
content: 'Javascript',
148+
},
149+
{
150+
id: '2',
151+
content: 'machine learning',
152+
},
153+
{
154+
id: '3',
155+
content: 'application',
156+
},
157+
{
158+
id: '4',
159+
content: 'introduction',
160+
},
161+
{
162+
id: '5',
163+
content: 'future',
164+
},
165+
{
166+
id: '6',
167+
content: 'Python',
168+
},
169+
{
170+
id: '7',
171+
content: 'Bitcoin',
172+
},
173+
];
174+
175+
const tagMap = tags.reduce((acc, tag) => {
176+
acc[tag.id] = tag;
177+
return acc;
178+
}, {});
179+
180+
const recommender = new ContentBasedRecommender();
181+
182+
recommender.trainBidirectional(posts, tags);
183+
184+
for (let post of posts) {
185+
const relatedTags = recommender.getSimilarDocuments(post.id);
186+
const tags = relatedTags.map(t => tagMap[t.id].content);
187+
console.log(post.content, 'related tags:', tags);
188+
}
189+
190+
191+
/*
192+
Why studying javascript is fun? related tags: [ 'Javascript' ]
193+
The trend for javascript in machine learning related tags: [ 'machine learning', 'Javascript' ]
194+
The most insightful stories about JavaScript related tags: [ 'Javascript' ]
195+
Introduction to Machine Learning related tags: [ 'machine learning', 'introduction' ]
196+
Machine learning and its application related tags: [ 'machine learning', 'application' ]
197+
Python vs Javascript, which is better? related tags: [ 'Python', 'Javascript' ]
198+
How Python saved my life? related tags: [ 'Python' ]
199+
The future of Bitcoin technology related tags: [ 'future', 'Bitcoin' ]
200+
Is it possible to use javascript for machine learning? related tags: [ 'machine learning', 'Javascript' ]
201+
*/
202+
203+
```
204+
205+
90206
## API
91207

92208
### constructor([options])
@@ -108,6 +224,12 @@ To tell the recommender about your documents and then it will start training its
108224

109225
* documents - an array of object, with fields **id** and **content**
110226

227+
228+
### trainBidirectional(collectionA, collectionB)
229+
230+
Works like the normal train function, but it creates recommendations
231+
between two different collections instead of within one collection.
232+
111233
### getSimilarDocuments(id, [start], [size])
112234

113235
To get an array of similar items with document id

example/example.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const ContentBasedRecommender = require('../index.js');
2+
const posts = require('../fixtures/sample-documents');
3+
const tags = require('../fixtures/sample-document-tags');
4+
5+
const tagMap = tags.reduce((acc, tag) => {
6+
acc[tag.id] = tag;
7+
return acc;
8+
}, {});
9+
10+
const recommender = new ContentBasedRecommender();
11+
12+
recommender.trainBidirectional(posts, tags);
13+
14+
for (let post of posts) {
15+
const relatedTags = recommender.getSimilarDocuments(post.id);
16+
const tags = relatedTags.map(t => tagMap[t.id].content);
17+
console.log(post.content, 'related tags:', tags);
18+
}
19+
20+

fixtures/sample-document-tags.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module.exports = [
2+
{
3+
id: '1',
4+
content: 'Javascript',
5+
},
6+
{
7+
id: '2',
8+
content: 'machine learning',
9+
},
10+
{
11+
id: '3',
12+
content: 'application',
13+
},
14+
{
15+
id: '4',
16+
content: 'introduction',
17+
},
18+
{
19+
id: '5',
20+
content: 'future',
21+
},
22+
{
23+
id: '6',
24+
content: 'Python',
25+
},
26+
{
27+
id: '7',
28+
content: 'Bitcoin',
29+
},
30+
];

fixtures/sample-documents.js

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,38 @@
11
module.exports = [
2-
{ id: '1000001', content: 'Why studying javascript is fun?' },
3-
{ id: '1000002', content: 'The trend for javascript in machine learning' },
4-
{ id: '1000003', content: 'The most insightful stories about JavaScript' },
5-
{ id: '1000004', content: 'Introduction to Machine Learning' },
6-
{ id: '1000005', content: 'Machine learning and its application' },
7-
{ id: '1000006', content: 'Python vs Javascript, which is better?' },
8-
{ id: '1000007', content: 'How Python saved my life?' },
9-
{ id: '1000008', content: 'The future of Bitcoin technology' },
10-
{ id: '1000009', content: 'Is it possible to use javascript for machine learning?' },
2+
{
3+
id: '1000001',
4+
content: 'Why studying javascript is fun?',
5+
},
6+
{
7+
id: '1000002',
8+
content: 'The trend for javascript in machine learning',
9+
},
10+
{
11+
id: '1000003',
12+
content: 'The most insightful stories about JavaScript',
13+
},
14+
{
15+
id: '1000004',
16+
content: 'Introduction to Machine Learning',
17+
},
18+
{
19+
id: '1000005',
20+
content: 'Machine learning and its application',
21+
},
22+
{
23+
id: '1000006',
24+
content: 'Python vs Javascript, which is better?',
25+
},
26+
{
27+
id: '1000007',
28+
content: 'How Python saved my life?',
29+
},
30+
{
31+
id: '1000008',
32+
content: 'The future of Bitcoin technology',
33+
},
34+
{
35+
id: '1000009',
36+
content: 'Is it possible to use javascript for machine learning?',
37+
},
1138
];
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
module.exports = [
2+
{
3+
id: '1000010',
4+
content: 'Why studying javascript is fun?',
5+
},
6+
{
7+
id: '1000011',
8+
content: 'The trend for javascript in machine learning',
9+
},
10+
{
11+
id: '1000012',
12+
content: 'The most insightful stories about JavaScript',
13+
},
14+
{
15+
id: '1000013',
16+
content: 'Introduction to Machine Learning',
17+
},
18+
{
19+
id: '1000014',
20+
content: 'Machine learning and its application',
21+
},
22+
{
23+
id: '1000015',
24+
content: 'Python vs Javascript, which is better?',
25+
},
26+
{
27+
id: '1000016',
28+
content: 'How Python saved my life?',
29+
},
30+
{
31+
id: '1000017',
32+
content: 'The future of Bitcoin technology',
33+
},
34+
{
35+
id: '1000018',
36+
content: 'Is it possible to use javascript for machine learning?',
37+
},
38+
];

0 commit comments

Comments
 (0)