bable 0.1.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 80a52778b8d0bc8b38193d5052416259fdaf8754
4
+ data.tar.gz: 44382d772d69ea548c17ba7542a95d13dd260c4e
5
+ SHA512:
6
+ metadata.gz: db49fa8b22439844d4b9edc74c88c27784c6d59afe54d82f7790293a5d64c2418f961f115d4d38a4c045e97d9267d3c28a14268ce97c7a8ebf886737a6a58546
7
+ data.tar.gz: 2df068c45f1d074eeb483a047eefd9d374158c6f29012ad3fdef2a3a3d25201f4832cc9b85e22958a08ae2b6c8e209493a41b1dba5b6fe51e13efae3bab49a0b
@@ -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,10 @@
1
+ AllCops:
2
+ RunRailsCops: false
3
+ DisplayStyleGuide: true
4
+ DisplayCopNames: true
5
+
6
+ Style/Documentation:
7
+ Enabled: false
8
+
9
+ Style/StringLiterals:
10
+ EnforcedStyle: double_quotes
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2
4
+ - 2.1
5
+ - 2.0.0
6
+ before_install: gem install bundler -v 1.10.6
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in bable.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Daniel Madrid
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,73 @@
1
+ [![Build Status](https://travis-ci.org/danimashu/bable.svg?branch=master)](https://travis-ci.org/danimashu/bable)
2
+
3
+ # Bable: Text readability indexes calculator
4
+
5
+ Bable is a library that will allow you calculate quantitative readability indexes of a given text. Read more about readability tests here: [](https://en.wikipedia.org/wiki/Readability_test).
6
+
7
+ Besides calculating indexes (currently available: `Automated Readability Index` and `Coleman-Liau index`), it will help you build your own index calculator, giving you the structure and the helper methods for making the process smoother.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application’s Gemfile:
12
+
13
+ ```ruby
14
+ gem "bable"
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install bable
24
+
25
+ ## Usage
26
+
27
+ ```ruby
28
+ require "bable"
29
+
30
+ # List of available indexes
31
+ Bable::Index.avalable_indexes # => [:ari, :coleman_liau]
32
+
33
+ # Instantiate the default index
34
+ Bable.index("text") # => #<Bable::Index::ColemanLiau @text="text">
35
+
36
+ # Instantiate a concrete index
37
+ Bable.index("text", index: :ari) # => #<Bable::Index::Ari @text="text">
38
+
39
+ # Calculate a readability index
40
+ index = Bable.index("text")
41
+ index.calc # => 7.72
42
+ ```
43
+
44
+ ## Implement your own index
45
+
46
+ If you want to implement your own index you just need to create a subclass of `Bable::Index::Base` and define a method `#calc` in it, where you'll calculate the actual formula. Note that instances of `Bable::Index::Base` will come with a `#text` attribute reader, which returns a decorator of `String`, the class `StatisticString` (see [lib/bable/statistic_string.rb](lib/bable/statistic_string.rb) ). You can take advantage of all those methods for implement your index.
47
+
48
+ ```ruby
49
+ module Bable
50
+ module Index
51
+ class MyNewIndex < Base
52
+ def calc
53
+ # Define your formula here.
54
+ end
55
+ end
56
+ end
57
+ end
58
+
59
+ # Then use it
60
+ Bable.index("text", index: :my_new_index).calc
61
+ ```
62
+
63
+ ## Contributing
64
+
65
+ 1. Fork the repo ( http://github.com/danimashu/bable/fork ).
66
+ 2. Create your feature branch `git checkout -b my-new-feature`.
67
+ 3. Commit your changes `git commit -am "Add some feature"` referencing a GitHub issue.
68
+ 4. Push to the branch `git push origin my-new-feature`.
69
+ 5. Create a new Pull Request.
70
+
71
+ ## License
72
+
73
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -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,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "bable/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "bable"
8
+ s.version = Bable::VERSION
9
+ s.authors = ["Daniel Madrid"]
10
+ s.email = ["hello@dmadridreina.com"]
11
+ s.summary = "Text readability indexes calculator."
12
+ s.homepage = "https://github.com/danimashu/bable"
13
+ s.license = "MIT"
14
+
15
+ s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^spec/}) }
16
+ s.bindir = "exe"
17
+ s.executables = s.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_runtime_dependency "activesupport", "~> 4.2.4"
21
+
22
+ s.add_development_dependency "bundler", "~> 1.10"
23
+ s.add_development_dependency "pry", "~> 0.10"
24
+ s.add_development_dependency "rake", "~> 10.0"
25
+ s.add_development_dependency "rspec", "~> 3.3"
26
+ s.add_development_dependency "rubocop", "~> 0.34"
27
+ s.add_development_dependency "yard", "~> 0.8"
28
+ end
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "bable"
5
+
6
+ require "pry"
7
+ Pry.start
@@ -0,0 +1,5 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
@@ -0,0 +1,21 @@
1
+ require "bable/version"
2
+ require "bable/index"
3
+ require "bable/index/base"
4
+ Dir.glob(File.join(File.dirname(__FILE__), "bable/index/*.rb")).each do |f|
5
+ require f
6
+ end
7
+ require "active_support/core_ext/string/inflections"
8
+
9
+ module Bable
10
+ class << self
11
+ # Instantiate a readability index calculator.
12
+ #
13
+ # @param text [String] the text to analyse.
14
+ # @param index [Symbol] the index to use (in underscore format).
15
+ # @return [Babel::Index::Base] the instantiated subclass of
16
+ # `Babel::Index::Base` according to the index.
17
+ def index(text, index: :coleman_liau)
18
+ Index.target_class(index).new(text)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,25 @@
1
+ module Bable
2
+ module Index
3
+ class << self
4
+ # List the implemented readability indexes.
5
+ #
6
+ # @return [Array<Symbol>] list of indexes in underscored format.
7
+ def available_indexes
8
+ indexes = constants.select do |constant|
9
+ c = const_get(constant)
10
+ c.is_a?(Class) && c < Base
11
+ end
12
+ indexes.map { |c| c.to_s.underscore.to_sym }
13
+ end
14
+
15
+ def target_class(index)
16
+ camelized_class = index.to_s.camelize
17
+
18
+ fail NotExistingIndexError unless const_defined?(camelized_class)
19
+ const_get(camelized_class)
20
+ end
21
+ end
22
+
23
+ class NotExistingIndexError < StandardError; end
24
+ end
25
+ end
@@ -0,0 +1,13 @@
1
+ module Bable
2
+ module Index
3
+ # This class is responsible of calculating the Automated Readability Index.
4
+ # Check https://en.wikipedia.org/wiki/Automated_readability_index for
5
+ # knowing more.
6
+ class Ari < Base
7
+ def calc
8
+ (4.71 * text.characters_count / text.words_count +
9
+ 0.5 * text.words_count / text.sentences_count - 21.43).round(2)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,23 @@
1
+ require "bable/statistic_string"
2
+
3
+ module Bable
4
+ module Index
5
+ class Base
6
+ attr_reader :text
7
+
8
+ def initialize(text)
9
+ @text = StatisticString.new(text)
10
+ end
11
+
12
+ # The calculation of the readability index. This method should be
13
+ # implemented in each index class (subclass of Base). The returning value
14
+ # could potentially means different things, depending on the index. Read
15
+ # the documentation of each index for knowing more.
16
+ #
17
+ # @return [Float] the resulting calculation of the index.
18
+ def calc
19
+ fail NotImplementedError
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,22 @@
1
+ module Bable
2
+ module Index
3
+ # This class is responsible of calculating the Coleman-Liau Index. Check
4
+ # https://en.wikipedia.org/wiki/Coleman%E2%80%93Liau_index for knowing more.
5
+ class ColemanLiau < Base
6
+ def calc
7
+ (0.0588 * average_chars_per_100_words -
8
+ 0.296 * average_sentences_per_100_words - 15.8).round(2)
9
+ end
10
+
11
+ private
12
+
13
+ def average_chars_per_100_words
14
+ text.average_word_length * 100
15
+ end
16
+
17
+ def average_sentences_per_100_words
18
+ text.average_word_length * 100 / text.average_sentence_length
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,34 @@
1
+ module Bable
2
+ class StatisticString < SimpleDelegator
3
+ WORD_REGEX = /\w+/
4
+ SENTENCE_REGEX = /[^\s.?!:][^.?!:]*[.?!:]+/
5
+
6
+ def words
7
+ scan(WORD_REGEX)
8
+ end
9
+
10
+ def sentences
11
+ scan(SENTENCE_REGEX)
12
+ end
13
+
14
+ def characters_count
15
+ words.join.size
16
+ end
17
+
18
+ def words_count
19
+ words.size
20
+ end
21
+
22
+ def sentences_count
23
+ sentences.size
24
+ end
25
+
26
+ def average_word_length
27
+ characters_count / words_count.to_f
28
+ end
29
+
30
+ def average_sentence_length
31
+ characters_count / sentences_count.to_f
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,3 @@
1
+ module Bable
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,161 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Madrid
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.2.4
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.2.4
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.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.34'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '0.34'
97
+ - !ruby/object:Gem::Dependency
98
+ name: yard
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '0.8'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '0.8'
111
+ description:
112
+ email:
113
+ - hello@dmadridreina.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".rubocop.yml"
121
+ - ".travis.yml"
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - bable.gemspec
127
+ - bin/console
128
+ - bin/setup
129
+ - lib/bable.rb
130
+ - lib/bable/index.rb
131
+ - lib/bable/index/ari.rb
132
+ - lib/bable/index/base.rb
133
+ - lib/bable/index/coleman_liau.rb
134
+ - lib/bable/statistic_string.rb
135
+ - lib/bable/version.rb
136
+ homepage: https://github.com/danimashu/bable
137
+ licenses:
138
+ - MIT
139
+ metadata: {}
140
+ post_install_message:
141
+ rdoc_options: []
142
+ require_paths:
143
+ - lib
144
+ required_ruby_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ required_rubygems_version: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - ">="
152
+ - !ruby/object:Gem::Version
153
+ version: '0'
154
+ requirements: []
155
+ rubyforge_project:
156
+ rubygems_version: 2.4.5
157
+ signing_key:
158
+ specification_version: 4
159
+ summary: Text readability indexes calculator.
160
+ test_files: []
161
+ has_rdoc: