Skip to content

Commit

Permalink
Merge pull request konfuzio-ai#41 from ghazalayasmeen/main
Browse files Browse the repository at this point in the history
added laugh generator
  • Loading branch information
atraining committed Sep 20, 2023
2 parents 89d7620 + 9e028b7 commit b3ac6ef
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
26 changes: 26 additions & 0 deletions bots/ghazalayasmeen/joke.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Why don't scientists trust atoms? Because they make up everything!
Why did the chicken go to the seance? To talk to the other side!
Why don't we tell secrets on a farm? Because the potatoes have eyes, the corn has ears, and the beans stalk.
I would tell you a joke about time travel, but you didn't like it.
Why don't programmers like nature? It has too many bugs.
Why do programmers always mix up Christmas and Halloween? Because Oct 31 == Dec 25!
Why do programmers prefer iOS development? It's less Java to spill.
Why did the programmer go broke? Because he used up all his cache.
Why did the programmer get kicked out of school? Because he kept breaking the class.
Why did the programmer go on a diet? He had too many bytes.
Why do programmers prefer dark mode? Because light attracts bugs.
Why was the computer cold? It left its Windows open.
Why did the web developer go broke? Because he didn't get enough cache.
Why did the programmer refuse to play cards with the jungle cat? Because he was afraid of Cheetahs.
Why was the developer unhappy at their job? They wanted arrays.
Why did the scarecrow win an award? Because he was outstanding in his field!
Parallel lines have so much in common. It's a shame they'll never meet.
Why did the math book look sad? Because it had too many problems.
I told my wife she was drawing her eyebrows too high. She seemed surprised.
I couldn't figure out how to put my seatbelt on. Then it just clicked.
Why did the bicycle fall over? Because it was two-tired.
What do you call a bear with no teeth? A gummy bear.
Why don't eggs tell jokes? Because they might crack up.
Why did the tomato turn red? Because it saw the salad dressing!
I used to play piano by ear, but now I use my hands.
Did you hear about the claustrophobic astronaut? He just needed a little space.
59 changes: 59 additions & 0 deletions bots/ghazalayasmeen/joke_bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
from textblob import TextBlob
import random


class Bot:
name = 'The Laugh Generator'

def __init__(self, joke_file='joke.txt'):
self.jokes = self.read_jokes_from_file(joke_file)

def read_jokes_from_file(self, file_name):
jokes = []
try:
with open(file_name, 'r') as file:
jokes = [line.strip() for line in file.readlines()]
except FileNotFoundError:
print(f"Jokes file '{file_name}' not found. Using default jokes.")
return jokes

def tell_joke(self):
# Just tell a random joke from our list
return random.choice(self.jokes)

def rate_joke(self, joke):
creativity = random.uniform(0, 1) # Random creativity rating between 0 and 1
timeliness = random.uniform(0, 1) # Random timeliness rating between 0 and 1

# Rate the joke based on its sentiment polarity
blob = TextBlob(joke)
polarity = blob.sentiment.polarity
# Convert polarity from [-1, 1] to [1, 10] for a more positive range
rating = (polarity + 1) * 5
rating = min(10, max(1, rating))
creativity_rating = (creativity + 1) * 5
timeliness_rating = (timeliness + 1) * 5
personalization_rating = self.personalize_rating()

overall_rating = (rating + creativity_rating + timeliness_rating + personalization_rating) / 4

# Ensure the rating is within the correct range
return min(10, max(1, overall_rating))

def personalize_rating(self):
# Calculate a personalized rating based on user preferences
# For simplicity, just return a random rating for demonstration
return random.randint(1, 10)


# Main method for testing
if __name__ == "__main__":
bot = Bot()

# Generate and tell a joke
joke = bot.tell_joke()
print("Joke:", joke)

# Rate the joke
rating = bot.rate_joke(joke)
print("Rating:", rating)
17 changes: 17 additions & 0 deletions bots/ghazalayasmeen/test_bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest
from joke_bot import Bot

@pytest.fixture
def bot():
return Bot()

def test_tell_joke(bot):
joke = bot.tell_joke()
assert isinstance(joke, str), "Joke is not a string."
assert len(joke) > 50, "Joke length is not within the correct range."

def test_rate_joke(bot):
joke = "Why was the computer cold at the office? Because it left its Windows open."
rating = bot.rate_joke(joke)
assert isinstance(rating, (int, float)), "Rating is not a number."
assert 1 <= rating <= 10, "Rating is not within the correct range."

0 comments on commit b3ac6ef

Please sign in to comment.