analtex 0.1.0

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: 8cbce5ca3ff877f7704df9c0043b017175e54b0f
4
+ data.tar.gz: 7442adf43d5a6aa477dfaa49b89278378dd9c34c
5
+ SHA512:
6
+ metadata.gz: 6e36cef2ad32701962d8a1f55cabb6c3a72e9fa8495c3177d60b625492238c6b09190dc5aaed92f030355e50bf152ba3f13f5e2429f1de71e153f29aa1ffc569
7
+ data.tar.gz: a629d94a2ebbb136ccf36ec7ead2b58be4f41eb703a1664cc55768336f9a6f1114ebe4a36918ee2df4b38005e3f164753d8e0808ea48e84578a854626d00c058
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -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 analtex.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 David Santoro
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,41 @@
1
+ # Analtex
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/analtex`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'analtex'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install analtex
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. 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 tags, 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/[USERNAME]/analtex. 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.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'test'
6
+ t.pattern = "test/*_test.rb"
7
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'analtex/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "analtex"
8
+ spec.version = Analtex::VERSION
9
+ spec.authors = ["David Santoro", "Federico Rebora"]
10
+ spec.email = ["soulnafein@gmail.com"]
11
+
12
+ spec.summary = %q{Text analysis for Ruby}
13
+ spec.description = %q{Text analysis for Ruby}
14
+ spec.homepage = "http://www.github.com/"
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 "minitest", "~> 4.7.3"
23
+ spec.add_development_dependency "pry"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.10"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "analtex"
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,38 @@
1
+ class DocumentFrequencyCalculator
2
+ attr_reader :document_frequencies
3
+
4
+ def initialize(dictionary)
5
+ @dictionary = dictionary
6
+ dictionary_size = @dictionary.words.keys.length
7
+ @document_frequencies = Array.new(dictionary_size, 0).to_a
8
+ @num_documents = 0
9
+ @idf = Array.new(dictionary_size, 0).to_a
10
+ end
11
+
12
+ def add_counts_from_bag_of_words(bag_of_words)
13
+ bag_of_words.each_with_index do |count, word_index|
14
+ @document_frequencies[word_index] = 1 + @document_frequencies[word_index] if (count > 0)
15
+ end
16
+ @num_documents += 1
17
+ end
18
+
19
+ def calculate_idfs
20
+ for i in 0..@dictionary.words.count
21
+ @idf[i] = Math.log(@num_documents.to_f / @document_frequencies[i].to_f)
22
+ end
23
+ end
24
+
25
+ def normalize_bag_of_words(bag_of_words)
26
+ total_terms_in_document = bag_of_words.reduce(0, &:+)
27
+ normalized_bag_of_words = Array.new(bag_of_words.length, 0).to_a
28
+ bag_of_words.each_with_index do |count, word_index|
29
+ raise 'This should not happen' if total_terms_in_document == 0
30
+ tf = count.to_f / total_terms_in_document.to_f
31
+ tf_idf = tf * @idf[word_index]
32
+ raise 'Got a NaN!' if tf_idf.nan?
33
+ normalized_bag_of_words[word_index] = tf_idf
34
+ end
35
+
36
+ normalized_bag_of_words
37
+ end
38
+ end
@@ -0,0 +1,8 @@
1
+ require "analtex/version"
2
+ require "analtex/bag_of_words"
3
+ require "analtex/dictionary"
4
+ require "analtex/document_frequency_calculator"
5
+
6
+ module Analtex
7
+ # Your code goes here...
8
+ end
@@ -0,0 +1,20 @@
1
+ module Analtex
2
+ class BagOfWords
3
+ def initialize(dictionary)
4
+ @dictionary = dictionary
5
+ end
6
+
7
+ def get_bag_of(words)
8
+ dictionary_size = @dictionary.words.length
9
+ bag = Array.new(dictionary_size, 0).to_a
10
+
11
+ words.each do |word|
12
+ index = @dictionary.words[word]
13
+ next if index.nil?
14
+ bag[index] = bag[index] + 1
15
+ end
16
+
17
+ return bag
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,37 @@
1
+ module Analtex
2
+ class Dictionary
3
+ def initialize
4
+ @word_counts = Hash.new { 0 }
5
+ @word_indices = {}
6
+ end
7
+
8
+ def add_word(word)
9
+ @word_counts[word] += 1
10
+ end
11
+
12
+ def add_words(words)
13
+ words.each do |word|
14
+ add_word(word)
15
+ end
16
+ end
17
+
18
+ def words
19
+ @word_indices
20
+ end
21
+
22
+ def word_counts
23
+ @word_counts
24
+ end
25
+
26
+ def prepare(min_occurrences = 1)
27
+ raise 'Do not run twice!' unless @word_indices.keys.empty?
28
+
29
+ @word_counts.each_pair do |word, occurrences|
30
+ if occurrences >= min_occurrences
31
+ new_word_index = @word_indices.length
32
+ @word_indices[word] = new_word_index
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,40 @@
1
+ module Analtex
2
+ class DocumentFrequencyCalculator
3
+ attr_reader :document_frequencies
4
+
5
+ def initialize(dictionary)
6
+ @dictionary = dictionary
7
+ dictionary_size = @dictionary.words.keys.length
8
+ @document_frequencies = Array.new(dictionary_size, 0).to_a
9
+ @num_documents = 0
10
+ @idf = Array.new(dictionary_size, 0).to_a
11
+ end
12
+
13
+ def add_counts_from_bag_of_words(bag_of_words)
14
+ bag_of_words.each_with_index do |count, word_index|
15
+ @document_frequencies[word_index] = 1 + @document_frequencies[word_index] if (count > 0)
16
+ end
17
+ @num_documents += 1
18
+ end
19
+
20
+ def calculate_idfs
21
+ for i in 0..@dictionary.words.count
22
+ @idf[i] = Math.log(@num_documents.to_f / @document_frequencies[i].to_f)
23
+ end
24
+ end
25
+
26
+ def normalize_bag_of_words(bag_of_words)
27
+ total_terms_in_document = bag_of_words.reduce(0, &:+)
28
+ normalized_bag_of_words = Array.new(bag_of_words.length, 0).to_a
29
+ bag_of_words.each_with_index do |count, word_index|
30
+ raise 'This should not happen' if total_terms_in_document == 0
31
+ tf = count.to_f / total_terms_in_document.to_f
32
+ tf_idf = tf * @idf[word_index]
33
+ raise 'Got a NaN!' if tf_idf.nan?
34
+ normalized_bag_of_words[word_index] = tf_idf
35
+ end
36
+
37
+ normalized_bag_of_words
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module Analtex
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: analtex
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - David Santoro
8
+ - Federico Rebora
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2015-10-29 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: minitest
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: 4.7.3
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: 4.7.3
28
+ - !ruby/object:Gem::Dependency
29
+ name: pry
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: bundler
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '1.10'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '1.10'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rake
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '10.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '10.0'
70
+ description: Text analysis for Ruby
71
+ email:
72
+ - soulnafein@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - CODE_OF_CONDUCT.md
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - analtex.gemspec
84
+ - bin/console
85
+ - bin/setup
86
+ - document_frequency_calculator.rb
87
+ - lib/analtex.rb
88
+ - lib/analtex/bag_of_words.rb
89
+ - lib/analtex/dictionary.rb
90
+ - lib/analtex/document_frequency_calculator.rb
91
+ - lib/analtex/version.rb
92
+ homepage: http://www.github.com/
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.4.5.1
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Text analysis for Ruby
116
+ test_files: []