Created
December 14, 2013 23:58
-
-
Save jmoon90/7966703 to your computer and use it in GitHub Desktop.
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
| class TddWordAnalytics | |
| attr_reader :sentence | |
| def initialize(sentence) | |
| @sentence = sentence | |
| end | |
| def analytics | |
| analytic = [] | |
| word = word_count.sort { |a,b| b[1]<=>a[1] }[0..1] | |
| word = word.map do |word| | |
| word[0] | |
| end | |
| letter = letter_count.sort { |a,b| b[1]<=>a[1] }[0..2] | |
| letter = letter.map do |letter| | |
| letter[0] | |
| end | |
| analytic << word | |
| analytic << letter | |
| analytic | |
| end | |
| def word_count | |
| counter = Hash.new(0) | |
| sentence.split(' ').each do |word| | |
| counter[word] += 1 | |
| end | |
| counter | |
| end | |
| def letter_count | |
| counter = Hash.new(0) | |
| sentence.split('').each do |letter| | |
| if letter.match(/[a-zA-Z]/) != nil | |
| letter = letter.downcase | |
| counter[letter] += 1 | |
| end | |
| end | |
| counter | |
| end | |
| def symbol_count | |
| counter = Hash.new(0) | |
| sentence.split('').each do |letter| | |
| if letter.match(/[^A-Za-z0-9_\s\t\r\n\f]/) | |
| counter[letter] += 1 | |
| end | |
| end | |
| counter | |
| end | |
| end |
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
| require './tdd_word_analytics' | |
| describe TddWordAnalytics do | |
| describe '#word' do | |
| it 'counts the number for each word' do | |
| expect(TddWordAnalytics.new('Hello').word_count).to eql({"Hello"=>1}) | |
| end | |
| it 'counts parses words in a sentence and give it a counter' do | |
| expect(TddWordAnalytics.new('This is my script').word_count).to eql({'This'=>1, 'is'=>1, 'my'=>1, 'script'=>1}) | |
| end | |
| end | |
| describe '#letter' do | |
| it 'counts how many times a given letter is used regardless of capitalization' do | |
| expect(TddWordAnalytics.new('Hello').letter_count).to include({'h'=>1, 'e'=>1, 'l'=>2, 'o'=>1}) | |
| end | |
| it 'only reads in letters' do | |
| expect(TddWordAnalytics.new('5 days').letter_count).to include({'d'=>1, 'a'=>1, 'y'=>1, 's'=>1}) | |
| end | |
| end | |
| describe '#non_letter' do | |
| it 'counts how many times any non_letter symbol is used excluding white space' do | |
| expect(TddWordAnalytics.new("5# y'ea").symbol_count).to include({'#'=>1, "'"=>1}) | |
| end | |
| end | |
| describe '#analytics' do | |
| tdd = TddWordAnalytics.new("What is up up") | |
| it 'outputs the most common word and letter' do | |
| expect(tdd.analytics).to match_array([['up','is'],['p','u','i']]) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment