epitome 0.2.0
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 +9 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +67 -0
- data/Rakefile +9 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/epitome.gemspec +27 -0
- data/lib/epitome.rb +7 -0
- data/lib/epitome/corpus.rb +145 -0
- data/lib/epitome/document.rb +13 -0
- data/lib/epitome/version.rb +3 -0
- metadata +128 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 00f6a83c548383971f75d417ed9f5a65c1a88656
|
4
|
+
data.tar.gz: 405fce9771c575a89975d9729e02f8b52bb28580
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2a4053ac8b0c4faf4ebab5240e4ae88f5094e22fb3d3255d552a30f4301566d0219ecee97ddaac5fdff2c9afadcdbe50c039502c367db64dab4666003b58508e
|
7
|
+
data.tar.gz: 8f7e8a17eb7ac524f36e25c6caf73abce6ee4cef9d16e5ff3977fc6bb01ab79fdb6aa4ec52dd962f6332a65ad6828d60b9cdcd66917c89fb1aa0a43afe41bc61
|
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) 2015 McFreely
|
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,67 @@
|
|
1
|
+
# Epitome
|
2
|
+
|
3
|
+
A small gem to make your text shorter. It's an implementation of the Lexrank algorithm. You can use it on a single text, but lexrank is designed to be used on a collection of texts. But it works the same anyway.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'epitome'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install hemingway
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Firstly, you need to create some documents.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
document_one = Epitome::Document.new("The cat likes catnip. He rolls and rolls")
|
27
|
+
document_two = Epitome::Document.new("The cat plays in front of the dog. The dog is placid.")
|
28
|
+
```
|
29
|
+
|
30
|
+
Then, organize your documents in a corpus
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
document_collection = [document_one, document_two]
|
34
|
+
@corpus = Epitome::Corpus.new(document_collection)
|
35
|
+
```
|
36
|
+
|
37
|
+
Finally, output the summary
|
38
|
+
```ruby
|
39
|
+
@corpus.summary(length=3)
|
40
|
+
```
|
41
|
+
|
42
|
+
This returns a nice, short text.
|
43
|
+
|
44
|
+
## Options
|
45
|
+
### Summary options
|
46
|
+
You can pass options to set the length of the expected summary, and set the similarity threshold
|
47
|
+
```ruby
|
48
|
+
@corpus.summary(5, 0.2)
|
49
|
+
```
|
50
|
+
The length is the number of sentences of the final output.
|
51
|
+
|
52
|
+
The threshold is a value between 0.1 and 0.3, but 0.2 is considered to give the best results (and thus the default value).
|
53
|
+
|
54
|
+
### Stopword option
|
55
|
+
When creating the corpus, you can set the language of the stopword list to be used
|
56
|
+
```ruby
|
57
|
+
@corpus = Epitome::Corpus.new(document_collection, "fr")
|
58
|
+
```
|
59
|
+
The default value is english "en".
|
60
|
+
You can find more about the stopword filter [here](https://github.com/brenes/stopwords-filter).
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
1. Fork it ( https://github.com/[my-github-username]/hemingway/fork )
|
64
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
65
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
66
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
67
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "epitome"
|
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/epitome.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 'epitome/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "epitome"
|
8
|
+
spec.version = Epitome::VERSION
|
9
|
+
spec.authors = ["McFreely"]
|
10
|
+
spec.email = ["paulmcfreely@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Epitome makes your texts shorter.}
|
13
|
+
spec.description = %q{An implementation of the Lexrank Algorithm, which summarize corpus of text documents.}
|
14
|
+
spec.homepage = "https://github.com/McFreely/epitome"
|
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.9"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "minitest"
|
25
|
+
spec.add_dependency "scalpel"
|
26
|
+
spec.add_dependency "stopwords-filter"
|
27
|
+
end
|
data/lib/epitome.rb
ADDED
@@ -0,0 +1,145 @@
|
|
1
|
+
require 'matrix'
|
2
|
+
require 'stopwords'
|
3
|
+
|
4
|
+
module Epitome
|
5
|
+
class Corpus
|
6
|
+
attr_reader :original_corpus
|
7
|
+
def initialize(document_collection, lang="en")
|
8
|
+
# lang is the language used to initialize the stopword list
|
9
|
+
@lang = lang
|
10
|
+
|
11
|
+
# Massage the document_collection into a more workable form
|
12
|
+
@original_corpus = {}
|
13
|
+
document_collection.each { |document| @original_corpus[document.id] = document.text }
|
14
|
+
@clean_corpus = {}
|
15
|
+
@original_corpus.each do |key, value|
|
16
|
+
@clean_corpus[key] = clean value
|
17
|
+
end
|
18
|
+
|
19
|
+
# Dictionary of term-frequency for each word
|
20
|
+
# to avoid unnecessary computations
|
21
|
+
@word_tf_doc = {}
|
22
|
+
|
23
|
+
# Just the sentences
|
24
|
+
@sentences = @original_corpus.values.flatten
|
25
|
+
|
26
|
+
# The number of documents in the corpus
|
27
|
+
@n_docs = @original_corpus.keys.size
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
def summary(summary_length, threshold=0.2)
|
32
|
+
s = @clean_corpus.values.flatten
|
33
|
+
# n is the number of sentences in the total corpus
|
34
|
+
n = @clean_corpus.values.flatten.size
|
35
|
+
|
36
|
+
# Vector of Similarity Degree for each sentence in the corpus
|
37
|
+
degree = Array.new(n) {0.00}
|
38
|
+
|
39
|
+
# Square matrix of dimension n = number of sentences
|
40
|
+
cosine_matrix = Matrix.build(n) do |i, j|
|
41
|
+
if idf_modified_cosine(s[i], s[j]) > threshold
|
42
|
+
degree[i] += 1.0
|
43
|
+
1.0
|
44
|
+
else
|
45
|
+
0.0
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
# Similarity Matrix
|
50
|
+
similarity_matrix = Matrix.build(n) do |i,j|
|
51
|
+
degree[i] == 0 ? 0.0 : ( cosine_matrix[i,j] / degree[i] )
|
52
|
+
end
|
53
|
+
|
54
|
+
# Random walk ala PageRank
|
55
|
+
# in the form of a power method
|
56
|
+
results = power_method similarity_matrix, n, 0.85
|
57
|
+
|
58
|
+
# Ugly sleight of hand to return a text based on results
|
59
|
+
# <Array>Results => <Hash>Results => <String>ResultsText
|
60
|
+
h = Hash[@sentences.zip(results)]
|
61
|
+
return h.sort_by {|k, v| v}.reverse.first(summary_length).to_h.keys
|
62
|
+
end
|
63
|
+
|
64
|
+
private
|
65
|
+
def clean(sentence_array)
|
66
|
+
# Clean the sentences a bit to avoid unnecessary operations
|
67
|
+
#
|
68
|
+
# Create stopword filter
|
69
|
+
filter = Stopwords::Snowball::Filter.new @lang
|
70
|
+
sentence_array.map do |s|
|
71
|
+
s = s.downcase
|
72
|
+
filter.filter(s.split).join(" ")
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def n_docs_including_w(word)
|
77
|
+
# Count the number of documents in the corpus containing the word
|
78
|
+
# Look for the word in the dictionnary first, calculate if not present
|
79
|
+
@word_tf_doc.fetch(word) do |w|
|
80
|
+
count = 0
|
81
|
+
docs = []
|
82
|
+
|
83
|
+
# Concanate the each document sentences to make it easier to search
|
84
|
+
@clean_corpus.values.each { |sentences| docs << sentences.join(" ") }
|
85
|
+
|
86
|
+
# Here, we user an interpolated string instead of a regex to avoid
|
87
|
+
# weird corner cases
|
88
|
+
docs.each { |s| count += 1 if s.include? "#{word}" }
|
89
|
+
|
90
|
+
@word_tf_doc[w] = count
|
91
|
+
count
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
def idf(word)
|
96
|
+
# Number of documents in which word appears
|
97
|
+
# Inverse Frequency Smooth (as per wikipedia article)
|
98
|
+
Math.log( @n_docs / n_docs_including_w(word) )
|
99
|
+
end
|
100
|
+
|
101
|
+
def tf(sentence, word)
|
102
|
+
# Number of occurences of word in sentence
|
103
|
+
sentence.scan(word).count
|
104
|
+
end
|
105
|
+
|
106
|
+
def sentence_tfidf_sum(sentence)
|
107
|
+
# The Sum of tfidf values for each of the words in a sentence
|
108
|
+
sentence.split(" ")
|
109
|
+
.map { |word| (tf(sentence, word)**2) * idf(word) }
|
110
|
+
.inject(:+)
|
111
|
+
end
|
112
|
+
|
113
|
+
def idf_modified_cosine(x, y)
|
114
|
+
# Compute the similarity between two sentences x, y
|
115
|
+
# using the modified cosine tfidf formula
|
116
|
+
numerator = (x + " " + y).split(" ")
|
117
|
+
.map { |word| tf(x, word) * tf(y, word) * (idf(word)**2) }
|
118
|
+
.inject(:+)
|
119
|
+
|
120
|
+
denominator = Math.sqrt(sentence_tfidf_sum(x)) * Math.sqrt(sentence_tfidf_sum(y))
|
121
|
+
numerator / denominator
|
122
|
+
end
|
123
|
+
|
124
|
+
def power_method(matrix, n, e)
|
125
|
+
# Accept a stochastic, irreducible & aperiodic matrix M
|
126
|
+
# Accept a matrix size n, an error tolerance e
|
127
|
+
# Output Eigenvector p
|
128
|
+
|
129
|
+
# init values
|
130
|
+
t = 0
|
131
|
+
p = Vector.elements(Array.new(n) { (1.0 / n) * 1} )
|
132
|
+
sigma = 1
|
133
|
+
|
134
|
+
until sigma < e
|
135
|
+
t += 1
|
136
|
+
prev_p = p.clone
|
137
|
+
p = matrix.transpose * prev_p
|
138
|
+
sigma = (p - prev_p).norm
|
139
|
+
end
|
140
|
+
|
141
|
+
p.to_a
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
metadata
ADDED
@@ -0,0 +1,128 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: epitome
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- McFreely
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-04-04 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.9'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.9'
|
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: minitest
|
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
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: scalpel
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: stopwords-filter
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: An implementation of the Lexrank Algorithm, which summarize corpus of
|
84
|
+
text documents.
|
85
|
+
email:
|
86
|
+
- paulmcfreely@gmail.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".travis.yml"
|
93
|
+
- Gemfile
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/console
|
98
|
+
- bin/setup
|
99
|
+
- epitome.gemspec
|
100
|
+
- lib/epitome.rb
|
101
|
+
- lib/epitome/corpus.rb
|
102
|
+
- lib/epitome/document.rb
|
103
|
+
- lib/epitome/version.rb
|
104
|
+
homepage: https://github.com/McFreely/epitome
|
105
|
+
licenses:
|
106
|
+
- MIT
|
107
|
+
metadata: {}
|
108
|
+
post_install_message:
|
109
|
+
rdoc_options: []
|
110
|
+
require_paths:
|
111
|
+
- lib
|
112
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - ">="
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0'
|
117
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
requirements: []
|
123
|
+
rubyforge_project:
|
124
|
+
rubygems_version: 2.4.5
|
125
|
+
signing_key:
|
126
|
+
specification_version: 4
|
127
|
+
summary: Epitome makes your texts shorter.
|
128
|
+
test_files: []
|