Skip to content

Commit 8e6935a

Browse files
Add star ranking to similar items (#2)
1 parent 35f210b commit 8e6935a

File tree

4 files changed

+108
-41
lines changed

4 files changed

+108
-41
lines changed

Diff for: .dockerignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dist/
2+
.vscode/
3+
node_modules/

Diff for: package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@
2727
"route-cache": "0.4.3",
2828
"serve-favicon": "^2.4.5",
2929
"vue": "^2.5.22",
30+
"vue-client-only": "^2.0.0",
3031
"vue-router": "^3.0.1",
3132
"vue-server-renderer": "^2.5.22",
33+
"vue-star-rating": "^1.6.1",
3234
"vuex": "^3.0.1",
3335
"vuex-router-sync": "^5.0.0"
3436
},
@@ -55,4 +57,4 @@
5557
"webpack-merge": "^4.2.1",
5658
"webpack-node-externals": "^1.7.2"
5759
}
58-
}
60+
}

Diff for: src/api/index.js

+30-7
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ if (api.onServer) {
1616
warmCache()
1717
}
1818

19-
function warmCache () {
19+
function warmCache() {
2020
fetchItems((api.cachedIds.top || []).slice(0, 30))
2121
setTimeout(warmCache, 1000 * 60 * 15)
2222
}
2323

24-
function fetch (child) {
24+
function fetch(child) {
2525
logRequests && console.log(`fetching ${child}...`)
2626
const cache = api.cachedItems
2727
if (cache && cache.has(child)) {
@@ -41,25 +41,25 @@ function fetch (child) {
4141
}
4242
}
4343

44-
export function fetchIdsByType (type) {
44+
export function fetchIdsByType(type) {
4545
return api.cachedIds && api.cachedIds[type]
4646
? Promise.resolve(api.cachedIds[type])
4747
: fetch(`${type}stories`)
4848
}
4949

50-
export function fetchItem (id) {
50+
export function fetchItem(id) {
5151
return fetch(`item/${id}`)
5252
}
5353

54-
export function fetchItems (ids) {
54+
export function fetchItems(ids) {
5555
return Promise.all(ids.map(id => fetchItem(id)))
5656
}
5757

58-
export function fetchUser (id) {
58+
export function fetchUser(id) {
5959
return fetch(`user/${id}`)
6060
}
6161

62-
export function watchList (type, cb) {
62+
export function watchList(type, cb) {
6363
let first = true
6464
const ref = api.child(`${type}stories`)
6565
const handler = snapshot => {
@@ -87,3 +87,26 @@ export function fetchSimilar(queries) {
8787
.then(resp => resp.json())
8888
.then(json => json.rankings.map(i => i.entries));
8989
}
90+
91+
function uuidv4() {
92+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
93+
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
94+
return v.toString(16);
95+
});
96+
}
97+
98+
export default function sendFeedBack(query, result, rating) {
99+
if (document.sessionId === undefined) {
100+
document.sessionId = uuidv4()
101+
}
102+
return fetchData('https://textsimilarity.research.peltarion.com/feedback', {
103+
method: 'POST',
104+
body: JSON.stringify({
105+
query: query,
106+
result: result,
107+
rating: rating,
108+
dataset: 'hn-sbert',
109+
sessionId: document.sessionId
110+
})
111+
})
112+
}

Diff for: src/components/Similar.vue

+72-33
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,105 @@
11

22
<template>
33
<div class="similar-posts">
4-
<span class="title">Similar: </span>
4+
<span class="title">Similar:</span>
55
<ul class="list" v-if="story.similar && story.similar.length !== 0">
6-
<li v-for="sim in story.similar" :key="sim.id">
7-
<span class="box" v-bind:style="{ background: getColor(sim.similarity_score)}" v-bind:title="sim.similarity_score"></span>
8-
<router-link :to="'/item/' + sim.id">{{ sim.title }} | <b>{{ new Date(sim.time * 1000).getFullYear() }}</b> | {{ sim.descendants }} comments</router-link>
6+
<li v-for="sim in story.similar" :key="sim.id">
7+
<span
8+
class="box"
9+
v-bind:style="{ background: getColor(sim.similarity_score)}"
10+
v-bind:title="sim.similarity_score"
11+
></span>
12+
<router-link :to="'/item/' + sim.id">
13+
{{ sim.title }} |
14+
<b>{{ new Date(sim.time * 1000).getFullYear() }}</b>
15+
| {{ sim.descendants }} comments |
16+
</router-link>
17+
<client-only>
18+
<div>
19+
<star-rating
20+
v-bind:star-size="15"
21+
active-color="#000000"
22+
v-bind:show-rating="false"
23+
@rating-selected="setRating($event, sim.title, story.title)"
24+
v-bind:inline="true"
25+
></star-rating>
26+
</div>
27+
</client-only>
928
</li>
1029
</ul>
1130
<div v-else class="no-posts">
1231
<span class="title">No similar posts found.</span>
1332
</div>
14-
1533
</div>
1634
</template>
1735

1836
<script>
37+
import StarRating from "vue-star-rating";
38+
import ClientOnly from "vue-client-only";
39+
import sendFeedBack from "../api";
1940
2041
export default {
21-
name: 'similar-posts',
22-
props: ['story'],
42+
name: "similar-posts",
43+
props: ["story"],
44+
components: {
45+
StarRating,
46+
ClientOnly
47+
},
2348
data: () => {
2449
return {
2550
colorStops: [
26-
{ start: 0, stop: 0.7, color: 'rgba(250, 157, 18, 0.5)' },
27-
{ start: 0.7, stop: 0.8, color: 'rgba(250, 157, 18, 0.75)' },
28-
{ start: 0.8, stop: 2, color: 'rgba(250, 157, 18, 1)' }
51+
{ start: 0, stop: 0.7, color: "rgba(250, 157, 18, 0.5)" },
52+
{ start: 0.7, stop: 0.8, color: "rgba(250, 157, 18, 0.75)" },
53+
{ start: 0.8, stop: 2, color: "rgba(250, 157, 18, 1)" }
2954
]
3055
};
3156
},
3257
methods: {
3358
getColor: function(score) {
34-
const found = this.colorStops.find(cs => score.toFixed(2) >= cs.start && score.toFixed(2) < cs.stop);
59+
const found = this.colorStops.find(
60+
cs => score.toFixed(2) >= cs.start && score.toFixed(2) < cs.stop
61+
);
3562
return found.color;
63+
},
64+
65+
setRating: function(rating, similar, story) {
66+
sendFeedBack(story, similar, rating);
3667
}
3768
}
38-
}
69+
};
3970
</script>
4071

4172
<style lang="stylus">
42-
.similar-posts
43-
line-height 1.2
73+
.similar-posts {
74+
line-height: 1.2;
75+
76+
.box {
77+
width: 12px;
78+
height: 0.85em;
79+
display: inline-block;
80+
margin-right: 8px;
81+
}
4482
45-
.box
46-
width: 12px
47-
height: .85em
48-
display: inline-block
49-
margin-right: 8px
83+
.title {
84+
font-size: 0.85em;
85+
}
86+
87+
ul.list, .no-posts {
88+
background-color: #f3f3f3;
89+
font-size: 0.85em;
90+
margin: 4px 0;
91+
padding: 8px 12px;
92+
border-radius: 4px;
5093
51-
.title
52-
font-size .85em
53-
ul.list, .no-posts
54-
background-color #f3f3f3
55-
font-size .85em
56-
margin 4px 0
57-
padding 8px 12px
58-
border-radius 4px
59-
li
60-
padding: 4px
61-
display: flex
62-
align-items: center
63-
a
64-
color: #828282;
94+
li {
95+
padding: 4px;
96+
display: flex;
97+
align-items: center;
98+
}
99+
}
65100
101+
a {
102+
color: #828282;
103+
}
104+
}
66105
</style>

0 commit comments

Comments
 (0)