Skip to content

Commit a9ee708

Browse files
committed
cosine similarity
1 parent 41fc84b commit a9ee708

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

server/chat-socket/src/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,12 @@ app.post("/api/sim_score", function(req, res) {
7373
const inputTags = tagManager.getTags(req.body.input)
7474
const baseTags = tagManager.getTags(req.body.base)
7575
const score = tagManager.similarityScore(inputTags, baseTags) + ""
76-
res.send(score)
76+
const resp = {
77+
input_tags: inputTags,
78+
base_tags: baseTags,
79+
score: score
80+
}
81+
res.send(resp)
7782
})
7883

7984
function countSocketAndUsers() {

server/chat-socket/src/tag.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,21 @@ const tagManager = {
8282
// TODO: longer word match should weigh more
8383
// e.g. 这个杀手不太冷 weigh more than 文艺片
8484
if (inputTags.length == 0) return 0
85-
let score = 0
85+
let matchCount = 0
8686
inputTags.forEach(tag => {
8787
if (baseTags.includes(tag)) {
8888
// small numbers are less important
8989
// E.g. "you have 2 new messages" show up in page title
90-
if (!isNaN(tag) && tag < 10) {
91-
score += 0.1
92-
} else {
93-
score += 1
94-
}
90+
// if (!isNaN(tag) && tag < 10) {
91+
// score += 0.1
92+
// } else {
93+
// score += 1
94+
// }
95+
matchCount += 1
9596
}
9697
})
97-
return (score * score) / inputTags.length
98+
99+
return (matchCount * matchCount) / (inputTags.length * baseTags.length)
98100
},
99101
getTags: pageTitle => {
100102
// TODO: write test cases

server/chat-socket/src/tag/stopwords.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"",
3131
"",
3232
"",
33+
"",
3334
"",
3435
"视频",
3536
"弹幕",

0 commit comments

Comments
 (0)