f74c5381 by Barry

Added a !question function that looks up in wolfram alpha then falls back to wik…

…ipedia using natural language parsing to look up proper nouns.
1 parent 1fc2beff
...@@ -4,12 +4,18 @@ import random ...@@ -4,12 +4,18 @@ import random
4 import datetime 4 import datetime
5 import re 5 import re
6 6
7 import traceback
8 import sys
9 import wikipedia
10
7 from dateutil.parser import parse 11 from dateutil.parser import parse
8 12
9 from discord.object import Object 13 from discord.object import Object
10 from ago import human 14 from ago import human
11 import simplejson as json 15 import simplejson as json
12 from collections import defaultdict 16 from collections import defaultdict
17 from nltk.tag import pos_tag
18 import wolframalpha
13 19
14 member_status = 'members.json' 20 member_status = 'members.json'
15 deliveries_file = 'deliveries.json' 21 deliveries_file = 'deliveries.json'
...@@ -21,6 +27,20 @@ credentials = 'creds.json' ...@@ -21,6 +27,20 @@ credentials = 'creds.json'
21 muted_until = datetime.datetime.now() 27 muted_until = datetime.datetime.now()
22 28
23 client = discord.Client() 29 client = discord.Client()
30 wolf = {}
31
32 def format_exception(e):
33 exception_list = traceback.format_stack()
34 exception_list = exception_list[:-2]
35 exception_list.extend(traceback.format_tb(sys.exc_info()[2]))
36 exception_list.extend(traceback.format_exception_only(sys.exc_info()[0], sys.exc_info()[1]))
37
38 exception_str = "Traceback (most recent call last):\n"
39 exception_str += "".join(exception_list)
40 # Removing the last \n
41 exception_str = exception_str[:-1]
42
43 return exception_str
24 44
25 def leaders(xs, top=20): 45 def leaders(xs, top=20):
26 counts = defaultdict(int) 46 counts = defaultdict(int)
...@@ -557,6 +577,29 @@ Stuff: ...@@ -557,6 +577,29 @@ Stuff:
557 client.send_message(message.channel, '{} Your fortune is... {}'.format(message.author.mention(), random.choice(data).encode('utf-8',errors='ignore'))) 577 client.send_message(message.channel, '{} Your fortune is... {}'.format(message.author.mention(), random.choice(data).encode('utf-8',errors='ignore')))
558 578
559 579
580 if message.content.startswith('!question'):
581 question = message.content[10:]
582 res = wolf.query(question)
583 try:
584 if len(res.pods):
585 pod_text = ""
586 for pod in res.pods:
587 if pod.text:
588 pod_text += pod.text
589 client.send_message(message.channel, '{} {}.'.format(message.author.mention(), pod_text[:1000]))
590 else:
591 tagged_sent = pos_tag(question.replace('?', '').split())
592 proper_nouns = [word for word, pos in tagged_sent if pos == 'NNP']
593 wiki_search = " ".join(proper_nouns)
594 print "Looking up {}".format(wiki_search)
595 wiki_out = wikipedia.summary(wiki_search, sentences=3)
596 client.send_message(message.channel, '{} {}.'.format(message.author.mention(), byteify(wiki_out)))
597 return
598 except Exception as e:
599 print(format_exception(e))
600 client.send_message(message.channel, 'I don\'t know {}.'.format(message.author.mention()))
601 return
602
560 if message.content.startswith('!addjoke'): 603 if message.content.startswith('!addjoke'):
561 try: 604 try:
562 json_data=open(joke_file).read() 605 json_data=open(joke_file).read()
...@@ -616,9 +659,12 @@ while retries < 1000: ...@@ -616,9 +659,12 @@ while retries < 1000:
616 try: 659 try:
617 json_data=open(credentials).read() 660 json_data=open(credentials).read()
618 creds = json.loads(json_data) 661 creds = json.loads(json_data)
662 wolf = wolframalpha.Client(creds['wolframkey'])
619 client.login(creds['username'], creds['password']) 663 client.login(creds['username'], creds['password'])
620 664
621 client.run() 665 client.run()
666 except KeyboardInterrupt:
667 quit()
622 except: 668 except:
623 retries += 1 669 retries += 1
624 print("Shit I crashed: Retry %s" % (retries,)) 670 print("Shit I crashed: Retry %s" % (retries,))
......
1 ["What's your name?\", asked the teacher.\n\"Mohammad,\" he replied.\n\"You're in Ireland now,\" replied the teacher, \"So from now on you will be known as Mike.\n\" Mohammad returned home after school.\n\"How was your day, Mohammad?\", his mother asked.\n\"My name is not Mohammad. I'm in Ireland and now my name is Mike”.\n\"Are you ashamed of your name? Are you trying to dishonor your parents, your heritage, your religion? Shame on you!\"\nAnd his mother beat the shit out of him. Then she called his father, who beat the shit out of him again.\nThe next day Mohammad returned to school. The teacher saw all of his fresh bruises.\n\"What happened to you, Mike?\", she asked.\n\"Well shortly after becoming an Irishman, I was attacked by two fucking Arabs.\"", "What does a Jew with an erection get when he walks into a wall? A broken nose.", "What’s the difference between a Catholic priest and acne? Acne doesn’t come on a boys face until he’s 13.", "What does a baby look like after a minute in the microwave? I don’t know, I close my eyes when I masturbate.", "I lost my virginity to a retarded girl last night… I wanted my first time to be special.", "What’s the difference between jam and jelly? I don’t jelly my cock down a bitches throat.", "What do you get if you cross an elephant with a poodle? A dead poodle with an 18 inch wide asshole.", "Why do Jewish girls like to fuck doggy style? They can’t stand to see somebody else have a good time.", "What’s the difference between a British man and his girlfriend? His girlfriend has a higher sperm count.", "How do you know if a Chinese person robs your house? Your homework is done, your computer is upgraded, but two hours later, the fucker is still trying to back out of your driveway.", "What do you call five Mexicans on the bottom of a pool? Cinco", "What did the little Mexican boy (Rui) get for Christmas? My bike.", "How do you kill a redneck? Wait ’till he fucks his sister then cut the brakes on his house.", "What did the kid with no arms get for Christmas?\nA pair of gloves.\nNah just kidding, he's still trying to open his gifts.", "What's black and sits at the top of a staircase?\nStephen Hawking after a house fire.", "What do you call a 5 year old with no friends?\nA sandy hook survivor.", "What's the difference between a Pakistani elementary school and an Al Qaeda base?\nI don't know, I just fly the fucking drone, man.", "What's the difference between food and ebola?\nAfricans get ebola.", "What do you do when your dishwasher doesn't work?\nSlap her.", "http://puu.sh/lSo0C/fcf7ccaed0.jpg", "A man sees a young boy standing at the side of a cliff, looking down.\n\"What's wrong?\" Asks the man.\n\"My parents car skidded and they fell off the cliff.\" Says the crying boy. \n\"Well this just isn't your day is it.\" The man says as he unbuttons his pants.", "I went to my doctor for a checkup last week.\n\"You need to stop masturbating.\" He said.\n\"Why?\"\n\"Because I'm trying to examine you.\"", "I was having sex with a woman last night, and she wouldn't stop calling out another mans name. Who the hell is \"rape\"?", "I don't understand terrorists. They kill themselves to get 72 virgins when they could just become priests and get them now", "Unsure of what to get your girlfriend for Christmas? Get her a pair of shoes and a dildo. If she doesn't like the shoes, she can go fuck herself.", "http://i.imgur.com/BH1kwTG.jpg", "To most Christians, the Bible is like a EULA. Nobody actually reads it, they just skip to the end and say 'I agree'", "When is a fruit also a vegetable? \nWhenever you go into a coma.", "[✓] I'm not a robot", "Goshzilla's height", "I don't find health-related puns funny anymore since I started suffering from an irony deficiency", "I used to be addicted to the Hokey Pokey, but then I turned myself around.", "Roses are red, violets are blue, @rui is a cuck, and so are you", "Did you hear about the guy who got hit in the head with a can of soda? He was lucky it was a soft drink."]
...\ No newline at end of file ...\ No newline at end of file
1 ["What's your name?\", asked the teacher.\n\"Mohammad,\" he replied.\n\"You're in Ireland now,\" replied the teacher, \"So from now on you will be known as Mike.\n\" Mohammad returned home after school.\n\"How was your day, Mohammad?\", his mother asked.\n\"My name is not Mohammad. I'm in Ireland and now my name is Mike”.\n\"Are you ashamed of your name? Are you trying to dishonor your parents, your heritage, your religion? Shame on you!\"\nAnd his mother beat the shit out of him. Then she called his father, who beat the shit out of him again.\nThe next day Mohammad returned to school. The teacher saw all of his fresh bruises.\n\"What happened to you, Mike?\", she asked.\n\"Well shortly after becoming an Irishman, I was attacked by two fucking Arabs.\"", "What does a Jew with an erection get when he walks into a wall? A broken nose.", "What’s the difference between a Catholic priest and acne? Acne doesn’t come on a boys face until he’s 13.", "What does a baby look like after a minute in the microwave? I don’t know, I close my eyes when I masturbate.", "I lost my virginity to a retarded girl last night… I wanted my first time to be special.", "What’s the difference between jam and jelly? I don’t jelly my cock down a bitches throat.", "What do you get if you cross an elephant with a poodle? A dead poodle with an 18 inch wide asshole.", "Why do Jewish girls like to fuck doggy style? They can’t stand to see somebody else have a good time.", "What’s the difference between a British man and his girlfriend? His girlfriend has a higher sperm count.", "How do you know if a Chinese person robs your house? Your homework is done, your computer is upgraded, but two hours later, the fucker is still trying to back out of your driveway.", "What do you call five Mexicans on the bottom of a pool? Cinco", "What did the little Mexican boy (Rui) get for Christmas? My bike.", "How do you kill a redneck? Wait ’till he fucks his sister then cut the brakes on his house.", "What did the kid with no arms get for Christmas?\nA pair of gloves.\nNah just kidding, he's still trying to open his gifts.", "What's black and sits at the top of a staircase?\nStephen Hawking after a house fire.", "What do you call a 5 year old with no friends?\nA sandy hook survivor.", "What's the difference between a Pakistani elementary school and an Al Qaeda base?\nI don't know, I just fly the fucking drone, man.", "What's the difference between food and ebola?\nAfricans get ebola.", "What do you do when your dishwasher doesn't work?\nSlap her.", "http://puu.sh/lSo0C/fcf7ccaed0.jpg", "A man sees a young boy standing at the side of a cliff, looking down.\n\"What's wrong?\" Asks the man.\n\"My parents car skidded and they fell off the cliff.\" Says the crying boy. \n\"Well this just isn't your day is it.\" The man says as he unbuttons his pants.", "I went to my doctor for a checkup last week.\n\"You need to stop masturbating.\" He said.\n\"Why?\"\n\"Because I'm trying to examine you.\"", "I was having sex with a woman last night, and she wouldn't stop calling out another mans name. Who the hell is \"rape\"?", "I don't understand terrorists. They kill themselves to get 72 virgins when they could just become priests and get them now", "Unsure of what to get your girlfriend for Christmas? Get her a pair of shoes and a dildo. If she doesn't like the shoes, she can go fuck herself.", "http://i.imgur.com/BH1kwTG.jpg", "To most Christians, the Bible is like a EULA. Nobody actually reads it, they just skip to the end and say 'I agree'", "When is a fruit also a vegetable? \nWhenever you go into a coma.", "[✓] I'm not a robot", "Goshzilla's height", "I don't find health-related puns funny anymore since I started suffering from an irony deficiency", "I used to be addicted to the Hokey Pokey, but then I turned myself around.", "Roses are red, violets are blue, @rui is a cuck, and so are you", "Did you hear about the guy who got hit in the head with a can of soda? He was lucky it was a soft drink.", "http://puu.sh/mqCuh/d4e52790c7.jpg"]
...\ No newline at end of file ...\ No newline at end of file
......