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
59 changes: 35 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,44 @@ 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 FEELINGS
| 2 | assigning key-value pairs in hash, using literal for the value that generates an array of strings. key :happy
| 3 | (see above) key :sad
| 6 | defining method analyze_mood with argument (words)
| 7-8 | assigning value of 0 to variable happy, assigning value of 0 to variable sad
| 9 | putting words into lower case
| 10 | applying .split to text words to get array of single words, then initiating .each loop
| 11 | comparing the word to the words in the value array of :happy
| 12 | redefine happy variable as happy + 1
| 13 | elsif condition comparing the word to words in value array of :sad
| 14 | redefine sad variable as sad + 1
| 17-19 | evaluating values of happy and sad variables and returning strings depending on outcome of evaluation

### 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 of strings
| FEELINGS[:sad] | array (say, "FEELINGS at sad")
| 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? | .include? method is case-sensitive

| What is the relationship between `words` and `word` (line 10)? | "words" is the array of words submitted as text by the user. "word" is each member of that array, or an individual word/string.

| Why doesn't line 19 have an associated if/condition? | it doesn't need one, because the two evaluations will return only true or false, so if neither of those is returned, the code automatically progresses to the final return.

| What is the relationship between `text[0]`, `text[1]`, and `words`? |
"text[0]" and "text[1]" are the arguments passed into analyze_mood. Within that method, they will be labeled as "words".

### 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 @@ -54,7 +58,10 @@ 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 .split is creating an array of items divided by spaces. It is including punctuation in each item where it appears next to a word. We need to take the punctuation out, because the happy and sad arrays contain no punctuation.

Choose a reason for hiding this comment

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

👍


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

**think**: Where should we call `strip_punctuation`? Does it matter? Why?
I called in within the loop where I am asking it to return the date and resulting string, because it has to be called on a string, not an array of strings, and this is where I was already destructuring the array of strings.

Choose a reason for hiding this comment

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

This is good reasoning. I should have also asked "Should you call strip_punctuation inside or outside analyze_mood? Why?" Your reasoning is good but I was also looking for the fact that there's probably no reason we would ever want to analyze_mood on text with punctuation, so we should call it inside the 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 @@ -77,6 +85,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?
I would add an if statement like "if entries.length == no_of_days, puts 'this period didn't have three happy days.'"

Choose a reason for hiding this comment

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

👍


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

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

**think**: Should you use an array or a hash to solve this problem? Why?
Because we were simply tallying the data and not trying to find specific days, I used an array to count the happy and sad days. I stored the dates of the happy and sad results instead of just tallying how many there were because it seemed like a useful piece of data to be able to access (which days were sad, which were happy, would be easy to access).

Choose a reason for hiding this comment

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

This reasoning is well supported, but I would have like to see that data stored somewhere where it could be used later, for this justification.


**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?
I was thinking about storing the happy and sad arrays and their length in a hash of feelings, like { happy: happy.length, sad: sad.length}, and then doing a .sort_by method on the hash to find the most common one. That would be a refactor that would allow you to add more emotions with more ease.

Choose a reason for hiding this comment

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

Yes! This is what I wanted. But then you don't need the array at all.

My overall_mood method, for reference:

def overall_mood(entries)
  return nil if entries.length == 0
  emoticons = Hash.new(0)
  entries.each do |entry|
    emoticon = analyze_mood(entry)
    emoticons[emoticon] += 1
  end
  return emoticons.max_by{|k,v| v}[0]
end

But I DO like your reasoning and why you did it.

62 changes: 58 additions & 4 deletions mood-analysis.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
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.split(" ").each do |word|
word = strip_punctuation(word)
if FEELINGS[:happy].include? word
happy += 1
elsif FEELINGS[:sad].include? word
Expand All @@ -19,6 +21,12 @@ def analyze_mood(words)
return ":-|"
end

# remove ! . , #
def strip_punctuation(words)
new_word = words.gsub(/[!.,#]/, "")
end


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 +36,51 @@ 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])
sad_text = [
"03/01 I'm having a terrible horrible no good day.",
"03/13 Yesterday was horrible, but today is horrible!",
"04/02 Sad Panda. #terribleday",
"04/15 Hello World, today is horrible! ",
"05/01 Great! Yay! Good! Yay! Happy. Happy.",
"05/11 Yay, yay, yay! I'm having a awfuly great day."
]

def happy_days(entries)
happy = 0
no_of_days = 0
entries.each do |day|
no_of_days += 1
mood = analyze_mood(day)
if mood == ":-)"
happy += 1
end
break if happy == 3
end
print "It takes #{no_of_days} days for 3 happy days to occur."
end

Choose a reason for hiding this comment

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

nit-picky: watch your tabbing.

Choose a reason for hiding this comment

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

I would have prefered to see this method return the smallest piece of information asked for. In this case, a fixnum representing the number of days it took to hit 3 happy days, instead of a string containing that infomration. That would allow more flexibility in using this method for different situations rather than being bound to this specific use case.


def overall_mood(entries)
happy = []
sad = []
no_of_days = 0

entries.each do |day|
no_of_days += 1
mood = analyze_mood(day)
if mood == ":-)"
happy << day[0..4]
else sad << day[0..4]
end
end

Choose a reason for hiding this comment

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

Oooh. This is interesting. I like that you stored the days that each mood happened on in separate arrays. Seems like maybe we could utilize this information for something cool, except that the array is stored in this method alone and not returned to anywhere else. I'd argue that a hash is a better choice here because all we are utilizing is emoticons and their counts (key/values). This is actually a common thing to see in whiteboarding interviews or problem solving of that nature (counting an undetermined number of things).

if happy.length > sad.length
puts "The most common mood is happy."
elsif happy.length == sad.length
puts "Equal sadness and happiness happened."
else puts "The most common mood is sad."
end
end

Choose a reason for hiding this comment

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

Same thing here. I wish this returned to smallest piece of information possible. In this case, just an emoticon that represents the most common mood seen.


# testing.
# overall_mood(text)
#
# overall_mood(sad_text)