Skip to content

Commit bcfd7fc

Browse files
committed
Assign likelihoods based on 'robust model', tied to user/title/body.
1 parent 7f29518 commit bcfd7fc

File tree

2 files changed

+84
-49
lines changed

2 files changed

+84
-49
lines changed

src/resolvers/index.ts

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as scuid from 'scuid';
22

3-
import { generateAuthToken, getUserId, probabilityHistogram, topicWords } from '../utils';
3+
import { generateAuthToken, getUserId, likelyTopics } from '../utils';
44

55
const DEFAULT_COUNT = 25;
66

@@ -66,42 +66,57 @@ export default {
6666
},
6767

6868
// refactor user relation into Class
69-
Post: (parent, { id }, { faker }) => ({
70-
id,
71-
title: faker.random.words(),
72-
body: faker.lorem.paragraphs(),
73-
published: faker.random.boolean(),
74-
createdAt: faker.date.past(),
75-
author: {
76-
id: scuid(),
77-
firstName: faker.name.firstName(),
78-
lastName: faker.name.lastName(),
79-
email: faker.internet.email(),
80-
avatar: faker.image.avatar()
81-
}
82-
}),
83-
84-
// refactor user relation into Class
85-
allPosts: (parent, { count }, { faker }) => {
86-
return new Array(count).fill(0).map(_ => ({
87-
id: scuid(),
88-
title: faker.random.words(),
89-
body: faker.lorem.sentences(),
69+
Post: (parent, { id }, { faker }) => {
70+
const title = faker.random.words();
71+
const body = faker.lorem.paragraphs();
72+
const firstName = faker.name.firstName();
73+
const lastName = faker.name.lastName();
74+
return {
75+
id,
76+
title,
77+
body,
9078
published: faker.random.boolean(),
9179
createdAt: faker.date.past(),
9280
author: {
9381
id: scuid(),
94-
firstName: faker.name.firstName(),
95-
lastName: faker.name.lastName(),
82+
firstName,
83+
lastName,
9684
email: faker.internet.email(),
9785
avatar: faker.image.avatar()
9886
},
99-
likelyTopics: probabilityHistogram(10)
100-
.map((likelihood, i) => ({
101-
label: topicWords[i],
102-
likelihood: likelihood
103-
}))
104-
}));
87+
likelyTopics: likelyTopics(
88+
`${firstName} ${lastName}`,
89+
title, body
90+
)
91+
};
92+
},
93+
94+
// refactor user relation into Class
95+
allPosts: (parent, { count }, { faker }) => {
96+
return new Array(count).fill(0).map(_ => {
97+
const title = faker.random.words();
98+
const body = faker.lorem.paragraphs();
99+
const firstName = faker.name.firstName();
100+
const lastName = faker.name.lastName();
101+
return {
102+
id: scuid(),
103+
title,
104+
body,
105+
published: faker.random.boolean(),
106+
createdAt: faker.date.past(),
107+
author: {
108+
id: scuid(),
109+
firstName,
110+
lastName,
111+
email: faker.internet.email(),
112+
avatar: faker.image.avatar()
113+
},
114+
likelyTopics: likelyTopics(
115+
`${firstName} ${lastName}`,
116+
title, body
117+
)
118+
}
119+
});
105120
}
106121
},
107122

src/utils.ts

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,48 @@ export const getUserId = ctx => {
1717
throw new Error('Not Authorized');
1818
};
1919

20-
// Generate a uniform probability histogram
21-
export const probabilityHistogram = bins => {
22-
const independent = []
23-
let total = 0
24-
for (let i = 0; i < bins; i++) {
25-
const weight = Math.random()
26-
total += weight
27-
independent.push(weight)
28-
}
29-
return independent.map(weight => weight / total)
30-
}
31-
3220
// Ten randomly-chosen but consistent topic nouns.
3321
export const topicWords = [
34-
'drama',
3522
'wedding',
36-
'management',
37-
'security',
38-
'quality',
23+
'sport',
3924
'fishing',
40-
'client',
25+
'shopping',
26+
'security',
27+
'management',
28+
'community',
29+
'celebrity',
4130
'birthday',
42-
'disaster',
4331
'potato'
44-
]
32+
];
33+
34+
export const likelyTopics = (user, title, body) => {
35+
// create a custom order over the topics, based on letters as they appear in
36+
// the user's name, post title, and post body, for variability
37+
const alphabet = `${title} ${user} ${body}`;
38+
const topicLabels = topicWords.sort((a, b) => {
39+
for (let i = 0; i < Math.min(a.length, b.length); i++) {
40+
const aValue = alphabet.indexOf(a[i]);
41+
const bValue = alphabet.indexOf(b[i]);
42+
if (aValue !== bValue) {
43+
return bValue - aValue;
44+
}
45+
}
46+
return b.length - a.length;
47+
})
48+
// compute a biased probability distribution
49+
const likelihoods = [];
50+
let total = 0;
51+
for (let i = 0; i < topicLabels.length; i++) {
52+
const weight = Math.random() * Math.log(i + 2);
53+
total += weight;
54+
likelihoods.push(weight);
55+
}
56+
likelihoods.reverse();
57+
// and associate each likelihood with a probability
58+
return likelihoods.map((weight, i) => {
59+
return {
60+
likelihood: weight / total,
61+
label: topicLabels[i]
62+
}
63+
}).sort((a, b) => b.likelihood - a.likelihood);
64+
};

0 commit comments

Comments
 (0)