mirror of
https://gh-proxy.org/https://github.com/klinsmaya/SQLBot.git
synced 2026-11-08 01:06:59 +08:00
19 lines
445 B
Python
19 lines
445 B
Python
# Author: Junjun
|
|
# Date: 2025/9/23
|
|
import math
|
|
|
|
|
|
def cosine_similarity(vec_a, vec_b):
|
|
if len(vec_a) != len(vec_b):
|
|
raise ValueError("The vector dimension must be the same")
|
|
|
|
dot_product = sum(a * b for a, b in zip(vec_a, vec_b))
|
|
|
|
norm_a = math.sqrt(sum(a * a for a in vec_a))
|
|
norm_b = math.sqrt(sum(b * b for b in vec_b))
|
|
|
|
if norm_a == 0 or norm_b == 0:
|
|
return 0.0
|
|
|
|
return dot_product / (norm_a * norm_b)
|