-
Notifications
You must be signed in to change notification settings - Fork 0
/
CountWords.rb
47 lines (38 loc) · 1.19 KB
/
CountWords.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# This script counts the words in each text file and the total word count
# in given directories.
LibDir = File.dirname(__FILE__) + '/lib/'
require File.expand_path(LibDir + 'Helpers')
include Helpers
def get_cmd_line_args()
if ARGV.length < 1
puts "\nArguments: <directory1> [<directory2> ...]"
exit
end
get_dirs_from_cmd_line_args(ARGV)
end
# Main
dirs = get_cmd_line_args
summary_word_counts = []
dirs.each do |directory|
puts "Counting words in directory " + directory
total_word_count = 0
Dir["#{directory}/*.txt"].each do |file_name|
position = read_file(file_name)
word = ""
file_word_count = 0
while not word.nil?
word, punctuation, position = get_next_word(position, word, punctuation)
file_word_count += 1 unless word.nil?
end
puts "#{file_word_count} words in file #{file_name}"
total_word_count += file_word_count
end
puts "#{total_word_count} words in directory #{directory}"
summary_word_counts.push [total_word_count, directory]
end
if (summary_word_counts.length > 1)
puts "\nSummary of word counts:"
summary_word_counts.each do |word_count, directory|
puts "#{word_count} words in directory #{directory}"
end
end