From 35da1ebd6c9e5cf9cfe3181291b34ccca2712694 Mon Sep 17 00:00:00 2001 From: Jade Vance Date: Fri, 1 Apr 2016 10:19:24 -0700 Subject: [PATCH] completed homework for mood analysis with readme note answers included --- .gitignore | 1 + README.md | 55 ++++++++++++++++++++---------------- mood-analysis.rb | 72 ++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 99 insertions(+), 29 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..496ee2c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.DS_Store \ No newline at end of file diff --git a/README.md b/README.md index 61958d6..2e5825d 100644 --- a/README.md +++ b/README.md @@ -9,40 +9,42 @@ Explain what is happening on each of the following lines in the code. | Line # | What's happening? |:------:|------------------- -| 1 | -| 2 | -| 3 | -| 6 | -| 7-8 | -| 9 | -| 10 | -| 11 | -| 12 | -| 13 | -| 14 | -| 17-19 | +| 1 | FEELINGS constant is initialized with a hash +| 2 | Hash key happy containing an array of happy moods +| 3 | Hash key sad containing an array of sad moods +| 6 | Method analyze_moods takes in the parameter words +| 7-8 | happy and sad variables are instantiated with integers +| 9 | words are sanitized with downcase +| 10 | words get split into an array of individual words +| 11 | if array contains a word that matches the happy feelings hash +| 12 | local variable happy gets incremented +| 13 | if array contains a word that matches the sad feelings hash +| 14 | local variable sad gets incremented +| 17-19 | happy face is returned if happiness is greater than sadness, sad face is returned if sadness is greater than happiness, meh face is returned if they are equal ### Data Types What's the Data Type of the following? | Code | Data Type |----------------------------|----------- -| FEELINGS | -| :sad | -| happy | -| words | -| words.split(" ") | -| FEELINGS[:sad] | -| FEELINGS[:happy].include? | -| analyze_mood(text) | +| FEELINGS | constant containing hash +| :sad | symbol +| happy | fixnum +| words | string +| words.split(" ") | array of string +| FEELINGS[:sad] | array +| FEELINGS[:happy].include? | boolean, true or false +| analyze_mood(text) | string ### Explaining the Code | Question | Answer |------------------------|------- -| Why do we need line 9? | -| What is the relationship between `words` and `word` (line 10)? | -| Why doesn't line 19 have an associated if/condition? | +| Why do we need line 9? | sanitize input to match hash of feelings +| What is the relationship between `words` and `word` (line 10)? | word is each element in the words array +| Why doesn't line 19 have an associated if/condition? | the other two conditions will not execute if the feelings are equal | What is the relationship between `text[0]`, `text[1]`, and `words`? | +words gets the values of text[0] on the first run through the method +words gets the values of text[1] on the second run through the method ### Assignment: Requirements 1. Replace lines 31 and 32 and write a loop to print out each day and the emoticon that is associated by analyzing the mood of that day. @@ -54,7 +56,8 @@ Your result will look like: ... ``` -**think**: Why does 03/13 come out as _sad_ when it should be _happy_? How could we fix this? +**think**: Why does 03/13 come out as _neutral_ when it should be _happy_? How could we fix this? +The exclamation in front of ! yay is causing yay to not be read by the loop and the same problem is happening with 05/01 as well. 2. To make the results a little more accurate, let's write and utilize a method called `strip_punctuation` to strip out the punctuation that affects the results. Namely, remove exclamation marks (!), periods (.), commas (,), and hashtags (#). @@ -68,6 +71,7 @@ After writing this method, our new result should be: ``` **think**: Where should we call `strip_punctuation`? Does it matter? Why? +before we iterate and analyze the data. It does matter otherwise you will get bad data 3. Write a method called `happy_days` to determine how many logged entries it takes until there have been three :-) happy days. @@ -77,6 +81,7 @@ It takes 5 entries for 3 happy days to occur ``` **think**: What are you going to do if there aren't at least 3 happy days? Where do you need to handle that case? +At the end of the loop, maybe return something about there only being x number of happy days for the total number of days 4. Write a method called `overall_mood` to determine the most common mood across all logged entries. @@ -86,5 +91,7 @@ The most common mood is :-) ``` **think**: Should you use an array or a hash to solve this problem? Why? +For counting until happy days, an Array because order matters +For overall mood, a Hash because order does not matter. But I also already have an array to work with so I just recycled the data I already had because honestly it's past midnight and my brain is only working partially. **think**: What if we eventually want to add feelings to our analysis? Can we write this code in a way that will prevent us from having to re-write it later? diff --git a/mood-analysis.rb b/mood-analysis.rb index e49df43..01028d0 100644 --- a/mood-analysis.rb +++ b/mood-analysis.rb @@ -1,15 +1,16 @@ FEELINGS = { - happy: %w(yay, good, great), - sad: %w(terrible, awful, horrible) + happy: %w(yay good great), + sad: %w(terrible awful horrible) } def analyze_mood(words) happy = 0 sad = 0 - words.downcase! + words = get_sanitized_input(words) words.split(" ").each do |word| if FEELINGS[:happy].include? word happy += 1 + elsif FEELINGS[:sad].include? word sad += 1 end @@ -19,6 +20,55 @@ def analyze_mood(words) return ":-|" end +def get_sanitized_input(words) + # input now matches feelings hash + words.downcase! + # did not write a strip_punctuation method but this line strips it + words.gsub!(/[^a-z0-9\s]/i, '') + return words +end + +def get_happy_days(emoji_array) + happy = 0 + smile = ":-)" + + number_of_days = 1 + emoji_array.each do |emoji| + if emoji == smile + happy += 1 + if happy >= 3 && happy < 4 + puts "It has taken #{number_of_days} days for #{happy} happy days to occur" + end + end + number_of_days += 1 + end + if happy > 3 + puts "There have only been #{happy} happy days out of a total of #{number_of_days-1} days" + end +end + +def get_overall_mood(emoji_array) + overall_mood = 0 + smile = ":-)" + frown = ":-(" + meh = ":-|" + + emoji_array.each do |emoji| + if emoji == smile + overall_mood += 1 + else emoji == frown + overall_mood -= 1 + end + end + if overall_mood > 0 + puts "overall mood is #{smile}" + elsif overall_mood < 0 + puts "overall mood is #{frown}" + else + puts "overall mood is #{meh}" + end +end + text = [ "03/01 I'm having a terrible horrible no good day.", "03/13 Yesterday was horrible, but today is great! Yay!", @@ -28,5 +78,17 @@ def analyze_mood(words) "05/11 Yay, yay, yay! I'm having a awfuly great day." ] -puts analyze_mood(text[0]) -puts analyze_mood(text[1]) +emoji_array = [] + +text.each do |day| + date = day.split(" ").first + emoji_array << analyze_mood(day) + puts date + analyze_mood(day) +end + +get_happy_days(emoji_array) +get_overall_mood(emoji_array) + + + +