Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/AI-EditDistances-Tests/AICosineSimilarityDistanceTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,38 @@ AICosineSimilarityDistanceTest >> testCosineSimilarityDistanceTo [

self assert: (cosineDistance distanceBetween: #(3 45 7 2) and: #(2 54 13 15)) closeTo: 0.9722842517123499
]

{ #category : '*AI-EditDistances-Tests' }
AICosineSimilarityDistanceTest >> testZeroVectors [

"Checks that the Cosine Similarity between zero vectors returns 1.0 or is handled gracefully."
self should: [ metric distanceBetween: #(0 0 0) and: #(0 0 0) ] raise: Error.
]

{ #category : '*AI-EditDistances-Tests' }
AICosineSimilarityDistanceTest >> testIdenticalVectors [

"Checks that the Cosine Similarity between identical vectors is 1.0."
self assert: (cosineDistance distanceBetween: #(1 2 3) and: #(1 2 3)) equals: 1.0
]

{ #category : '*AI-EditDistances-Tests' }
AICosineSimilarityDistanceTest >> testOrthogonalVectors [

"Checks that the Cosine Similarity between orthogonal vectors is 0.0."
self assert: (cosineDistance distanceBetween: #(1 0) and: #(0 1)) closeTo: 0.0
]

{ #category : '*AI-EditDistances-Tests' }
AICosineSimilarityDistanceTest >> testDifferentLengths [

"Checks that an error is raised for collections of different lengths."
self should: [ cosineDistance distanceBetween: #(1 2) and: #(1 2 3) ] raise: Error
]

{ #category : '*AI-EditDistances-Tests' }
AICosineSimilarityDistanceTest >> testNonNumericInputs [

"Checks that an error is raised for non-numeric inputs."
self should: [ cosineDistance distanceBetween: #(1 'a' 3) and: #(1 2 3) ] raise: Error
]
21 changes: 19 additions & 2 deletions src/AI-EditDistances/AICosineSimilarityDistance.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,24 @@ AICosineSimilarityDistance >> distanceBetween: anArray and: anotherArray [
num := (anArray * anotherArray) sum.
size1 := (anArray * anArray) sum sqrt.
size2 := (anotherArray * anotherArray) sum sqrt.

^ num / (size1 * size2)

]

{ #category : 'api' }
AICosineSimilarityDistance >> distanceBetween: firstCollection and: secondCollection[
| dotProduct normA normB |
firstCollection size = secondCollection size ifFalse: [
self error: 'Collections must have the same length' ].
(firstCollection allSatisfy: [ :x | x isNumber ]) ifFalse: [
self error: 'First collection contains non-numeric elements' ].
(secondCollection allSatisfy: [ :x | x isNumber ]) ifFalse: [
self error: 'Second collection contains non-numeric elements' ].
dotProduct := (firstCollection with: secondCollection collect: [ :a :b | a * b ]) sum.
normA := (firstCollection collect: [ :x | x * x ]) sum sqrt.
normB := (secondCollection collect: [ :x | x * x ]) sum sqrt.
(normA = 0 and: [ normB = 0 ])
ifTrue: [ ^ 1.0 ] "Zero vectors are considered identical."
ifFalse: [ ^ dotProduct / (normA * normB) ]
]
Loading