Last active
December 31, 2015 09:19
-
-
Save sdanko11/7965886 to your computer and use it in GitHub Desktop.
Word Analytics
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_relative "top_three" | |
| require 'pry' | |
| require 'rspec' | |
| class WordAnalytics | |
| attr_reader :character_count | |
| def initialize(phrase) | |
| @phrase = phrase | |
| @letters = [] | |
| @total_counts = [] | |
| end | |
| def start_word_count | |
| words = @phrase.split(' ') | |
| get_unique_character_or_words(words) | |
| end | |
| def start_letter_count | |
| letters = @phrase.delete(' ') | |
| array_of_letters = letters.split('') | |
| @letters = array_of_letters | |
| get_unique_character_or_words(@letters) | |
| end | |
| def get_unique_character_or_words(words_or_letters) | |
| all_characters = [] | |
| uniq_characters = words_or_letters.uniq | |
| uniq_characters.each do |character| | |
| uniq_characters = {character => 0} | |
| all_characters << uniq_characters | |
| end | |
| count_characters_or_words(all_characters, words_or_letters) | |
| end | |
| def count_characters_or_words(character_or_word_count, all_letters) | |
| all_letters.each do |each_letter| | |
| character_or_word_count.each do |letter| | |
| if each_letter == letter.keys[0] | |
| letter[each_letter] += 1 | |
| end | |
| end | |
| end | |
| @character_count = character_or_word_count | |
| 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 'rspec' | |
| require_relative 'analytics' | |
| describe WordAnalytics do | |
| let(:phrase1) { WordAnalytics.new("my my my my") } | |
| let(:phrase2) { WordAnalytics.new("this is 22 22 44 44 33 ") } | |
| let(:phrase3) { WordAnalytics.new("?? ??? asfdas 222 $$ ///") } | |
| let(:phrase4) { WordAnalytics.new("----------") } | |
| let(:phrase5) { WordAnalytics.new("/////--00000") } | |
| it 'it should calculate the amount of times each word is used in a phrase' do | |
| expect(phrase1.start_word_count).to eql([{'my' => 4}]) | |
| end | |
| it 'it should calculate the amount of times each word is used in a phrase' do | |
| expect(phrase2.start_word_count).to eql([{'this' => 1}, {'is' => 1},{'22' => 2}, {'44' => 2}, {'33' => 1}]) | |
| end | |
| it "should calculate the number of times each character is used" do | |
| expect(phrase1.start_letter_count).to eql([{'m' => 4}, {'y' => 4}]) | |
| end | |
| it "should calculate the number of times each character is used" do | |
| expect(phrase3.start_letter_count).to eql([{'?' => 5}, {"a"=>2}, {'s'=>2}, {'f'=>1}, | |
| {'d'=>1}, {'2'=> 3}, {'$'=> 2}, {'/'=>3}]) | |
| end | |
| it "should calculate the number of times each character is used" do | |
| expect(phrase4.start_letter_count).to eql([{'-' => 10}]) | |
| end | |
| it "should calculate the number of times each character is used" do | |
| expect(phrase5.start_letter_count).to eql([{'/'=> 5}, {'-' => 2}, {'0' => 5}]) | |
| 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
| class TopThree | |
| def initialize(counts) | |
| @counts = counts.character_count | |
| end | |
| def get_top_3_used_characters_or_words | |
| @order_counts = @counts.sort_by { |hsh| hsh.values } | |
| until @order_counts.count == 3 | |
| @order_counts.shift | |
| @order_counts.count | |
| end | |
| @order_counts | |
| 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 'rspec' | |
| require_relative 'analytics' | |
| require_relative 'top_three' | |
| describe TopThree do | |
| word = WordAnalytics.new("word word word word word word yes yes yes yes no yes no ha steve") | |
| word.start_word_count | |
| word3 = WordAnalytics.new("tttttttsssssiiiivqqqq444") | |
| word3.start_letter_count | |
| characters = WordAnalytics.new(';;;;;---2222..?') | |
| characters.start_letter_count | |
| let(:word1) { TopThree.new(word) } | |
| let(:word2) { TopThree.new(word3) } | |
| let(:word5) { TopThree.new(characters) } | |
| it 'it should calculate the top 3 words used words in a phrase' do | |
| expect(word1.get_top_3_used_characters_or_words).to eql([{'no'=>2}, {'yes'=> 5}, {'word' => 6}]) | |
| end | |
| it 'it should calculate the top 3 used characters in a phrase' do | |
| expect(word2.get_top_3_used_characters_or_words).to eql([{"q"=>4}, {"s"=>5}, {"t"=>7}]) | |
| end | |
| it 'it should calculate the top 3 used characters in a phrase' do | |
| expect(word5.get_top_3_used_characters_or_words).to eql([{'-'=> 3}, {'2' => 4},{';'=>5}]) | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment