zipfy 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.travis.yml +5 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +42 -0
- data/Rakefile +12 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/bin/zipfy +62 -0
- data/lib/zipfy.rb +109 -0
- data/lib/zipfy/version.rb +3 -0
- data/lib/zipfy/word_data.rb +13 -0
- data/zipfy.gemspec +27 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0da3f0c6e6311a52b646c2e0e4bb3e1e9a6558f3
|
4
|
+
data.tar.gz: 99d62a3cacf7666e7b0328ee81e38d70bf88f8b4
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 97a2b41f8e28e30ebdd616ee42eff3bd684d7b013ceefd556a69a725f42b88903513ed6616ca2c72cc359d8a6afe43b382bec44553661a87303330913a1e82e2
|
7
|
+
data.tar.gz: bd0cd82f14544ba82f80b81555bce751109b6779f7c47116f108ae61eb7722b2408c933d404a1c12b554021d5844f86b0cb3e9121f0be696ee0b3e6de9b59c58
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 bjubes
|
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,42 @@
|
|
1
|
+
# Zipfy
|
2
|
+
|
3
|
+
Zipfy calculates word distribution in text files or strings and their deviation from Zipf's law"
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'zipfy'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install zipfy
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Instanciate an instance of the class `Zipf` and use its instance methods, or just run Zipfy via the command line
|
24
|
+
`zipfy file_to_analyze.txt -v`
|
25
|
+
|
26
|
+
use `zipfy --help` for all possible arguments.
|
27
|
+
|
28
|
+
## Development
|
29
|
+
|
30
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
31
|
+
|
32
|
+
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).
|
33
|
+
|
34
|
+
## Contributing
|
35
|
+
|
36
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/bjubes/zipfy. Please ensure all pull requests are compatible with the license.
|
37
|
+
|
38
|
+
|
39
|
+
## License
|
40
|
+
|
41
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
42
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "zipfy"
|
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
|
data/bin/setup
ADDED
data/bin/zipfy
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
#using http://robdodson.me/how-to-write-a-command-line-ruby-gem/
|
4
|
+
require 'getoptlong'
|
5
|
+
require 'zipfy'
|
6
|
+
include Zipfy
|
7
|
+
|
8
|
+
verbose = false
|
9
|
+
save_path = nil
|
10
|
+
overwrite_save = false
|
11
|
+
|
12
|
+
opts = GetoptLong.new(
|
13
|
+
[ '--help', '-h', GetoptLong::NO_ARGUMENT ],
|
14
|
+
[ '--save', '-s', GetoptLong::REQUIRED_ARGUMENT ],
|
15
|
+
[ '--verbose', '-v', GetoptLong::NO_ARGUMENT ]
|
16
|
+
)
|
17
|
+
|
18
|
+
file_path = ARGV[0]
|
19
|
+
if !file_path
|
20
|
+
puts "Please specify a file to evaluate. Run zipf -h for more information"
|
21
|
+
exit
|
22
|
+
end
|
23
|
+
|
24
|
+
opts.each do |opt, arg|
|
25
|
+
case opt
|
26
|
+
when '--help'
|
27
|
+
puts <<-EOF
|
28
|
+
Usage: zipf [FILE]
|
29
|
+
|
30
|
+
FILE:
|
31
|
+
the file the analyze
|
32
|
+
|
33
|
+
-h, --help:
|
34
|
+
show help
|
35
|
+
|
36
|
+
-v, --verbose:
|
37
|
+
show table of calculations
|
38
|
+
|
39
|
+
-s, --save:
|
40
|
+
save output of verbose to a file.
|
41
|
+
|
42
|
+
-f --force:
|
43
|
+
overwrite save file if one already exists
|
44
|
+
EOF
|
45
|
+
exit
|
46
|
+
when '--verbose'
|
47
|
+
verbose = true
|
48
|
+
when '--save'
|
49
|
+
save_path = arg
|
50
|
+
when '--force'
|
51
|
+
overwrite_save = true
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
z = Zipf.new
|
56
|
+
z.create_distribution( z.load_file file_path)
|
57
|
+
z.distribution
|
58
|
+
z.sort_distribution
|
59
|
+
z.calculate_zipfness
|
60
|
+
z.puts_distribution if verbose
|
61
|
+
z.save_dist_to_file save_path, overwrite_save if save_path
|
62
|
+
puts z.calculate_std_dev_from_reg
|
data/lib/zipfy.rb
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'pry'
|
2
|
+
# a program for seeing the zipfian distribution of a text.
|
3
|
+
require 'putsplus'
|
4
|
+
include Putsplus
|
5
|
+
require_relative 'zipfy/word_data.rb'
|
6
|
+
|
7
|
+
module Zipfy
|
8
|
+
class Zipf
|
9
|
+
TAB = "\t"
|
10
|
+
attr_reader :distribution
|
11
|
+
attr_reader :total
|
12
|
+
attr_reader :zipf_constant
|
13
|
+
|
14
|
+
#Parses a file into an array of words
|
15
|
+
def load_file file_path
|
16
|
+
words = []
|
17
|
+
File.open(file_path, 'r') do |f|
|
18
|
+
words = f.read.to_s.split(/[\s,-]/)
|
19
|
+
end
|
20
|
+
words.each do |word|
|
21
|
+
word.gsub!(/\W+/, '') #remove non characters
|
22
|
+
word.downcase!
|
23
|
+
end
|
24
|
+
words.delete("") #remove blank entries
|
25
|
+
words
|
26
|
+
end
|
27
|
+
|
28
|
+
#
|
29
|
+
# sets instance var distribution to the distirubtion of the words set passed
|
30
|
+
#
|
31
|
+
def create_distribution words
|
32
|
+
temp_hash = Hash.new(0)
|
33
|
+
@distribution = []
|
34
|
+
@total = 0
|
35
|
+
@zipf_constant = nil
|
36
|
+
|
37
|
+
words.each do |word|
|
38
|
+
temp_hash[word] += 1
|
39
|
+
@total += 1
|
40
|
+
end
|
41
|
+
temp_hash.keys.each do |k|
|
42
|
+
@distribution << WordData.new(k, temp_hash[k])
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def sort_distribution
|
47
|
+
@distribution = @distribution.sort_by {|wd| wd.frequency.to_i}
|
48
|
+
end
|
49
|
+
|
50
|
+
def calculate_zipfness
|
51
|
+
calculate_zipf_constant unless @zipf_constant
|
52
|
+
|
53
|
+
#zipf number is equal to freq * zipf_const/total
|
54
|
+
# so call zipf_const/total 'z'
|
55
|
+
z = @zipf_constant/@total.to_f
|
56
|
+
length = @distribution.length
|
57
|
+
|
58
|
+
@distribution.map do |d|
|
59
|
+
d.zip_number = z * d.frequency
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def puts_distribution
|
64
|
+
underline " "*6 + "Word Distirbution" + " "*6
|
65
|
+
length = @distribution.length
|
66
|
+
@distribution.each_with_index do |wd, i|
|
67
|
+
tabs = 4 - (wd.word.length / 8).floor
|
68
|
+
puts wd.word + (TAB * tabs).to_s + wd.frequency.to_s + TAB + (length - i).to_s + TAB + wd.zip_number.to_s
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def save_dist_to_file file_path, overwrite_file = false
|
73
|
+
if File.exists?(file_path) && overwrite_file
|
74
|
+
puts "Requires --force to overwrite existing file '#{file_path}'"
|
75
|
+
exit
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def calculate_zipf_constant
|
80
|
+
#this word has rank =1 , so its zipf factor should be 1/n = 1/1 = 1
|
81
|
+
# so find its percentage and then multiply it up to one.
|
82
|
+
|
83
|
+
#make the following true
|
84
|
+
#freq of most common word / total words = 1
|
85
|
+
#by adding a constant to the right side
|
86
|
+
# where constant = total words/ freq of most common word
|
87
|
+
|
88
|
+
@zipf_constant = @total.to_f / @distribution.last.frequency.to_f
|
89
|
+
@zipf_constant
|
90
|
+
end
|
91
|
+
|
92
|
+
#must calculate zipfness first
|
93
|
+
def calculate_std_dev_from_reg
|
94
|
+
#the zipfness should be equal to 1/rank. lets see the average deviatoin form that number
|
95
|
+
length = @distribution.length
|
96
|
+
deviations = []
|
97
|
+
|
98
|
+
@distribution.each_with_index do |wd,i|
|
99
|
+
theoretical = 1/(length - i).to_f
|
100
|
+
actual = wd.zip_number
|
101
|
+
deviation = (theoretical - actual).abs
|
102
|
+
deviations << deviation
|
103
|
+
end
|
104
|
+
sum = 0.0
|
105
|
+
deviations.each {|d| sum += d}
|
106
|
+
(sum/length.to_f)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
data/zipfy.gemspec
ADDED
@@ -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 'zipfy/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "zipfy"
|
8
|
+
spec.version = Zipfy::VERSION
|
9
|
+
spec.authors = ["bjubes"]
|
10
|
+
spec.email = ["brian2386@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = "Zipfy calculates word distribution in text files or strings and their deviation from Zipf's law"
|
13
|
+
spec.homepage = "https://github.com/bjubes/ziphy"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "bin"
|
20
|
+
spec.executables = ["zipfy"]
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_runtime_dependency 'putsplus'
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: zipfy
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- bjubes
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: putsplus
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.13'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.13'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.0'
|
69
|
+
description:
|
70
|
+
email:
|
71
|
+
- brian2386@gmail.com
|
72
|
+
executables:
|
73
|
+
- zipfy
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- bin/zipfy
|
86
|
+
- lib/zipfy.rb
|
87
|
+
- lib/zipfy/version.rb
|
88
|
+
- lib/zipfy/word_data.rb
|
89
|
+
- zipfy.gemspec
|
90
|
+
homepage: https://github.com/bjubes/ziphy
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata: {}
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 2.5.2
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: Zipfy calculates word distribution in text files or strings and their deviation
|
114
|
+
from Zipf's law
|
115
|
+
test_files: []
|