Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 28 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,39 +9,39 @@ 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 | The constant FEELINGS is being given the value of a hash containing two keys.
| 2 | happy: is the first hash key of the FEELINGS hash. Its value is an array with three strings listed in it.
| 3 | sad: is the second hash key of the FEELINGS hash. Its value is an array with three strings listed in it.
| 6 | The method analyze_mood(words) is being named and defined.
| 7-8 | The variables happy and sad are being set to a value of 0.
| 9 | The parameter 'words' is being permanently set to be in all downcase letters.
| 10 | The parameter 'words' is being split into an array. Then an each loop is started.
| 11 | This is the continuation of the each loop, adding an if loop inside of it. This is saying "Does the value of key ':happy' (in the constant 'FEELINGS') include any of the words in the parameter 'words' array?"
| 12 | The value of the variable 'happy' will be increased by one.
| 13 | This elsif statement statement is a continuation of the if loop of 11. It is saying "Does the value of key ':sad' (in the constant 'FEELINGS') include any of the words in the parameter 'words' array?"
| 14 | The value of the variable 'sad' will increase by one.
| 17-19 | After each word in the 'words' array has gone through the loops, these lines evaluate which has the higher value (happy or sad). If happy is higher, a smiley emoticon is returned. If sad is higher, a frowny emoticon is returned. If they are the same value a straight-face emoticon is returned.

### 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? |
| Why do we need line 9? | Line 9 makes it so that the parameter words can be matched to the words in the happy: and sad: arrays, which are all in downcase.
| What is the relationship between `words` and `word` (line 10)? | 'words' is now an array of words. 'word' is one element inside of the array.
| Why doesn't line 19 have an associated if/condition? | Because it is implicit. If neither of the above conditions occur, Ruby will have returned nothing. So Ruby will move on to return the last thing.
| What is the relationship between `text[0]`, `text[1]`, and `words`? |

### Assignment: Requirements
Expand All @@ -54,7 +54,7 @@ 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? It comes out as neutral because all of the words that would normally be evaluated true (by lines 11 or 13) have a punctuation mark at the end of them in the string being evaluated. We could fix this by striping the strings of their punctuation before we turn it into an array. We could do that by adding a line in the analyze_mood method, using a gsub and a regexp.

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 (#).

Expand All @@ -67,7 +67,7 @@ After writing this method, our new result should be:
...
```

**think**: Where should we call `strip_punctuation`? Does it matter? Why?
**think**: Where should we call `strip_punctuation`? Does it matter? Why? We need to call strip_punctuation before we call analyze_mood, because in order for analyze_mood to populate correct results the strings must be stripped of their text.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is true. What I should have asked is "Should you call strip_punctuation inside or outside the analyze_mood method?"


3. Write a method called `happy_days` to determine how many logged entries it takes until there have been three :-) happy days.

Expand All @@ -76,7 +76,7 @@ Your output could be something like:
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?
**think**: What are you going to do if there aren't at least 3 happy days? Where do you need to handle that case? What am I going to do? Well, I would need to change my code to account for this. I would add an else to my if/elsif loop: puts "There were less than 3 happy days in #{entries} entries. :-(" (I actually did do this, because it made sense to me to do so).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is also true. But you should think about the fact that you are now returning two different types of data potentially. (I can explain this better in person)


4. Write a method called `overall_mood` to determine the most common mood across all logged entries.

Expand All @@ -85,6 +85,6 @@ Your output could be something like:
The most common mood is :-)
```

**think**: Should you use an array or a hash to solve this problem? Why?
**think**: Should you use an array or a hash to solve this problem? Why? ...Well, I used neither to solve the problem, because it made sense to me to continue to go along the path the previous methods had. To be honest, I'm not really sure how to solve the problem using arrays and hashes. ...Did I miss the point of the question? I'm sorry. :-/

**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?
**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? You know, I'm not sure, and I'm kind of confused. I look forward to going over this with the class (or someone one on one), because I don't think I did this correctly. I mean, my code works, but its long and complicated, and there was probably a better way to do it.
77 changes: 73 additions & 4 deletions mood-analysis.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
FEELINGS = {
happy: %w(yay, good, great),
sad: %w(terrible, awful, horrible)
happy: %w(yay good great),
sad: %w(terrible awful horrible)
}

def strip_punctuation(words)
words.gsub(/[!.,#]/, '')
end

def analyze_mood(words)
happy = 0
sad = 0
Expand All @@ -19,6 +23,60 @@ def analyze_mood(words)
return ":-|"
end

def happy_days(words)
entries = 0
happy = 0
strip = []

words.each do |word|
strip << strip_punctuation(word)
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be shorter and less repetitive in logic if you said

strip = strip_punctuation(words)


strip.each do |string|
if happy < 3
entries += 1
if analyze_mood(string) == ":-)"
happy += 1
end
elsif happy == 3
puts "It takes #{entries} entries for 3 happy days to occur."
else
puts "There were less than 3 happy days in #{entries} entries. :-("
end
end
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method would be better if it just returned a fixnum, not a string and then you can print the result of the method call in a string. This would isolate the return value and allow us to do other things with this information instead of being constrained to just saying how many days it takes for three happy days.


def overall_mood(words)
happy = 0
sad = 0
neutral = 0
strip = []

words.each do |word|
strip << strip_punctuation(word)
end

strip.each do |string|
if analyze_mood(string) == ":-)"
happy += 1
elsif analyze_mood(string) == ":-("
sad += 1
elsif analyze_mood(string) == ":-|"
neutral +=0
end
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works, but doesn't allow for expansion very easily if we wanted to add other feelings. i.e. "Afraid :O" or something. This is where I wanted you all to see that a hash makes the solution cleaner and more expandable in the future.


if happy > (sad && neutral)
puts "The most common mood is :-)"
elsif sad > (happy && neutral)
puts "The most common mood is :-("
elsif neutral > (sad && happy)
puts "The most common mood is :-|"
else
puts "The most common mood is unable to be determined."
end
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then here we'd be able to utilize the hash's methods to find the max mood easier as well.


text = [
"03/01 I'm having a terrible horrible no good day.",
"03/13 Yesterday was horrible, but today is great! Yay!",
Expand All @@ -28,5 +86,16 @@ 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])
stripped = []

text.each do |string|
stripped << strip_punctuation(string)
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you had decided to call strip_punctuation inside the analyze_mood method, you wouldn't have had to do this code so repetitively throughout. What I wanted you to think about is "Is there ever a situation where I wouldn't want to strip the punctuation?" and if there isn't then this should be built into the analyze_mood method.


stripped.each do |string|
puts string[0..5] + analyze_mood(string)
end

happy_days(text)

overall_mood(text)