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
53 changes: 29 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | Establish hash called FEELINGS
| 2 | Establish key happy with array of "happy" strings
| 3 | Establish key sad with array of "sad" strings
| 6 | Establish method called analyze_mood that requires (words)
| 7-8 | Establish happy variable as 0, Establish sad variable as 0
| 9 | takes words and transforms into lower case
| 10 | Split words apart (by their spaces) and put in an array. Then look at each word and do...
| 11 | if the word being looked at is in the Feelings:happy key...
| 12 | add one to the happy variable.
| 13 |if the word being looked at is in the Feelings:sad key...
| 14 | add 1 to the sad variable.
| 17-19 | return a smiley face if the happy variable is > than sad, a sad face if sad is > happy or a neutral face 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 | 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? | All words analyzed need to be lower case so that they will match the words in FEELINGS.
| What is the relationship between `words` and `word` (line 10)? | words is the name of the string that was input with the method. word is the given variable name that will identify each particular word as it goes through the .each loop
| Why doesn't line 19 have an associated if/condition? | It doesn't need to because there are only three possible outcomes (>, < or =). Because > and < were already defined, the last possibility has to be =.
| What is the relationship between `text[0]`, `text[1]`, and `words`? | Text0 is the first item in the text array. Text1 is the second item in the text array. words is the placeholder variable for whatever argument is given to the method, which in this case will be either text[0] or text[1].

### 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.
Expand All @@ -55,6 +55,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?
It does not count yay as a happy word because the exclamation point at the end of the string is not removed. Since "yay!" != "yay", it is not counted. We could fix this by including words.delete! "!", ".", ","

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 @@ -68,6 +69,7 @@ After writing this method, our new result should be:
```

**think**: Where should we call `strip_punctuation`? Does it matter? Why?
I think we should call strip_punctuation inside of the analyze_mood method, anytime before the .each loop so the loop doesn't analyze any words that still have the punctuation attached.

Choose a reason for hiding this comment

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

Good. But why did you call it inside analyze_mood instead of outside it? (I agree with your choice, I just want to know your reasoning.)


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

Expand All @@ -77,6 +79,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?
See my code for how I handled this case. If the happy_days method did not produce at least three :-)s when analyzing the text, then a different value is returned.

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

Expand All @@ -86,5 +89,7 @@ The most common mood is :-)
```

**think**: Should you use an array or a hash to solve this problem? Why?
I used an array, however I think you could use a hash just as well. I chose an array so I could use enumerable methods on it, and I could not think of a reason why key-value pairs would be neccessary.

Choose a reason for hiding this comment

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

See comments in code.


**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?
The overall_mood method can be rewritten to use another method instead of analyze_mood. So a new method called analyze_feelings, for example, could replace analyze_mood in this method. I don't know if I understand what this question is asking.

Choose a reason for hiding this comment

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

I think I should have re-written this question. I was trying to ask what if we add another feeling like "Afriad :O" what all do we need to change, etc.

42 changes: 38 additions & 4 deletions mood-analysis.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
FEELINGS = {
happy: %w(yay, good, great),
sad: %w(terrible, awful, horrible)
happy: ["yay", "good", "great"],
sad: ["terrible", "awful", "horrible"]
}

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

def analyze_mood(words)
happy = 0
sad = 0
words.downcase!
words = strip_punctuation(words)
words.split(" ").each do |word|
if FEELINGS[:happy].include? word
happy += 1
Expand All @@ -19,6 +24,34 @@ def analyze_mood(words)
return ":-|"
end

def happy_days(text)
smile_count = 0
day_count = 0
text.each do |line|
day_count += 1
if analyze_mood(line) == ":-)"
smile_count += 1
end
if smile_count == 3
break

Choose a reason for hiding this comment

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

This if/break combo works, but there's no advantage to it over just putting the return "It took... here. That way you also wouldn't have to repeat the exact same if condition test.

end
end
if smile_count == 3
return "It took #{day_count} days for happiness to occur."
else
return "Sorry, no happy days..."

Choose a reason for hiding this comment

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

If we did what I stated above, then here you don't need an if (else) at all because the only way we get here is that 3 happy days weren't found.

end
end

def overall_mood(text)
moods_array = []
text.each do |line|
moods_array << analyze_mood(line)
end
overall_mood = moods_array.max_by{|mood| moods_array.count(mood)}
return "The most common mood is #{overall_mood}"
end

Choose a reason for hiding this comment

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

This works. A hash makes more sense to me because we can easily count the emoticons in the hash where the emoticon is the key and the count is the value, this type of usage (counting) is quite frequently used in lots of whiteboarding problems. Storing things in an array is fine, except that we have no use for the series of emoticons here, only their counts.

Choose a reason for hiding this comment

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

Also, watch your formatting here. Everything in def overall_mood should be tabbed over one.

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 +61,6 @@ 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 |line|
puts line[0..4] + " " + analyze_mood(line)
end