From 7a9469ad1c03729f59552bc56cf678253c5063cb Mon Sep 17 00:00:00 2001 From: Anna Wilson Date: Thu, 31 Mar 2016 22:30:04 -0700 Subject: [PATCH 1/4] Created strip_punctuation method --- README.md | 49 ++++++++++++++++++++++++------------------------ mood-analysis.rb | 13 +++++++++---- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 61958d6..e650924 100644 --- a/README.md +++ b/README.md @@ -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) | idk ### 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. @@ -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 (#). diff --git a/mood-analysis.rb b/mood-analysis.rb index e49df43..f6322e0 100644 --- a/mood-analysis.rb +++ b/mood-analysis.rb @@ -1,6 +1,6 @@ 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) @@ -19,6 +19,10 @@ def analyze_mood(words) return ":-|" end +def strip_punctiation(words) + words.gsub(/[!.,#]/i, '') +end + text = [ "03/01 I'm having a terrible horrible no good day.", "03/13 Yesterday was horrible, but today is great! Yay!", @@ -28,5 +32,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 From c1eb41099f15a169074687044e0df431d3e2bbae Mon Sep 17 00:00:00 2001 From: Anna Wilson Date: Thu, 31 Mar 2016 22:56:44 -0700 Subject: [PATCH 2/4] Fixed strip_punctuation, now it works in analyze_mood method --- mood-analysis.rb | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/mood-analysis.rb b/mood-analysis.rb index f6322e0..7b3cd42 100644 --- a/mood-analysis.rb +++ b/mood-analysis.rb @@ -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 @@ -19,10 +24,6 @@ def analyze_mood(words) return ":-|" end -def strip_punctiation(words) - words.gsub(/[!.,#]/i, '') -end - text = [ "03/01 I'm having a terrible horrible no good day.", "03/13 Yesterday was horrible, but today is great! Yay!", From bd8550855b3afe33fcfeb71246dc11c4eeff7e81 Mon Sep 17 00:00:00 2001 From: Anna Wilson Date: Thu, 31 Mar 2016 23:19:54 -0700 Subject: [PATCH 3/4] happy_days method functional --- README.md | 1 + mood-analysis.rb | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/README.md b/README.md index e650924..47fa3d6 100644 --- a/README.md +++ b/README.md @@ -69,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. 3. Write a method called `happy_days` to determine how many logged entries it takes until there have been three :-) happy days. diff --git a/mood-analysis.rb b/mood-analysis.rb index 7b3cd42..7e6f99d 100644 --- a/mood-analysis.rb +++ b/mood-analysis.rb @@ -24,6 +24,37 @@ def analyze_mood(words) return ":-|" end +def happy_days(text) + #text needs to remain in order as an array. + #analyze_mood called on each line of 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 + end + end + if smile_count == 3 + return "It took #{day_count} days for happiness to occur." + else + return "Sorry, no happy days..." + end + #save the return value + #when return value of :-) = 3, puts index of that line + 1 for number of days. +end +# 3. Write a method called `happy_days` to determine how many logged entries +# it takes until there have been three :-) happy days. +# +# Your output could be something like: +# ``` +# It takes 5 entries for 3 happy days to occur +# ``` + + text = [ "03/01 I'm having a terrible horrible no good day.", "03/13 Yesterday was horrible, but today is great! Yay!", From e7ec0ebba0c2378fbeccb578dd77979b3e65a094 Mon Sep 17 00:00:00 2001 From: Anna Wilson Date: Thu, 31 Mar 2016 23:43:33 -0700 Subject: [PATCH 4/4] Added overall_mood method --- README.md | 5 ++++- mood-analysis.rb | 19 ++++++++----------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 47fa3d6..149b972 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ What's the Data Type of the following? | words.split(" ") | Array | FEELINGS[:sad] | Array | FEELINGS[:happy].include? | Boolean -| analyze_mood(text) | idk +| analyze_mood(text) | String ### Explaining the Code | Question | Answer @@ -79,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. @@ -88,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. **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. diff --git a/mood-analysis.rb b/mood-analysis.rb index 7e6f99d..a482392 100644 --- a/mood-analysis.rb +++ b/mood-analysis.rb @@ -25,8 +25,6 @@ def analyze_mood(words) end def happy_days(text) - #text needs to remain in order as an array. - #analyze_mood called on each line of text. smile_count = 0 day_count = 0 text.each do |line| @@ -43,17 +41,16 @@ def happy_days(text) else return "Sorry, no happy days..." end - #save the return value - #when return value of :-) = 3, puts index of that line + 1 for number of days. end -# 3. Write a method called `happy_days` to determine how many logged entries -# it takes until there have been three :-) happy days. -# -# Your output could be something like: -# ``` -# It takes 5 entries for 3 happy days to occur -# ``` +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 text = [ "03/01 I'm having a terrible horrible no good day.",