From 4bedeed33ccb0b0467a66c37ae5125b5f869ef76 Mon Sep 17 00:00:00 2001 From: Mann Patel <130435633+MannPatel0@users.noreply.github.com> Date: Sun, 23 Feb 2025 19:34:39 -0700 Subject: [PATCH] trying the reconmmondation in golang --- backend/recommondation.go | 45 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 backend/recommondation.go diff --git a/backend/recommondation.go b/backend/recommondation.go new file mode 100644 index 0000000..f87cb26 --- /dev/null +++ b/backend/recommondation.go @@ -0,0 +1,45 @@ +package main + +import ( + "fmt" + "math" +) + +func cosine(x, y []int) float64 { + var dotProduct, normX, normY float64 + for i := 1; i < len(x); i++ { + dotProduct += float64(x[i] * y[i]) + normX += float64(x[i] * x[i]) + normY += float64(y[i] * y[i]) + } + return dotProduct / (math.Sqrt(normX) * math.Sqrt(normY)) +} + +func main() { + history := [][]int{ + {1, 0, 1, 0}, + {0, 1, 0, 1}, + {1, 0, 1, 1}, + } + + productCategory := [][]int{ + {1, 1, 0, 0}, + {0, 0, 0, 1}, + {0, 0, 1, 0}, + {0, 0, 1, 1}, + {0, 1, 0, 0}, + + {0, 1, 1, 1}, + {1, 1, 1, 0}, + {0, 0, 0, 1}, + {1, 1, 1, 1}, + } + + // Calculate similarity between first search and each product + for i, product := range productCategory { + sim := cosine(history[0], product) + fmt.Printf("Similarity with product %d: %f\n", i, sim) + } +} + +