Function
distanceToScore
(distance float64, metric string)
Source from the content-addressed store, hash-verified
| 200 | } |
| 201 | |
| 202 | func distanceToScore(distance float64, metric string) float64 { |
| 203 | switch metric { |
| 204 | case "cosine": |
| 205 | // cosine distance in [0,2], 相似度越高距离越小 |
| 206 | score := 1.0 - distance |
| 207 | if score < -1 { |
| 208 | score = -1 |
| 209 | } |
| 210 | if score > 1 { |
| 211 | score = 1 |
| 212 | } |
| 213 | return score |
| 214 | case "l2": |
| 215 | // 简单取负值, 距离越小得分越高 |
| 216 | return -distance |
| 217 | default: |
| 218 | return 1.0 - distance |
| 219 | } |
| 220 | } |
Tested by
no test coverage detected