-
Notifications
You must be signed in to change notification settings - Fork 23
mood-analysis hw #8
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 |
|---|---|---|
|
|
@@ -9,40 +9,40 @@ 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 |Declaring constant variable | ||
| | 2 |Declaring happy key-value pair | ||
| | 3 |Declaring sad key-value pair | ||
| | 6 |Declaring method | ||
| | 7-8 |Declaring variables | ||
| | 9 |Making words lowercase | ||
| | 10 |Making string into array of words and looping through each word | ||
| | 11 |Checking happy key in FEELINGS for the word | ||
| | 12 |Add 1 to happy if word is in happy value array | ||
| | 13 |Checking sad key in FEELINGS for the word | ||
| | 14 |Add 1 to sad if word is in sad value array | ||
| | 17-19 |Return appropriate smiley based on happiness/sadness values | ||
|
|
||
| ### 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 |Hash | ||
| | :sad |Symbol | ||
| | happy |Fixnum | ||
| | words |String | ||
| | words.split(" ") |Array | ||
| | FEELINGS[:sad] |Array | ||
| | FEELINGS[:happy].include? |Boolean | ||
| | 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? | | ||
| | What is the relationship between `text[0]`, `text[1]`, and `words`? | | ||
| | Why do we need line 9? |Lines 11 and 13 are case sensitive | ||
| | What is the relationship between `words` and `word` (line 10)? | words is a string and word is an element of an array | ||
| | Why doesn't line 19 have an associated if/condition? | It is essentially the else statement. | ||
| | What is the relationship between `text[0]`, `text[1]`, and `words`? | words is a parameter, text[0] and text[1] are arguments. | ||
|
|
||
| ### 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 +54,9 @@ 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? | ||
|
|
||
| Both happy and sad values are 0 because each happy and sad word has punctuation attached so the words aren't matching during the .include method. This could be fixed by looping each word through a method that strips punctuation. | ||
|
|
||
| 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 +70,7 @@ After writing this method, our new result should be: | |
| ``` | ||
|
|
||
| **think**: Where should we call `strip_punctuation`? Does it matter? Why? | ||
| We should call strip_punctuation before running the analyze_mood. It matters because analyze_mood cannot produce an accurate result until the punctuation is removed. | ||
|
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. 👍 But do you think you should call strip_punctuation inside or outside the analyze_mood method? Why? |
||
|
|
||
| 3. Write a method called `happy_days` to determine how many logged entries it takes until there have been three :-) happy days. | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,15 +1,20 @@ | ||
| FEELINGS = { | ||
| happy: %w(yay, good, great), | ||
| sad: %w(terrible, awful, horrible) | ||
| happy: %w(yay good great), #remove commas?? | ||
| sad: %w(terrible awful horrible) | ||
| } | ||
|
|
||
|
|
||
| def analyze_mood(words) | ||
| happy = 0 | ||
| sad = 0 | ||
| words.downcase! | ||
| days += 1 | ||
| words.split(" ").each do |word| | ||
| if FEELINGS[:happy].include? word | ||
| happy += 1 | ||
| if happy == 3 | ||
| happy_days | ||
| end | ||
| elsif FEELINGS[:sad].include? word | ||
| sad += 1 | ||
| end | ||
|
|
@@ -19,6 +24,15 @@ def analyze_mood(words) | |
| return ":-|" | ||
| end | ||
|
|
||
| def strip_punctuation(sentence) | ||
| sentence = sentence.gsub(/[!.,#]/, "") | ||
| end | ||
|
|
||
|
|
||
| def happy_days | ||
| puts "It took #{days} days to have three happy days" | ||
| 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. Hmm.... ok. This works. But it doesn't follow what was asked. I don't want to have to call analyze_mood in order to find out how many days it takes to get 3 happy days and I don't want the number of happy_days to print every time I call analyze_mood. You solve both problems, which is great! But I don't want these two things tied together. I want to be able to do either operation independent of the other. |
||
|
|
||
| text = [ | ||
| "03/01 I'm having a terrible horrible no good day.", | ||
| "03/13 Yesterday was horrible, but today is great! Yay!", | ||
|
|
@@ -28,5 +42,10 @@ 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]) | ||
| text.each do |sentence| | ||
| sentence = strip_punctuation(sentence) | ||
| analyze_mood(sentence) | ||
| sentence_array = sentence.split(" ") | ||
| puts "#{sentence_array[0]} #{analyze_mood(sentence)}" | ||
| happy_days | ||
| end | ||
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.
👍