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
61 changes: 32 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,43 @@ 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 | an hash is being assigned
| 2 | a array is being assigned to the key happy
| 3 | a array is being assigned to the key sad
| 6 | an instance method "analyze_mood" is define and expecting one input
| 7-8 | sets happy and sad value to zero
| 9 | sets the variable word to all lower case letters
| 10 | the words are split into an array by the spaces and each iteration goes through a word in the array
| 11 | checks if the element exists in the happy key
| 12 | adds one point to the value of happy
| 13 | if line 10 fails, checks if the element exists in the sad key array
| 14 | adds one point to the value of sad
| 17-19 | compares happy and sad values and returns a string

### 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 |key/symbol
| happy |variable/fixnum
| words |variable/string
| words.split(" ") |array
| FEELINGS[:sad] |array
| FEELINGS[:happy].include? |boolean
| analyze_mood(text) | instance method/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 values in the hash are in lower case
| What is the relationship between `words` and `word` (line 10)?
|words becomes an array full of 'word' instances
| Why doesn't line 19 have an associated if/condition?
| it is only run if the previous two return conditions are false
| What is the relationship between `text[0]`, `text[1]`, and `words`?
|"words" is equal to text[x] if used like in line 31-32

### 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 +57,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 _sad_ when it should be _happy_? How could we fix this? the include? method doesn't work when word is not exactly the same as the element in the array. "yay!" is not equal to "yay"

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 @@ -67,7 +70,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? You can call strip_punctuation right after line 14 because this is the only point in the code where the words are being analyzed individually or replace element in line 38. I think it's easier to read if you put it in line 14.

Choose a reason for hiding this comment

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

This is a great answer! I was also looking for the fact that it belong in the analyze_mood method because there shouldn't be a situation where we want to analyze_mood without strip_punctuation.


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,15 +79,15 @@ 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? On line 49, the code is supposed to break if the number of happy entries reaches 3. If not, it will finish running through all the entries and output the greatest number of happy entries in the whole journal. We can add an if statement in the happy_days method that returns a different text if happy_entries never reaches 3.

Choose a reason for hiding this comment

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

You write good explanations!


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

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? A hash would be easier to use and read. If an array is used, there may be confusion on which 'face' is used for which element. Assigning the 'face' as the keys makes it clear.

Choose a reason for hiding this comment

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

👍


**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?
57 changes: 51 additions & 6 deletions mood-analysis.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
FEELINGS = {
happy: %w(yay, good, great),
sad: %w(terrible, awful, horrible)
happy: %w(yay good great),
sad: %w(terrible awful horrible)
}
def strip_punctuation(word)
word_strip = word.gsub(/[^a-z0-9\s]/i, "")
return word_strip
end

def analyze_mood(words)
happy = 0
sad = 0
words.downcase!
words.split(" ").each do |word|
if FEELINGS[:happy].include? word
word_s = strip_punctuation(word)
if FEELINGS[:happy].include? word_s
happy += 1
elsif FEELINGS[:sad].include? word
elsif FEELINGS[:sad].include? word_s
sad += 1
end
end
Expand All @@ -28,5 +33,45 @@ 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 |element|
date = element[0..4]
smiley = analyze_mood(element)
puts date + " "+ smiley
end

Choose a reason for hiding this comment

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

I like the naming here with variables that makes this code easy to read.


def happy_days(entries)
happy_entry = 0
general_entry = 0
#keep track of # of happy days and entries for each entry
entries.map do |entry|
happy_entry += 1 if analyze_mood(entry) == ":-)"
general_entry += 1
break if happy_entry == 3

Choose a reason for hiding this comment

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

There's no reason to just break here instead of just putting the return right here

end
#output "it takes 5 entries for 3 happy days to occur"
if happy_entry < 3
puts "Sorry, not enough happy days!"
else
puts "It takes #{general_entry} entries for 3 happy days to occur."
end
end

Choose a reason for hiding this comment

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

This is fine. But would be better if this method returned the smallest piece of information possible. In this case, just the fixnum of how many days it takes to get 3 happy days. If we do that then we have the ability to utilize this method in different ways instead of being tied to this exact output text / use case.


def overall_mood(entries)
frequency = {
":-)" => 0,
":-(" => 0,
":-|" => 0
}

entries.map do |entry|
mood_instance = analyze_mood(entry)
frequency[":-)"] += 1 if mood_instance == ":-)"
frequency[":-("] += 1 if mood_instance == ":-("
frequency[":-|"] += 1 if mood_instance == ":-|"
end

Choose a reason for hiding this comment

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

This looks really good and clean, but could have been generalized to make it adaptable with more moods without the need to re-write.

My overall_mood method:

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

Choose a reason for hiding this comment

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

FUNNY. We even picked the same variables names. I guess we think alike!


winner = frequency.max_by{|key, value| value}
puts "The most common mood is #{winner[0]}"
end

Choose a reason for hiding this comment

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

Here I also would have liked to see the smallest piece of information returned. In this case, just the emoticon.

happy_days(text)
overall_mood(text)