Created
January 20, 2019 18:56
-
-
Save vikas95prasad/7606b1830841d46dedd5fc5e7e928f54 to your computer and use it in GitHub Desktop.
Get word count of a file in ruby
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
file = File.open("SampleTextFile_100kb.txt", "r") | |
words_print = {} | |
file.each_line do |line| | |
words = line.split | |
words.each do |word| | |
puts "previous word => #{word}" | |
word = word.downcase.gsub(/[,()'".]+\z/,'') | |
puts "afterwardsword" | |
if words_print[word] | |
words_print[word] += 1 | |
else | |
words_print[word] = 1 | |
end | |
end | |
end | |
puts words_print |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment