S
Steven Demonnin
I have been working my way through a ruby book (Beginning Ruby) and I
want to extend on an interesting capability dealing with hashes.
the code:
text=''
line_count=0
File.open("txt.txt").each do |line|
line_count +=1
text << line
end
puts "#{line_count} lines"
total_charachters=text.length
puts "#{total_charachters} charachters"
sentence_count=text.split(/\.|\?|!/).length
total_characters_no_spaces=text.gsub(/\s+/,"").length
puts "#{total_characters_no_spaces} without spaces"
word_count=text.split.length
puts "#{word_count} words in the text and #{sentence_count} sentences"
paragraph_count= text.split(/\n\n/).length
puts "#{paragraph_count} paragraphs"
puts "#{sentence_count/paragraph_count} sentences per paragraph on
avarage"
puts "#{word_count/sentence_count} words per sentence"
stop_words= %w{a the by on for of are with just but and to the my has
some in}
words=text.scan(/\w+/)
keywords=words.select{|word| !stop_words.include?(word)}
puts "#{((keywords.length.to_f/words.length.to_f)*100).to_i}% non stop
words"
this has been a fun code, and I have been running various text files
through it.
What I want to know is, is it possible to create a has where the key is
the word, and the value is the number of occurrences of the word in the
text, and then sort the hash by the values?
want to extend on an interesting capability dealing with hashes.
the code:
text=''
line_count=0
File.open("txt.txt").each do |line|
line_count +=1
text << line
end
puts "#{line_count} lines"
total_charachters=text.length
puts "#{total_charachters} charachters"
sentence_count=text.split(/\.|\?|!/).length
total_characters_no_spaces=text.gsub(/\s+/,"").length
puts "#{total_characters_no_spaces} without spaces"
word_count=text.split.length
puts "#{word_count} words in the text and #{sentence_count} sentences"
paragraph_count= text.split(/\n\n/).length
puts "#{paragraph_count} paragraphs"
puts "#{sentence_count/paragraph_count} sentences per paragraph on
avarage"
puts "#{word_count/sentence_count} words per sentence"
stop_words= %w{a the by on for of are with just but and to the my has
some in}
words=text.scan(/\w+/)
keywords=words.select{|word| !stop_words.include?(word)}
puts "#{((keywords.length.to_f/words.length.to_f)*100).to_i}% non stop
words"
this has been a fun code, and I have been running various text files
through it.
What I want to know is, is it possible to create a has where the key is
the word, and the value is the number of occurrences of the word in the
text, and then sort the hash by the values?