frekwenza 0.0.1
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/.gitignore +2 -0
- data/LICENSE.txt +22 -0
- data/README.md +56 -0
- data/frekwenza.gemspec +19 -0
- data/lib/frekwenza/stop_words.rb +23 -0
- data/lib/frekwenza/tf_idf.rb +56 -0
- data/lib/frekwenza/version.rb +3 -0
- data/lib/frekwenza.rb +3 -0
- metadata +54 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: fe93d2259013d43219aac982560c795d7e698a03
|
|
4
|
+
data.tar.gz: 833373b3981ad562cb81ea3372d6f447c0a9d2bc
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: aaef936370715683e87b369fc7f578cc9160be2a694fdcf7cd61b2d8a0f6eed1232adc4b933de5d1c7d68c91674ac792b8a80fde8c45941500041d971e0f969e
|
|
7
|
+
data.tar.gz: e29aa5916853ac534800acd05e64261155b682f420b20e483e64ddc45bc4e6372571adaa19a8850fd768def67746bf4860f5fca44e038c75f62076ebe23c1c61
|
data/.gitignore
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2013 mathieuripert
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
# Frekwenza
|
|
2
|
+
|
|
3
|
+
This gem is based on [Mathieu Ripert](https://github.com/mathieuripert)'s work, [ruby-tf-idf](https://github.com/mathieuripert/ruby-tf-idf). Some changes are made in the gem, aside from the project structure.
|
|
4
|
+
|
|
5
|
+
## Usage
|
|
6
|
+
|
|
7
|
+
Mostly similar with ruby-tf-idf, here's an example right from ruby-tf-idf's readme only with the "RubyTfIdf" part replaced with "Frekwenza".
|
|
8
|
+
|
|
9
|
+
```
|
|
10
|
+
require 'frekwenza'
|
|
11
|
+
|
|
12
|
+
corpus = [
|
|
13
|
+
'A big enough hammer can usually fix anything',
|
|
14
|
+
'A bird in the hand is a big mistake .',
|
|
15
|
+
'A bird in the hand is better than one overhead!',
|
|
16
|
+
'A career is a job that takes about 20 more hours a week.',
|
|
17
|
+
'A clean desk is a sign of a cluttered desk drawer.',
|
|
18
|
+
'A cynic smells flowers and looks for the casket.'
|
|
19
|
+
]
|
|
20
|
+
|
|
21
|
+
limit = 3 #restrict to the top 3 relevant words per document
|
|
22
|
+
|
|
23
|
+
t = Frekwenza::TfIdf.new corpus, limit
|
|
24
|
+
t.tf_idf
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
The main difference lies in the stop words. Frekwenza doesn't include hard coded stop words as ruby-tf-idf. To use stop words in Frekwenza, add the path to the file containing the stop words as the third parameter.
|
|
28
|
+
|
|
29
|
+
```
|
|
30
|
+
require 'frekwenza'
|
|
31
|
+
|
|
32
|
+
corpus = [
|
|
33
|
+
'A big enough hammer can usually fix anything',
|
|
34
|
+
'A bird in the hand is a big mistake .',
|
|
35
|
+
'A bird in the hand is better than one overhead!',
|
|
36
|
+
'A career is a job that takes about 20 more hours a week.',
|
|
37
|
+
'A clean desk is a sign of a cluttered desk drawer.',
|
|
38
|
+
'A cynic smells flowers and looks for the casket.'
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
limit = 3 #restrict to the top 3 relevant words per document
|
|
42
|
+
stop_words = "stop_words.txt"
|
|
43
|
+
|
|
44
|
+
t = Frekwenza::TfIdf.new corpus, limit, stop_words
|
|
45
|
+
t.tf_idf
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
The following is an example output of Frekwenza (the format is exactly the same as ruby-tf-idf's).
|
|
49
|
+
|
|
50
|
+
```
|
|
51
|
+
[ {"anything"=>0.7781512503836436, "fix"=>0.7781512503836436, "enough"=>0.7781512503836436}, {"mistake"=>0.7781512503836436, "bird"=>0.47712125471966244, "in"=>0.47712125471966244}, {"overhead!"=>0.7781512503836436, "better"=>0.7781512503836436, "one"=>0.7781512503836436}, {"week"=>0.7781512503836436, "career"=>0.7781512503836436, "hours"=>0.7781512503836436}, {"desk"=>1.5563025007672873, "drawer"=>0.7781512503836436, "clean"=>0.7781512503836436}, {"casket"=>0.7781512503836436, "cynic"=>0.7781512503836436, "smells"=>0.7781512503836436}, ... ]
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
## Special Thanks
|
|
55
|
+
|
|
56
|
+
[Mathieu Ripert](https://github.com/mathieuripert), author or [ruby-tf-idf](https://github.com/mathieuripert/ruby-tf-idf) gem.
|
data/frekwenza.gemspec
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'frekwenza/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |gem|
|
|
7
|
+
gem.name = "frekwenza"
|
|
8
|
+
gem.version = Frekwenza::VERSION
|
|
9
|
+
gem.authors = ["Mathieu Ripert", "Edwin Tunggawan"]
|
|
10
|
+
gem.email = ["mathieu.ripert@gmail.com", "vcc.edwint@gmail.com"]
|
|
11
|
+
gem.description = %q{Term Frequency - Inverse Document Frequency }
|
|
12
|
+
gem.summary = %q{Calculating TF-IDF, based on Mathieu Ripert's ruby-tf-idf gem}
|
|
13
|
+
gem.homepage = "https://github.com/sdsdkkk/frekwenza"
|
|
14
|
+
|
|
15
|
+
gem.files = `git ls-files`.split($/)
|
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
|
18
|
+
gem.require_paths = ["lib", "lib/frekwenza"]
|
|
19
|
+
end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
module Frekwenza
|
|
2
|
+
class StopWords
|
|
3
|
+
attr_reader :stop_words
|
|
4
|
+
|
|
5
|
+
def initialize(file)
|
|
6
|
+
string = read(file)
|
|
7
|
+
build_list(string)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
private
|
|
11
|
+
def read(file)
|
|
12
|
+
string = ""
|
|
13
|
+
File.open(file, "r") do |f|
|
|
14
|
+
string = f.read
|
|
15
|
+
end
|
|
16
|
+
string
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def build_list(string)
|
|
20
|
+
@stop_words = string.downcase.gsub(/[^a-z0-9]/, ' ').split(' ').uniq
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
module Frekwenza
|
|
2
|
+
class TfIdf
|
|
3
|
+
attr_reader :tf, :idf, :tf_idf
|
|
4
|
+
|
|
5
|
+
def initialize(docs, limit, stop_words_file=nil)
|
|
6
|
+
@docs = split_docs(docs)
|
|
7
|
+
@tf = []
|
|
8
|
+
@idf = {}
|
|
9
|
+
@tf_idf = []
|
|
10
|
+
@docs_size = @docs.size
|
|
11
|
+
calculate_tf_and_idf
|
|
12
|
+
calculate_tf_idf(limit, stop_words_file)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
private
|
|
16
|
+
def split_docs(docs)
|
|
17
|
+
words = []
|
|
18
|
+
docs.each do |d|
|
|
19
|
+
words << d.downcase.gsub(/[^a-z0-9]/, ' ').split(' ')
|
|
20
|
+
end
|
|
21
|
+
words
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def calculate_tf_and_idf
|
|
25
|
+
@docs.each do |words|
|
|
26
|
+
terms_freq = words.inject(Hash.new(0)){|h, e| h[e]+=1; h}
|
|
27
|
+
@tf.push(terms_freq)
|
|
28
|
+
distinct_words = words.uniq
|
|
29
|
+
distinct_words.each do |w|
|
|
30
|
+
if @idf.has_key?(w)
|
|
31
|
+
y = @docs_size / ( 10**(@idf[w]) )
|
|
32
|
+
y += 1
|
|
33
|
+
@idf[w] = Math.log10(@docs_size / y)
|
|
34
|
+
else
|
|
35
|
+
@idf[w] = Math.log10(@docs_size)
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def calculate_tf_idf(limit, stop_words_file)
|
|
42
|
+
@tf.each do |tf_freq|
|
|
43
|
+
tfidf = Hash.new(0)
|
|
44
|
+
tf_freq.each do |k, v|
|
|
45
|
+
tfidf[k] = @idf[k] * v
|
|
46
|
+
end
|
|
47
|
+
if stop_words_file
|
|
48
|
+
sw = StopWords.new(stop_words_file)
|
|
49
|
+
tfidf.reject!{|k| sw.stop_words.include?(k)}
|
|
50
|
+
end
|
|
51
|
+
tfidf = Hash[tfidf.sort_by{|k, v| -v}[0..limit-1]]
|
|
52
|
+
@tf_idf.push(tfidf)
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
data/lib/frekwenza.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: frekwenza
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Mathieu Ripert
|
|
8
|
+
- Edwin Tunggawan
|
|
9
|
+
autorequire:
|
|
10
|
+
bindir: bin
|
|
11
|
+
cert_chain: []
|
|
12
|
+
date: 2015-07-25 00:00:00.000000000 Z
|
|
13
|
+
dependencies: []
|
|
14
|
+
description: 'Term Frequency - Inverse Document Frequency '
|
|
15
|
+
email:
|
|
16
|
+
- mathieu.ripert@gmail.com
|
|
17
|
+
- vcc.edwint@gmail.com
|
|
18
|
+
executables: []
|
|
19
|
+
extensions: []
|
|
20
|
+
extra_rdoc_files: []
|
|
21
|
+
files:
|
|
22
|
+
- ".gitignore"
|
|
23
|
+
- LICENSE.txt
|
|
24
|
+
- README.md
|
|
25
|
+
- frekwenza.gemspec
|
|
26
|
+
- lib/frekwenza.rb
|
|
27
|
+
- lib/frekwenza/stop_words.rb
|
|
28
|
+
- lib/frekwenza/tf_idf.rb
|
|
29
|
+
- lib/frekwenza/version.rb
|
|
30
|
+
homepage: https://github.com/sdsdkkk/frekwenza
|
|
31
|
+
licenses: []
|
|
32
|
+
metadata: {}
|
|
33
|
+
post_install_message:
|
|
34
|
+
rdoc_options: []
|
|
35
|
+
require_paths:
|
|
36
|
+
- lib
|
|
37
|
+
- lib/frekwenza
|
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
39
|
+
requirements:
|
|
40
|
+
- - ">="
|
|
41
|
+
- !ruby/object:Gem::Version
|
|
42
|
+
version: '0'
|
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
requirements: []
|
|
49
|
+
rubyforge_project:
|
|
50
|
+
rubygems_version: 2.2.2
|
|
51
|
+
signing_key:
|
|
52
|
+
specification_version: 4
|
|
53
|
+
summary: Calculating TF-IDF, based on Mathieu Ripert's ruby-tf-idf gem
|
|
54
|
+
test_files: []
|