flesch_kincaid 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ad4fa6c2dfc9314c73443857132beea60178be73
4
+ data.tar.gz: 2893d25edbb290acba77003343b8c49b2d9e12f0
5
+ SHA512:
6
+ metadata.gz: 128762881c47d201f076d846039052044eb0f3bd7e2e67f8106a9634dc2311a03b344497774ac7436fa88cf063be6b173a64e1ad4d67f897cd7e55b826f3d76e
7
+ data.tar.gz: 5fd13ef7fc1fee7656837fa95e858eb6ec3abb355b38650fc10458a3fe58f2ff8cafdc2384d978908b5e5cca4cc03ae536a478193b855141b30945389cb97728
data/.gitignore ADDED
@@ -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
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+ # Specify your gem's dependencies in flesch_kincaid.gemspec
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Bodacious
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,62 @@
1
+ # FleschKincaid
2
+
3
+ Simple, super-lightweight gem for calculating the Flesch Kincaid score for a given block of text.
4
+
5
+ ## Vs Odyssey
6
+
7
+ We needed to calculate F.K. scores in Ruby.
8
+
9
+ There's a similar gem named [Odyssey](https://github.com/cameronsutter/odyssey), which provides some additional formulae too. However, that codebase is a bit larger and slower.
10
+
11
+ [flesch_kincaid](https://github.com/KatanaCode/flesch_kincaid) is more than 2x faster (see the [benchmark](benchmarks/odyssey.rb))
12
+
13
+ If you just need standard Flesch Kincaid scores, this gem should do the trick.
14
+
15
+ Warming up --------------------------------------
16
+ Odyssey 31.000 i/100ms
17
+ Flesch Kincaid 75.000 i/100ms
18
+ Calculating -------------------------------------
19
+ Odyssey 323.004 (± 4.0%) i/s - 1.643k in 5.095737s
20
+ Flesch Kincaid 764.062 (± 3.3%) i/s - 3.825k in 5.012054s
21
+
22
+
23
+ ## Installation
24
+
25
+ Add this line to your application's Gemfile:
26
+
27
+ ```ruby
28
+ gem 'flesch_kincaid'
29
+ ```
30
+
31
+ And then execute:
32
+
33
+ $ bundle
34
+
35
+ Or install it yourself as:
36
+
37
+ $ gem install flesch_kincaid
38
+
39
+ ## Usage
40
+
41
+ ``` ruby
42
+ @result = FleschKincaid.read("I can't explain myself, I'm afraid, sir' said Alice, 'because I'm not myself, you see.")
43
+ @result.score # => 75.77
44
+ @result.grade # => "7th grade"
45
+ @result.notes # => "Fairly easy to read."
46
+ ```
47
+
48
+ ## Development
49
+
50
+ 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.
51
+
52
+ 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).
53
+
54
+ ## Contributing
55
+
56
+ Bug reports and pull requests are welcome on GitHub at https://github.com/katanacode/flesch_kincaid.
57
+
58
+
59
+ ## License
60
+
61
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
62
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,36 @@
1
+ $LOAD_PATH << File.join(File.dirname(__FILE__), "lib")
2
+
3
+ require "benchmark/ips"
4
+ require "odyssey"
5
+ require "flesch_kincaid"
6
+
7
+ TEXT = DATA.read
8
+
9
+ Benchmark.ips do |test|
10
+
11
+ test.report "Odyssey" do
12
+ Odyssey.flesch_kincaid_re(TEXT, false)
13
+ end
14
+
15
+ test.report "Flesch Kincaid" do
16
+ FleschKincaid.read(TEXT).score
17
+ end
18
+
19
+ end
20
+
21
+ __END__
22
+ Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam commodo lobortis justo eget interdum. Nullam lorem ex, efficitur nec ornare eu, fringilla ac nulla. Nulla facilisi. Donec accumsan accumsan sem, et dignissim dolor porta at. Nulla fringilla magna ac magna tincidunt mattis. Quisque aliquet, est nec tristique blandit, felis odio efficitur dui, in convallis massa orci ut nisl. Nullam hendrerit justo vel ante placerat, et imperdiet urna volutpat. Proin risus libero, accumsan ut tellus a, lacinia ultricies nisl. Fusce ultricies interdum diam, vitae tincidunt odio maximus id. Praesent in tortor aliquam mi pulvinar dignissim. Interdum et malesuada fames ac ante ipsum primis in faucibus.
23
+
24
+ Suspendisse et odio justo. Maecenas dolor ipsum, commodo et justo et, pulvinar hendrerit sapien. Donec dapibus lacus nec eros congue tincidunt. Vestibulum ut eleifend nibh. Curabitur vehicula dui mollis, tempor odio eget, dignissim lorem. In tempus, nisl sed feugiat luctus, enim dolor sollicitudin velit, nec cursus erat sapien non purus. Sed in lacus et lectus vestibulum fermentum. Integer et dignissim purus, sit amet fringilla ante. In finibus sed orci non rhoncus. Maecenas a tempus augue. Pellentesque vel justo id odio condimentum porta.
25
+
26
+ Morbi pulvinar dignissim lorem, a tincidunt sem tempor nec. Suspendisse ultrices sapien ut nisi vehicula varius. Aenean pharetra ligula et convallis blandit. Curabitur iaculis eget mauris et interdum. Aenean rhoncus eros sed placerat semper. Aenean a ex elementum, tincidunt risus sit amet, ultricies est. Ut aliquet urna a blandit aliquam. Quisque vel porttitor ex. Curabitur erat odio, dapibus ac tortor sit amet, auctor finibus augue. Suspendisse varius sit amet ligula sed ornare. Duis consectetur posuere pharetra. Praesent ornare ante blandit sem tincidunt fringilla. Interdum et malesuada fames ac ante ipsum primis in faucibus. Aliquam erat volutpat. Morbi auctor elit vel sollicitudin tincidunt. Duis auctor sit amet tellus iaculis maximus.
27
+
28
+ In finibus cursus lectus, ac elementum ante volutpat non. Duis rhoncus euismod purus vitae placerat. Donec vitae tempor velit, quis egestas purus. Mauris condimentum tellus vitae diam laoreet mattis. Nulla lobortis lectus sit amet neque volutpat, sed dapibus lacus tempus. Nullam ligula tellus, tempus auctor faucibus eu, efficitur in odio. Proin molestie fringilla turpis in rhoncus. Nam sollicitudin magna vulputate est luctus, ut porta libero facilisis. Mauris rhoncus nisl a arcu interdum, a pulvinar justo vestibulum. Nulla quis sodales ipsum. Donec nulla felis, hendrerit ac mauris lobortis, suscipit eleifend lectus. Suspendisse nisi mi, imperdiet nec iaculis eu, vestibulum at nisl.
29
+
30
+ Donec sit amet lectus quis justo faucibus sagittis. Proin eros mauris, sodales non arcu in, ullamcorper tristique lectus. Mauris dignissim tempus nunc, sed mattis ipsum tincidunt nec. Praesent tristique urna sem, euismod vulputate risus tempor quis. Donec nec lacinia eros. Nulla mauris felis, ultrices ac molestie et, vulputate id odio. Aliquam ac imperdiet libero, at faucibus ipsum. Nulla sollicitudin pharetra tempor. Donec turpis purus, maximus id molestie vitae, vulputate sit amet odio.
31
+
32
+ Morbi aliquam metus urna, ut porta nibh mattis ac. Pellentesque ut neque magna. Quisque pellentesque enim eu nunc commodo condimentum. Proin id posuere dui, vitae euismod sem. Suspendisse et metus ex. Ut vulputate pellentesque orci, in sagittis nisi laoreet eu. Donec ac arcu dui. Fusce suscipit imperdiet elementum.
33
+
34
+ Ut dapibus aliquet nibh a posuere. Vivamus accumsan sapien quis augue dictum vehicula. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Integer sit amet lacus massa. Duis pretium eleifend ante et vulputate. Praesent iaculis eu erat vel ornare. Donec luctus nisl ac purus consequat, nec sagittis mauris tincidunt.
35
+
36
+ Nulla molestie placerat nisi, nec sollicitudin elit venenatis eu. In malesuada, nunc sed feugiat vulputate, neque erat tincidunt sapien, quis convallis purus leo et arcu. Proin vel ligula id enim aliquet commodo. Aliquam semper justo sed finibus tempus. Praesent ullamcorper sit amet mi sed ullamcorper. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec ultrices purus nulla, vel ultrices risus aliquet eu. Nunc dolor sapien, faucibus quis quam sit amet, aliquam vehicula mi. Cras accumsan semper augue, id consectetur mauris elementum ac. Donec quis nulla non eros tempus bibendum. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "flesch_kincaid"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -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 'flesch_kincaid/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "flesch_kincaid"
8
+ spec.version = FleschKincaid::VERSION
9
+ spec.authors = ["Bodacious"]
10
+ spec.email = ["gavin@katanacode.com"]
11
+
12
+ spec.summary = %q{Lightweight gem for running Flesch Kincaid tests against text}
13
+ spec.description = %q{Flesch Kincaid allows you to parse blocks of text and assess their readability based on the Flesch Kincaid metric.}
14
+ spec.homepage = "https://github.com/katanacode/flesch_kincaid"
15
+ spec.license = "MIT"
16
+
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
19
+ f.match(%r{^(spec)/})
20
+ end
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "rubocop"
24
+ spec.add_development_dependency "bundler", "~> 1.14"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec"
27
+ end
@@ -0,0 +1,12 @@
1
+ require "flesch_kincaid/version"
2
+ require "flesch_kincaid/test"
3
+ require "flesch_kincaid/result"
4
+ module FleschKincaid
5
+
6
+ module_function
7
+
8
+ def read(text)
9
+ FleschKincaid::Test.new(text).result
10
+ end
11
+
12
+ end
@@ -0,0 +1,75 @@
1
+ module FleschKincaid
2
+
3
+ # Internal: A test result object, containing the score, grade, and notes.
4
+ #
5
+ # Examples
6
+ #
7
+ # @result = Result.new(80)
8
+ # @result.score # => 80.0
9
+ #
10
+ # @result = Result.new(80)
11
+ # @result.grade # => "6th grade"
12
+ #
13
+ # @result = Result.new(80)
14
+ # @result.notes # => "Easy to read. Conversational English for consumers."
15
+ class Result
16
+
17
+ ##
18
+ # The Flesh Kincaid formula result score.
19
+ #
20
+ # Returns {Float}
21
+ attr_reader :score
22
+
23
+
24
+ # Initialize a new Result
25
+ #
26
+ # score - The Flesh Kincaid formula result score.
27
+ #
28
+ def initialize(score)
29
+ @score = score.to_f
30
+ end
31
+
32
+ # Approx. reading age for score.
33
+ #
34
+ # Returns {String}
35
+ def grade
36
+ case score
37
+ when 90..100 then "5th grade"
38
+ when 80..90 then "6th grade"
39
+ when 70..80 then "7th grade"
40
+ when 60..70 then "8th & 9th grade"
41
+ when 50..60 then "10th to 12th grade"
42
+ when 30..50 then "College"
43
+ when 1..30 then "College Graduate"
44
+ else
45
+ "n/a"
46
+ end
47
+ end
48
+
49
+ # Additional notes to describe the score
50
+ #
51
+ # Returns {String}
52
+ def notes
53
+ case score
54
+ when 90..100
55
+ "Very easy to read. Easily understood by an average 11-year-old student."
56
+ when 80..90
57
+ "Easy to read. Conversational English for consumers."
58
+ when 70..80
59
+ "Fairly easy to read."
60
+ when 60..70
61
+ "Plain English. Easily understood by 13-15-year-old students."
62
+ when 50..60
63
+ "Fairly difficult to read."
64
+ when 30..50
65
+ "Difficult to read."
66
+ when 1..30
67
+ "Very difficult to read. Best understood by university graduates."
68
+ else
69
+ "No score available"
70
+ end
71
+ end
72
+
73
+ end
74
+
75
+ end
@@ -0,0 +1,84 @@
1
+ module FleschKincaid
2
+
3
+ ##
4
+ # Internal: Tests a block of text based on the Flesch-Kincaid readability test and
5
+ # returns a {Result}.
6
+ #
7
+ #
8
+ # Examples
9
+ #
10
+ # FleschKincaid::Test.new("A sentence that is long and hard to read").result
11
+ class Test
12
+
13
+ BASE = 206.835.freeze
14
+
15
+ SENTENCE_MULTIPLIER = 1.015.freeze
16
+
17
+ SYLLABLE_MULTIPLIER = 84.6.freeze
18
+
19
+ WORD_REGEX = /\w+[^\w]/.freeze
20
+
21
+ SENTENCE_REGEX = /[\w\s](\.|\!|\?)+/.freeze
22
+
23
+ COMPRESS_VOWEL_REGEX = /(?:[^laeiouy]es|ed|[^laeiouy]e)$/.freeze
24
+
25
+ Y_REGEX = /^y/.freeze
26
+
27
+ VOWEL_REGEX = /[aeiouy]{1,2}/.freeze
28
+
29
+
30
+ attr_reader :text
31
+
32
+
33
+ def initialize(text)
34
+ @text = text
35
+ end
36
+
37
+ def result
38
+ @result ||= Result.new(formula_output)
39
+ end
40
+
41
+ # The total number of words in {text}
42
+ #
43
+ # Returns {Integer}
44
+ def total_words
45
+ text.scan(WORD_REGEX).count
46
+ end
47
+
48
+ # The total number of sentences in {text}
49
+ #
50
+ # Returns {Integer}
51
+ def total_sentences
52
+ text.scan(SENTENCE_REGEX).count
53
+ end
54
+
55
+ # The total number of syllables in {text}
56
+ #
57
+ # Returns {Integer}
58
+ def total_syllables
59
+ return 1 if text.length <= 3
60
+ text
61
+ .downcase
62
+ .sub(COMPRESS_VOWEL_REGEX, '')
63
+ .sub(Y_REGEX, '')
64
+ .scan(VOWEL_REGEX).size
65
+ end
66
+
67
+
68
+ private
69
+
70
+
71
+ # Runs the Flesch Kincaid formula:
72
+ #
73
+ # 206.835 - 1.015(totalwords / totalsentences) - 84.6 (totalsyllables / total words)
74
+ #
75
+ # Returns {Float}
76
+ def formula_output
77
+ return 0.0 if total_words == 0 || total_sentences == 0
78
+ sentence_val = SENTENCE_MULTIPLIER * Rational(total_words, total_sentences)
79
+ syllable_val = SYLLABLE_MULTIPLIER * Rational(total_syllables, total_words)
80
+ (BASE - sentence_val - syllable_val).round(2)
81
+ end
82
+
83
+ end
84
+ end
@@ -0,0 +1,3 @@
1
+ module FleschKincaid
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: flesch_kincaid
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bodacious
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-04-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rubocop
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
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.14'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.14'
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: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Flesch Kincaid allows you to parse blocks of text and assess their readability
70
+ based on the Flesch Kincaid metric.
71
+ email:
72
+ - gavin@katanacode.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - benchmarks/odyssey.rb
84
+ - bin/console
85
+ - bin/setup
86
+ - flesch_kincaid.gemspec
87
+ - lib/flesch_kincaid.rb
88
+ - lib/flesch_kincaid/result.rb
89
+ - lib/flesch_kincaid/test.rb
90
+ - lib/flesch_kincaid/version.rb
91
+ homepage: https://github.com/katanacode/flesch_kincaid
92
+ licenses:
93
+ - MIT
94
+ metadata: {}
95
+ post_install_message:
96
+ rdoc_options: []
97
+ require_paths:
98
+ - lib
99
+ required_ruby_version: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: '0'
109
+ requirements: []
110
+ rubyforge_project:
111
+ rubygems_version: 2.6.8
112
+ signing_key:
113
+ specification_version: 4
114
+ summary: Lightweight gem for running Flesch Kincaid tests against text
115
+ test_files: []