-
Notifications
You must be signed in to change notification settings - Fork 23
Jade Vance Mood Analysis Homework #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| .DS_Store |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, what I should have asked is "Should we call strip_punctuation inside or outside analyze_mood?" Why?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would say inside because the mood cannot be properly analyzed unless the punctuation is stripped from it. |
||
|
|
||
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ? You didn't use an array for this (nor did you need to) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, nm. I see. Hmm, well this works. But the array is unnecessary. |
||
| 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. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Zzzz. Sleepy. You were correct in thinking a Hash was the answer, though your reasoning is incorrect. It doesn't have to do with the ordering (or lack of). It has to do with the fact that we want to count the occurrences of an indeterminate number of things, which is often solved utilizing the key/value pairings of a hash. (This is common in whiteboarding problems too) |
||
|
|
||
| **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? | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is just a verbose way of saying There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suspect you did this because it was printing the following line multiple times. Once for the first time it found 3 happy days and then for every day after that. You could eliminate that by fixing the condition as you did, but then the loop continues to iterate when we don't need it to. Instead you could have had a |
||
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works, but I'd rather this method return a single fixnum opposed to the string with the fixnum in it. That leaves us more flexibility in using that information in a different way instead of being stuck to this specific use case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method was supposed to return something, not puts anything. |
||
|
|
||
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hrm. This logic is flawed. I believe this doesn't work if the :-| is the highest mood. It actually only gives you meh if smile and frown are the same number, regardless of how many meh's there are. This method needs complete overhaul. |
||
|
|
||
| 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) | ||
|
|
||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍