Skip to content

Instantly share code, notes, and snippets.

@livmaria7891
Last active August 15, 2016 04:18
Show Gist options
  • Save livmaria7891/08c79c6391551be7233c164740fa3515 to your computer and use it in GitHub Desktop.
Save livmaria7891/08c79c6391551be7233c164740fa3515 to your computer and use it in GitHub Desktop.
Mood Analysis assignment
#Assigns a hash to the variable FEELINGS
FEELINGS = {
#Assigns array to happy key
happy: %w(yay good great),
#Assigns array to sad key
sad: %w(terrible awful horrible)
#Close Hash
}
def strip_punctuation(entry)
return entry.gsub(/\W/, " ")
end
#Creates method 'analyze_mood' that takes one parameter
def analyze_mood(words)
#Sets variables happy and sad equal to zero
happy = 0
sad = 0
#calls downcase to change string is words argument to lowercase
def analyze_mood(words)
happy = 0
sad = 0
words.downcase!
words = strip_punctuation(words)
#creates array, new items added wherever there is a space in the string
words.split(" ").each do |word|
#Check if the returned array contains an item from the value of the 'happy' key, if so 1 is added to the happy variable
if FEELINGS[:happy].include?(word)
happy += 1
#Check if the returned array contains an item from the value of the 'sad' key, if so 1 is added to the sad variable
elsif FEELINGS[:sad].include?(word)
sad += 1
end
end
#If there are more happy words than sad, return :-), more sad than happy return :(, otherwise return neutral
if happy > sad
return ":-)"
elsif happy < sad
return ":-("
else
return ":-|"
end
end
end
text = [
"03/01 I'm having a terrible horrible no good day.",
"03/13 Yesterday was horrible, but today is great! Yay!",
"04/02 Sad Panda. #terribleday",
"04/15 Hello World, today is fabulous! #yay",
"05/01 Great! Yay! Good! Yay! Happy. Happy.",
"05/11 Yay, yay, yay! I'm having a awfuly great day."
]
analyze_mood(text)
mood_keeper = []
text.each do |day|
puts "#{day.split(" ")[0]} #{analyze_mood(day)}"
mood_keeper << analyze_mood(day)
end
def happy_days(array)
entry = 0
happy_face = 0
array.each do |face|
if face == ":-)"
happy_face += 1
end
entry += 1
if happy_face == 3
puts "It takes #{entry} entries for 3 happy todays to occur."
end
end
if happy_face < 3
puts "Bummer. #{happy_face} happy day(s)."
end
end
def overall_mood(arr)
happy = 0
sad = 0
neutral = 0
arr.each do |face|
if face == ":-)"
happy += 1
elsif face == ":-|"
neutral += 0
else
sad += 1
end
end
if happy > sad && happy > neutral
puts "The overall mood is happy!"
elsif sad > neutral
puts "The overall mood is sad :-("
else
puts "The mood is neutral."
end
end
happy_days(text)
happy_days(mood_keeper)
overall_mood(mood_keeper)
=begin
Data Types
FEELINGS: hash
:sad - key
happy - integer
words string
words.split - array of strings
FEELINGS[:sad] - array
FEELINGS[:happy].include? boolean
analyze_mood(text) - string
Why do we need line 9?
All of the strings in the arrays are written in lowercase, so if any of the
words in the arguments contain an upper case letter it won't return true in
the if statement, even if the letters otherwise match.
What is the relationship between words and word (line 10)?
Words references the argument passed to the method. Word is a place holder for
each item in the array created by .split. "Word" is each individual word in the
string of "words".
Why doesn't line 22 have an associated if/condition?
It's unnecessary because happy > sad and happy < sad cover every possible condition
except a tie between happy and sad. It could say 'elsif happy == sad', but that would be
unnecessary because if the value isn't greater or less than, it has to be equal.
What is the relationship between text[0], text[1], and words?
text[0] and text[1] are both items in the text array. Words is a place holder
for either of them within the method.
=end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment