word_tally 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
+ SHA256:
3
+ metadata.gz: 1ad3839063c426520a6ca9adca2f1ff3121524788e6bbbf3bd7c88eeca8c09cb
4
+ data.tar.gz: 90b03da8789104dde63ea26723dbe0010b0ed7d7383cec04a581d40c65c0cab3
5
+ SHA512:
6
+ metadata.gz: eadd0010fc80a61c7a50174074f4b72d1a3df3de17ead595686c4baa7de81a612637f0a7919a63a347de6b9f6fc650822aed5baba7872c9b5ad7c316ed5fb467
7
+ data.tar.gz: c17d955c7c134b80cacc754d04b9b197fb546cb6c03188f635d2a8099c9c299f75345eed82a48a82f38ae0c4f91dcf84dd78ddd0606d03a3e6a230f92ec42b23
data/.rubocop.yml ADDED
@@ -0,0 +1,25 @@
1
+ require:
2
+ - rubocop-minitest
3
+ - rubocop-rake
4
+
5
+ AllCops:
6
+ TargetRubyVersion: 3.3
7
+ NewCops: enable
8
+
9
+ Layout/SpaceInsideHashLiteralBraces:
10
+ EnforcedStyle: no_space
11
+
12
+ Lint/AmbiguousOperatorPrecedence:
13
+ Enabled: false
14
+
15
+ Metrics/ParameterLists:
16
+ Max: 9
17
+
18
+ Style/Alias:
19
+ EnforcedStyle: prefer_alias
20
+
21
+ Style/Documentation:
22
+ Enabled: false
23
+
24
+ Style/NumberedParametersLimit:
25
+ Max: 9
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) Shannon Skipper
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,35 @@
1
+ # WordTally
2
+
3
+ TODO: Delete this and the text below, and describe your gem
4
+
5
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/word_tally`. To experiment with that code, run `bin/console` for an interactive prompt.
6
+
7
+ ## Installation
8
+
9
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ 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.
26
+
27
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/word_tally.
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/bin/word-tally ADDED
@@ -0,0 +1,89 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'optparse'
5
+ require 'word_tally'
6
+ require 'word_tally/args'
7
+
8
+ options = ARGV.options do |cli|
9
+ cli.banner = <<~BANNER
10
+ Output a tally of the number of times unique words appear in source input.
11
+
12
+ \u001b[4mUsage:\u001b[0m word-tally [OPTIONS] [INPUT]
13
+
14
+ \u001b[4mArguments:\u001b[0m
15
+ [INPUT] File path to use as input rather than stdin ("-") [default: -]
16
+
17
+ \u001b[4mOptions:\u001b[0m
18
+ BANNER
19
+ cli.version = WordTally::VERSION
20
+ cli.accept(Symbol, &:to_sym)
21
+
22
+ cli.on '-s', '--sort=ORDER', 'Sort order [default: desc] [possible values: desc, asc, unsorted]', Symbol
23
+ cli.on '-c', '--case=FORMAT', 'Case normalization [default: lower] [possible values: original, upper, lower]', Symbol
24
+ cli.on '-m', '--min_chars=COUNT', 'Exclude words that contain fewer than min chars [default: 1]', Integer
25
+ cli.on '-M', '--min_count=COUNT', 'Exclude words that appear fewer than min times [default: 1]', Integer
26
+ cli.on '-D', '--delimiter=VALUE', 'Delimiter between keys and values [default: " "]', String
27
+ cli.on '-o', '--output=PATH', 'Write output to file rather than stdout', String
28
+ cli.on '-v', '--verbose', 'Print verbose details'
29
+ cli.on '-d', '--debug', 'Print debugging information'
30
+ cli.on '-h', '--help', 'Print help' do
31
+ puts cli
32
+ exit
33
+ end
34
+ cli.on '-V', '--version', 'Print version' do
35
+ puts cli.ver
36
+ end
37
+ end
38
+
39
+ args = WordTally::Args.new
40
+ options.parse!(into: args)
41
+
42
+ input = if ARGV.empty?
43
+ $stdin
44
+ else
45
+ File.open(ARGV.shift)
46
+ end
47
+
48
+ word_tally = WordTally.new(
49
+ input,
50
+ format: args.case,
51
+ order: args.sort,
52
+ min_chars: args.min_chars,
53
+ min_count: args.min_count
54
+ )
55
+
56
+ delimiter = args.delimiter
57
+
58
+ if args.verbose
59
+ warn "total-words#{delimiter}#{word_tally.count}"
60
+ warn "uniq-words#{delimiter}#{word_tally.uniq_count}"
61
+ end
62
+
63
+ if args.debug
64
+ warn "delimiter#{delimiter}#{delimiter}"
65
+ warn "case#{delimiter}#{args.case}"
66
+ warn "order#{delimiter}#{args.sort}"
67
+ warn "min-chars#{delimiter}#{args.min_chars}"
68
+ warn "min-count#{delimiter}#{args.min_count}"
69
+ warn "verbose#{delimiter}#{args.verbose}"
70
+ warn "debug#{delimiter}#{args.debug}"
71
+ end
72
+
73
+ tally = word_tally.tally
74
+
75
+ warn("\n") if (args.verbose || args.debug) && tally.any?
76
+
77
+ output = args.output
78
+
79
+ if output
80
+ File.open output, 'w' do |file|
81
+ tally.each do |word, count|
82
+ file << "#{word}#{delimiter}#{count}\n"
83
+ end
84
+ end
85
+ else
86
+ tally.each do |word, count|
87
+ puts "#{word}#{delimiter}#{count}"
88
+ end
89
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ class WordTally
4
+ class Args
5
+ attr_accessor :sort, :case, :min_chars, :min_count, :delimiter, :output, :verbose, :debug
6
+
7
+ def initialize(sort: :desc, word_case: :lower, min_chars: 1, min_count: 1, delimiter: ' ', output: nil,
8
+ verbose: false, debug: false)
9
+ @sort = sort
10
+ @case = word_case
11
+ @min_chars = min_chars
12
+ @min_count = min_count
13
+ @delimiter = delimiter
14
+ @output = output
15
+ @verbose = verbose
16
+ @debug = debug
17
+ end
18
+
19
+ def []=(attribute, value)
20
+ instance_variable_set("@#{attribute}", value)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class WordTally
4
+ VERSION = '0.1.0'
5
+ end
data/lib/word_tally.rb ADDED
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'word_tally/version'
4
+
5
+ class WordTally
6
+ attr_reader :tally, :count, :uniq_count, :avg
7
+
8
+ def initialize(input, format: :lower, order: :desc, min_chars: 1, min_count: 1)
9
+ tally_map = tally_map(input, format, min_chars)
10
+ tally_map.filter! { |_word, count| count >= min_count } if min_count > 1
11
+ @count = tally_map.values.sum
12
+ @tally = tally_map.to_a
13
+ @uniq_count = @tally.size
14
+ @avg = @count.positive? && @count.fdiv(@uniq_count)
15
+
16
+ sort(order)
17
+ end
18
+
19
+ def sort(order)
20
+ case order
21
+ in :desc
22
+ @tally.sort_by! { |_word, count| -count }
23
+ in :asc
24
+ @tally.sort_by! { |_word, count| count }
25
+ in :unsorted
26
+ nil
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def tally_map(input, format, min_chars)
33
+ tally = Hash.new(0)
34
+
35
+ input.each_line(chomp: true) do |line|
36
+ words = normalize(line, format).split(/\b/).grep(/[[:alnum:]]/)
37
+ words.filter! { |word| word.size >= min_chars } if min_chars > 1
38
+
39
+ words.each { |word| tally[word] += 1 }
40
+ end
41
+
42
+ tally
43
+ end
44
+
45
+ def normalize(text, format)
46
+ case format
47
+ in :lower
48
+ text.downcase
49
+ in :upper
50
+ text.upcase
51
+ in :original
52
+ text
53
+ end
54
+ end
55
+ end
metadata ADDED
@@ -0,0 +1,50 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: word_tally
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Shannon Skipper
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 2024-06-06 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: Output a tally of the number of times unique words appear in file or
13
+ stdin source.
14
+ email:
15
+ - shannonskipper@gmail.com
16
+ executables:
17
+ - word-tally
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".rubocop.yml"
22
+ - LICENSE.txt
23
+ - README.md
24
+ - bin/word-tally
25
+ - lib/word_tally.rb
26
+ - lib/word_tally/args.rb
27
+ - lib/word_tally/version.rb
28
+ homepage: https://github.com/havenwood/word_tally
29
+ licenses:
30
+ - MIT
31
+ metadata:
32
+ rubygems_mfa_required: 'true'
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 3.3.0
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubygems_version: 3.6.0.dev
48
+ specification_version: 4
49
+ summary: Tallies the count of words from a file or streamed input.
50
+ test_files: []