idftags 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ec135ce8c1deaea30ed837e3c237499732ef6a95
4
+ data.tar.gz: 45d5083727cd8c1afc4331e4f159a5acc95e6ab3
5
+ SHA512:
6
+ metadata.gz: 92f0f020b18a19571fed563bfd8748907f936566ec79a54a92b5584782a062b2c8922ac3a8b92caae03393ce16ace8e1ee3cae4b270b22d0d88fe18a316f8a77
7
+ data.tar.gz: c3d593a18adee689b103944842eb2fff1a8b01253266abf913e6ac844452cca5366ed4a7f7e6408daef42f7c2a0fe27c232111161bda1af4cf6eadd0148a31c6
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers 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. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in idftags.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Stefan Neidig
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.
@@ -0,0 +1,116 @@
1
+ # IDFTags
2
+
3
+ IDFTags extracts tags (or significant words) from a string using the inverse document frequency algorithm. You can find more about this [here](https://en.wikipedia.org/wiki/Tf%E2%80%93idf#Definition)
4
+
5
+ In summary the algorithm considers a document, which is a string a and computes the weighted frequency within that document. This tells us how common the word is. In fact the more interesting value is the inverse document frequency. For this we consider a list of documents and calculate the weighted frequency of a term in all that documents. Multiply this values for the term and you have the tdidf value. Based on this value you can rank the terms of a document and pick the first n values, which are your tags.
6
+
7
+ ### What is this?
8
+
9
+ This is a library to generate tags from a document based on other documents to calculate the meaning of a word. So you have to provide a list of documents to get proper results. This applies very well to titles or content of form posts (like for example stackoverflow). This gem is not meant to extract tags using self learning algorithms, which are solely based on the single document itself. Keep that in mind. There will be an example to showcase the gem.
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'IDFTags'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install IDFTags
26
+
27
+ ## Usage
28
+ ### Basic
29
+ IDFtags needs a document and a list of documents to calculate both the document frequency and inverse document freuency of terms. So let be the document and documents respectively
30
+
31
+ ```ruby
32
+ document = 'This is a document'
33
+ documents = ['A sample sentence', 'Another sample sentence']
34
+ ```
35
+
36
+ Then you can retrieve the three most prominent tags of the document by
37
+
38
+ ```ruby
39
+ require 'idftags'
40
+
41
+ idftags = IDFTags::IDFTags.new
42
+ tags = idftags.tags document, documents, 3
43
+ ```
44
+
45
+ ### Custom weights
46
+ You can also use custom weights to refine the output. This is especially useful when dealing with longer documents.
47
+
48
+ ```ruby
49
+ require 'idftags'
50
+
51
+ idftags = IDFTags::IDFTags.new :weight_log_norm, :weight_probabilistic_inverse_frequency
52
+ tags = idftags.tags 'document', ['document1', 'document2']
53
+ ```
54
+ Currently available weights:
55
+
56
+ | document frequency | inverse document frequency |
57
+ |--------------------|----------------------------------------|
58
+ | weight_binary | weight_unary |
59
+ | weight_raw | weight_inverse_frequency |
60
+ | weight_log_norm | weight_inverse_frequency_smooth |
61
+ | weight_double_norm | weight_probabilistic_inverse_frequency |
62
+
63
+ Take a look at https://en.wikipedia.org/wiki/Tf%E2%80%93idf#Term_frequency_2 for a more in depth explanation and recommendation of what weights you should choose.
64
+
65
+ ### Bad words
66
+ Usually you want to exclude certain words from the parsing process since they do not carry any information whatsoever. An exmaple of such words are 'a', 'and', 'I', 'to' and so on. You can exclude them by providing a so called bad word lexicon. Currently we support bad word lexica by locale.
67
+
68
+ ```ruby
69
+ require 'idftags'
70
+
71
+ lexicon = IDFTags::BadWordLexicon.new :en, ['a', 'to']
72
+ lexicon.add('I')
73
+ lexicon.add_all(['my', 'me'])
74
+
75
+ idftags = IDFTags::IDFTags.new
76
+ idftags.register_bad_word_lexicon lexicon
77
+
78
+ idftags.tags 'I want to see me', [....] # -> ['see', 'want']
79
+ ```
80
+ You can also create bad word lexica from yaml files.
81
+ ```ruby
82
+ require 'idftags'
83
+
84
+ lexicon = IDFTags::BadWordLexicon.from_yml 'path/to/file.yml'
85
+ ```
86
+ whereas a yml file should look like this
87
+ ```yaml
88
+ en:
89
+ common:
90
+ yes: 'yes'
91
+ no: 'no'
92
+ test:
93
+ nested:
94
+ cool: 'cool'
95
+ nested2:
96
+ nice: 'nice'
97
+ ```
98
+ To unregister a bad word lexicon simply do
99
+ ```ruby
100
+ idftags.unregister_bad_word_lexicon :en
101
+ ```
102
+ ## Development
103
+
104
+ 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.
105
+
106
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
107
+
108
+ ## Contributing
109
+
110
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/IDFTags. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
111
+
112
+
113
+ ## License
114
+
115
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
116
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "idftags"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'IDFTags/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "idftags"
8
+ spec.version = IDFTags::VERSION
9
+ spec.authors = ["Stefan Neidig"]
10
+ spec.email = ["stefan@rpdev.net"]
11
+
12
+ spec.summary = %q{IDFTags generates tags based on text and documents.}
13
+ spec.description = %q{IDFTags generates tags based on text and documents. It uses inverse document frequency to calculate the meaning of words.}
14
+ spec.homepage = "https://github.com/dasheck0/idftags"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec"
25
+ end
@@ -0,0 +1,50 @@
1
+ require 'yaml'
2
+
3
+ module IDFTags
4
+ class BadWordLexicon
5
+
6
+ attr_accessor :locale
7
+ attr_reader :bad_words
8
+
9
+ def initialize locale, bad_words = []
10
+ @locale = locale
11
+ @bad_words = bad_words
12
+ end
13
+
14
+ def add bad_word
15
+ @bad_words << bad_word if bad_word
16
+ @bad_words.uniq!
17
+ end
18
+
19
+ def add_all bad_words
20
+ @bad_words += bad_words if bad_words
21
+ @bad_words.uniq!
22
+ end
23
+
24
+ def self.from_yml filename
25
+ if File.exist? filename
26
+ content = YAML.load_file(filename)
27
+
28
+ result = BadWordLexicon.new content.keys.first.to_sym
29
+ result.traverse(content) { |node|
30
+ result.add node
31
+ }
32
+
33
+ result
34
+ end
35
+ end
36
+
37
+ def traverse hash, &blk
38
+ case hash
39
+ when Hash
40
+ hash.each { |_, v| traverse(v, &blk) }
41
+ when Array
42
+ hash.each { |v| traverse(v, &blk) }
43
+ else
44
+ blk.call(hash)
45
+ end
46
+
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,51 @@
1
+ require_relative './utilities'
2
+
3
+ module IDFTags
4
+ class InverseDocumentFrequency
5
+
6
+ attr_accessor :weight
7
+
8
+ def initialize weight = :weight_inverse_frequency
9
+ raise Exception("Weight #{weight} is not defined. Available weights are #{weights}") unless weights.include? weight
10
+ @weight = weight
11
+ end
12
+
13
+ def inverse_document_frequency term, documents
14
+ self.send(weight, term, documents)
15
+ end
16
+
17
+ private
18
+
19
+ def weights
20
+ private_methods.select { |method|
21
+ method.to_s.split('_').first == 'weight'
22
+ }.map(&:to_sym)
23
+ end
24
+
25
+ def weight_unary term, documents
26
+ 1
27
+ end
28
+
29
+ def weight_inverse_frequency term, documents
30
+ return 0 if term.nil? or documents.nil? or documents.empty?
31
+ Math.log(documents.length.to_f / (1 +Utilities.document_frequency(term, documents)))
32
+ end
33
+
34
+ def weight_inverse_frequency_smooth term, documents
35
+ return 0 if term.nil? or documents.nil? or documents.empty?
36
+ Math.log((1+documents.length.to_f) / (1+Utilities.document_frequency(term, documents)))
37
+ end
38
+
39
+ def weight_inverse_frequency_max term, documents
40
+
41
+ end
42
+
43
+ def weight_probabilistic_inverse_frequency term, documents
44
+ return 0 if term.nil? or documents.nil? or documents.empty?
45
+ nt = Utilities.document_frequency(term, documents)
46
+ Math.log((documents.length.to_f - nt) / (1 + nt))
47
+
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,43 @@
1
+ module IDFTags
2
+ class TermFrequency
3
+
4
+ attr_accessor :weight
5
+
6
+ def initialize weight = :weight_raw
7
+ raise Exception("Weight #{weight} is not defined. Available weights are #{weights}") unless weights.include? weight
8
+ @weight = weight
9
+ end
10
+
11
+ def term_frequency term, document
12
+ self.send(weight, term, document)
13
+ end
14
+
15
+ private
16
+
17
+ def weights
18
+ private_methods.select { |method|
19
+ method.to_s.split('_').first == 'weight'
20
+ }.map(&:to_sym)
21
+ end
22
+
23
+ def weight_binary term, document
24
+ return 0 if document.nil? or term.nil?
25
+ document.include?(term) ? 1 : 0
26
+ end
27
+
28
+ def weight_raw term, document
29
+ Utilities.raw_frequency term, document
30
+ end
31
+
32
+ def weight_log_norm term, document
33
+ return 0 if document.nil? or term.nil?
34
+ Math.log(1 + weight_raw(term, document))
35
+ end
36
+
37
+ def weight_double_norm term, document, k = 0.5
38
+ return 0 if document.nil? or term.nil?
39
+ k + (1-k) * weight_raw(term, document).to_f / Utilities.max_frequency(document)
40
+ end
41
+
42
+ end
43
+ end
@@ -0,0 +1,22 @@
1
+ module IDFTags
2
+ class Utilities
3
+
4
+ def self.max_frequency document
5
+ return 0 if document.nil? or document.empty?
6
+ document.split.map { |term| raw_frequency(term, document) }.max
7
+ end
8
+
9
+ def self.raw_frequency term, document
10
+ return 0 if term.nil? or document.nil?
11
+ document.split.map(&:strip).select { |t| t == term }.count
12
+ end
13
+
14
+ def self.document_frequency term, documents
15
+ return 0 if term.nil? or documents.nil?
16
+ documents.select { |document|
17
+ raw_frequency(term, document) > 0
18
+ }.count
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module IDFTags
2
+ VERSION = "0.2.2"
3
+ end
@@ -0,0 +1,54 @@
1
+ require 'IDFTags/version'
2
+ require 'IDFTags/inverse_document_frequency'
3
+ require 'IDFTags/term_frequency'
4
+ require 'IDFTags/bad_word_lexicon'
5
+
6
+ module IDFTags
7
+
8
+ class IDFTags
9
+
10
+ attr_accessor :tf, :idf
11
+
12
+ def initialize td_weight = :weight_raw, idf_weight = :weight_inverse_frequency
13
+ @tf = TermFrequency.new td_weight
14
+ @idf = InverseDocumentFrequency.new idf_weight
15
+ @bad_word_lexica = []
16
+ end
17
+
18
+ def tags document, documents, tag_count = 5
19
+ extract_terms(document).map { |term|
20
+ [term, tfidf(term, document, documents)]
21
+ }.sort_by { |v| v.last}.reverse[0..(tag_count-1)].map(&:first)
22
+ end
23
+
24
+ def register_bad_word_lexicon bad_word_lexicon
25
+ lexicon = @bad_word_lexica.select { |l| l.locale == bad_word_lexicon.locale }.first
26
+
27
+ if lexicon
28
+ lexicon.add_all bad_word_lexicon.bad_words
29
+ else
30
+ @bad_word_lexica << bad_word_lexicon
31
+ end
32
+ end
33
+
34
+ def unregister_bad_word_lexicon locale
35
+ @bad_word_lexica.delete_if { |l| l.locale == locale }
36
+ end
37
+
38
+ private
39
+
40
+ def extract_terms document
41
+ prepare_document(document).split.uniq - @bad_word_lexica.map{ |l| l.nil? ? [] : l.bad_words.map(&:downcase) }.flatten.uniq
42
+ end
43
+
44
+ def prepare_document document
45
+ document.downcase.gsub(/(\,|\.)/, '') if document
46
+ end
47
+
48
+ def tfidf term, document, documents
49
+ @idf.inverse_document_frequency(term, documents) * @tf.term_frequency(term, document)
50
+ end
51
+
52
+ end
53
+
54
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: idftags
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.2
5
+ platform: ruby
6
+ authors:
7
+ - Stefan Neidig
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-11-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: IDFTags generates tags based on text and documents. It uses inverse document
56
+ frequency to calculate the meaning of words.
57
+ email:
58
+ - stefan@rpdev.net
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - ".rspec"
65
+ - ".travis.yml"
66
+ - CODE_OF_CONDUCT.md
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - idftags.gemspec
74
+ - lib/IDFTags/bad_word_lexicon.rb
75
+ - lib/IDFTags/inverse_document_frequency.rb
76
+ - lib/IDFTags/term_frequency.rb
77
+ - lib/IDFTags/utilities.rb
78
+ - lib/IDFTags/version.rb
79
+ - lib/idftags.rb
80
+ homepage: https://github.com/dasheck0/idftags
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '0'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.4.6
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: IDFTags generates tags based on text and documents.
104
+ test_files: []