cosine-similarity 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/cosine_similarity.rb +10 -0
- data/spec/cosine_similarity_spec.rb +25 -0
- metadata +61 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: e7968a15556f769f7cb90338cebaf24c359ac2cb221f5e6c1381d1e31421d85e
|
4
|
+
data.tar.gz: 855618955acffcfd7c419b746cf9c53fb0f9be1adfc7bbd2b0c9814f6356057f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0dbcbf28d6d8d57f4efa65f7c53e8ac1d08f9f431d3ce5fd2fc1e2d0844a91a1939ae991ab43b54d0fed8681dbfc600be0123819d9c18393281d3695725fadca
|
7
|
+
data.tar.gz: fdbd3dec631e3c695324581cd99936d43f3913b3c2143490678fa517cdb8c8b070f46b236c24bf8951f41c36d3d38d385919120122697f47b36a95e731b34dcf
|
@@ -0,0 +1,10 @@
|
|
1
|
+
def cosine_similarity(embedding1, embedding2)
|
2
|
+
if !embedding1.is_a?(Array) || !embedding2.is_a?(Array)
|
3
|
+
return 'Error: Both arguments must be arrays'
|
4
|
+
else
|
5
|
+
dot_product = embedding1.zip(embedding2).map{|x,y| x*y}.sum
|
6
|
+
norm1 = Math.sqrt(embedding1.map{|x| x**2}.sum)
|
7
|
+
norm2 = Math.sqrt(embedding2.map{|x| x**2}.sum)
|
8
|
+
return dot_product / (norm1 * norm2)
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require './cosine_similarity.rb'
|
2
|
+
|
3
|
+
describe '#cosine_similarity' do
|
4
|
+
context 'when both arguments are arrays' do
|
5
|
+
it 'returns the cosine similarity of the two identical embeddings' do
|
6
|
+
embedding1 = [1, 2, 3]
|
7
|
+
embedding2 = [1, 2, 3]
|
8
|
+
expect(cosine_similarity(embedding1, embedding2)).to eq(1)
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'returns the cosine similarity of the two identical embeddings' do
|
12
|
+
embedding1 = [1, 2, 3]
|
13
|
+
embedding2 = [1, 2, 4]
|
14
|
+
expect(cosine_similarity(embedding1, embedding2)).to eq(0.9914601339836675)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'when either argument is not an array' do
|
19
|
+
it 'returns an error message' do
|
20
|
+
embedding1 = [1, 2, 3]
|
21
|
+
embedding2 = 'not an array'
|
22
|
+
expect(cosine_similarity(embedding1, embedding2)).to eq('Error: Both arguments must be arrays')
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cosine-similarity
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kane Hooper
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-02-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.4'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.4'
|
27
|
+
description: This gem provides a method for calculating the cosine similarity between
|
28
|
+
two vector embeddings.
|
29
|
+
email:
|
30
|
+
- kanehooper@hotmail.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- lib/cosine_similarity.rb
|
36
|
+
- spec/cosine_similarity_spec.rb
|
37
|
+
homepage: https://rubygems.org/gems/cosine-similarity
|
38
|
+
licenses:
|
39
|
+
- MIT
|
40
|
+
metadata:
|
41
|
+
source_code_uri: https://github.com/reinteractive/cosine-similarity
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
requirements: []
|
57
|
+
rubygems_version: 3.1.4
|
58
|
+
signing_key:
|
59
|
+
specification_version: 4
|
60
|
+
summary: A gem for calculating cosine similarity between two vectors.
|
61
|
+
test_files: []
|