doc_sim 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 829be4b580fb7cfe5d0d1014e4b70aa9404606f284c929a6be1bf1272b0d6f5c
4
+ data.tar.gz: d1cda541ae00d55ba0d0bd6aa073993ae2ed70c1f7778870576926adcf1e0b61
5
+ SHA512:
6
+ metadata.gz: 8f842f40a808d33564e617fad4c562c68acc35f232358ccfd6f9c1c82445d57137cd7f91f945f4b8da0df65f97d6deddebf3a4d201c39944a9cee7ff2918176d
7
+ data.tar.gz: 7ec44e6bcf2e5cc8339344599a3c841776aa593bd0370525f7b9ca6b2748e38fe03ac855519afcfbc0f3d8c0f8633a50d9d12d8f78e6dcf274029dc67b72a65f
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,34 @@
1
+ AllCops:
2
+ TargetRubyVersion: 3.2
3
+ NewCops: enable
4
+
5
+ Style/StringLiterals:
6
+ Enabled: true
7
+ EnforcedStyle: double_quotes
8
+
9
+ Style/StringLiteralsInInterpolation:
10
+ Enabled: true
11
+ EnforcedStyle: double_quotes
12
+
13
+ Layout/LineLength:
14
+ Max: 120
15
+
16
+ Gemspec/DevelopmentDependencies:
17
+ Enabled: true
18
+ EnforcedStyle: gemspec
19
+
20
+ Gemspec/RequireMFA:
21
+ Enabled: false
22
+
23
+ Metrics/BlockLength:
24
+ Enabled: true
25
+ Exclude:
26
+ - spec/**/*.rb
27
+ - doc_sim.gemspec
28
+
29
+ Naming/MethodParameterName:
30
+ AllowedNames:
31
+ - x
32
+ - y
33
+ - z
34
+ - k
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 3.2.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2023-08-18
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at castlehoneyjung@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Forthoney
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Document Similarity - Efficient probablistic algorithm for calculating document similarity
2
+
3
+ A Ruby implementation of [Mining of Massive Datasets](http://www.mmds.org/)'s document similarity algorithm. It uses Minhash and Localitiy Sensitive Hashing to efficiently find documents with a high probability of being similar.
4
+
5
+ ## Installation
6
+
7
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_PRIOR_TO_RELEASE_TO_RUBYGEMS_ORG
16
+
17
+ ## Usage
18
+
19
+ 1. Shingle your documents using `Shingling.shingle` (k-shingling). The optimal k value differs based on the type of argument, but 5 is a good first guess.
20
+ 2. Initialize a `Minhash::Minhash`.
21
+ 3. Use the `Minhash.signature` to get the signature array for each document.
22
+ 4. Initialize a `LocalitySensitiveHashing::LocalitySensitiveHashing` with appropriate band and row length, depending on your desired precision and recall.
23
+ 5. `insert` each signaturea array with its index into the LSH
24
+ 6. Take all potentially similar pairs (candiate pairs) from the LSH
25
+ 7. (Optional) Rigorously calculate the similarity between the candidate pairs. This will need to be done manually.
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/Forthoney/document_similarity. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/Forthoney/document_similarity/blob/main/CODE_OF_CONDUCT.md).
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the LocalitySensitiveHashing project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/Forthoney/document_similarity/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/Steepfile ADDED
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+ # D = Steep::Diagnostic
3
+ #
4
+ # target :lib do
5
+ # signature "sig"
6
+ #
7
+ # check "lib" # Directory name
8
+ # check "Gemfile" # File name
9
+ # check "app/models/**/*.rb" # Glob
10
+ # # ignore "lib/templates/*.rb"
11
+ #
12
+ # # library "pathname" # Standard libraries
13
+ # # library "strong_json" # Gems
14
+ #
15
+ # # configure_code_diagnostics(D::Ruby.default) # `default` diagnostics setting (applies by default)
16
+ # # configure_code_diagnostics(D::Ruby.strict) # `strict` diagnostics setting
17
+ # # configure_code_diagnostics(D::Ruby.lenient) # `lenient` diagnostics setting
18
+ # # configure_code_diagnostics(D::Ruby.silent) # `silent` diagnostics setting
19
+ # # configure_code_diagnostics do |hash| # You can setup everything yourself
20
+ # # hash[D::Ruby::NoMethod] = :information
21
+ # # end
22
+ # end
23
+
24
+ # target :test do
25
+ # signature "sig", "sig-private"
26
+ #
27
+ # check "test"
28
+ #
29
+ # # library "pathname" # Standard libraries
30
+ # end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LocalitySensitiveHashing
4
+ # Hashing technique which groups similar objects together
5
+ class LocalitySensitiveHashing
6
+ def initialize(n_rows, n_bands)
7
+ @buckets = Array.new(n_bands) { generate_band_bucket }
8
+ @n_rows = n_rows
9
+ end
10
+
11
+ def insert(signature, doc_id)
12
+ if signature.length != @n_rows * @buckets.length
13
+ raise(ArgumentError, "signature length does not match n_rows and n_bands")
14
+ end
15
+
16
+ signature.each_slice(@n_rows).with_index do |band_signature, band_idx|
17
+ @buckets[band_idx][band_signature] << doc_id
18
+ end
19
+ end
20
+
21
+ def similar_pairs
22
+ similar = Set.new
23
+ @buckets.each do |band_bucket|
24
+ band_bucket.each_value do |bucket|
25
+ similar.merge(bucket.combination(2))
26
+ end
27
+ end
28
+ similar
29
+ end
30
+
31
+ private
32
+
33
+ def generate_band_bucket
34
+ Hash.new { |table, key| table[key] = [] }
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "murmurhash3"
4
+
5
+ module Minhash
6
+ # Class for generating Minhash signature
7
+ class Minhash
8
+ attr_reader :seed_root
9
+
10
+ def initialize(n_hashes = 1, seed_root = rand(2**32))
11
+ @seed_root = seed_root
12
+ @hashes = Array.new(n_hashes) do |seed|
13
+ ->(x) { MurmurHash3::V32.str_hash(x, seed_root + seed) }
14
+ end
15
+ end
16
+
17
+ # Produces the Minhash signature for a given Set
18
+ #
19
+ # @param set [Set] the set to produce the signature for
20
+ #
21
+ # @return [Array[Integer]] 32 bit integer array of length n_hashes
22
+ def signature(set)
23
+ counter = Array.new(@hashes.length, Float::INFINITY)
24
+ set.each do |elem|
25
+ @hashes.each_with_index do |hash_func, i|
26
+ counter[i] = [counter[i], hash_func.call(elem)].min
27
+ end
28
+ end
29
+ counter
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Shingling
4
+ def self.shingle(document, k)
5
+ max_index = document.length - k + 1
6
+ max_index.times.map do |i|
7
+ document[i...(i + k)]
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DocumentSimilarity
4
+ VERSION = "0.1.0"
5
+ end
data/lib/doc_sim.rb ADDED
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "doc_sim/version"
4
+ require "doc_sim/minhash"
5
+ require "doc_sim/locality_sensitive_hashing"
6
+ require "doc_sim/shingling"
7
+
8
+ module DocumentSimilarity
9
+ class Error < StandardError; end
10
+ # Your code goes here...
11
+ end
@@ -0,0 +1,16 @@
1
+ # TypeProf 0.21.3
2
+
3
+ # Classes
4
+ module LocalitySensitiveHashing
5
+ class LocalitySensitiveHashing
6
+ @buckets: Array[Hash[Set[Integer], Array[Integer]]]
7
+ @n_rows: Integer
8
+
9
+ def initialize: (Integer n_rows, Integer n_bands) -> void
10
+ def insert: (Set[Integer] signature, Integer id) -> void
11
+ def similar_pairs: -> Set[Array[Integer]]
12
+
13
+ private
14
+ def generate_band_bucket: -> Hash[Set[Integer], Array[Integer]]
15
+ end
16
+ end
@@ -0,0 +1,15 @@
1
+ module Minhash
2
+ # Class for generating Minhash signature
3
+ class Minhash
4
+ attr_reader seed_root: Integer
5
+
6
+ def initialize: (?Integer n_hashes, ?Integer seed_root) -> void
7
+
8
+ # Produces the Minhash signature for a given Set
9
+ #
10
+ # @param set [Set] the set to produce the signature for
11
+ #
12
+ # @return [Array[Integer]] 32 bit integer array of length n_hashes
13
+ def signature: (Set[String] set) -> Array[Integer]
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module Shingling
2
+ def self.shingle: (String document, Integer k) -> Array[String]
3
+ end
data/sig/doc_sim.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module DocumentSimilarity
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: doc_sim
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Forthoney
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-08-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: murmurhash3
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.7
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.7
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.12'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.12'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.56'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.56'
55
+ - !ruby/object:Gem::Dependency
56
+ name: steep
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 1.5.3
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.5.3
69
+ description:
70
+ email:
71
+ - castlehoneyjung@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".rspec"
77
+ - ".rubocop.yml"
78
+ - ".ruby-version"
79
+ - CHANGELOG.md
80
+ - CODE_OF_CONDUCT.md
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - Steepfile
85
+ - lib/doc_sim.rb
86
+ - lib/doc_sim/locality_sensitive_hashing.rb
87
+ - lib/doc_sim/minhash.rb
88
+ - lib/doc_sim/shingling.rb
89
+ - lib/doc_sim/version.rb
90
+ - sig/doc_sim.rbs
91
+ - sig/doc_sim/locality_sensitive_hashing.rbs
92
+ - sig/doc_sim/minhash.rbs
93
+ - sig/doc_sim/shingling.rbs
94
+ homepage: https://github.com/Forthoney/doc_sim
95
+ licenses:
96
+ - MIT
97
+ metadata:
98
+ homepage_uri: https://github.com/Forthoney/doc_sim
99
+ source_code_uri: https://github.com/Forthoney/doc_sim
100
+ changelog_uri: https://github.com/Forthoney/doc_sim/blob/main/CHANGELOG.md
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: 3.2.0
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubygems_version: 3.4.19
117
+ signing_key:
118
+ specification_version: 4
119
+ summary: Calculating approximate document similarity withlocality sensitive hashing
120
+ algorithm using Minhash signatures
121
+ test_files: []