def get_response(self): """ Get a response from the chatbot and display it. """ user_input = self.usr_input.get() self.usr_input.delete(0, tk.END) if self.user_name == "": self.user_name = user_input if self.user_name == "": self.conversation['state'] = 'normal' self.conversation.insert(tk.END, "Enter your name:" + "\n") self.conversation['state'] = 'disabled' else: response = chatbot.get_response("Hello") self.conversation['state'] = 'normal' self.conversation.insert( tk.END, "Human: " + user_input + "\n" + "ChatBot: " + str(response.text) + "\n") self.conversation['state'] = 'disabled' else: command = 'null' if user_input[0:4] == 'play': command = 'play' if user_input[0:5] == 'tweet': command = 'tweet' if command == 'play': if user_input == 'play song': exec(open('rand_beat.py').read()) elif user_input == 'play game': exec(open('game_run.py').read()) elif command == 'tweet': status = "From " + self.user_name + ": " + user_input[6:] new_tweet = tweet.Tweet(status=status) self.conversation['state'] = 'normal' self.conversation.insert( tk.END, "\n" + self.user_name + " just tweeted: " + user_input[6:] + "\n" + "Check out your tweet at: https://twitter.com/project_i_do" + "\n" + "\n") self.conversation['state'] = 'disabled' else: response = chatbot.get_response(user_input) self.conversation['state'] = 'normal' self.conversation.insert( tk.END, "Human: " + user_input + "\n" + "ChatBot: " + str(response.text) + "\n") self.conversation['state'] = 'disabled'
def buildDict4Tweets(self, foldername): """Build dictionary for tweets got from PHEME dataset. The raw_data.json will be generated in each event folder. Arguments: foldername {str} -- folder name of an event """ resultDict = defaultdict(dict) resultPickle = [] folderpath = os.path.join(self.rootPath, foldername) if not os.path.exists(folderpath): print("the path {} does not exist.".format(folderpath)) return rnrsName = [ rnr for rnr in os.listdir(folderpath) if os.path.isdir(os.path.join(folderpath, rnr)) ] for rnrName in rnrsName: rnrPath = os.path.join(folderpath, rnrName) # print("rnrPath ", rnrPath) rumorsName = [ rumor for rumor in os.listdir(rnrPath) if os.path.isdir(os.path.join(rnrPath, rumor)) ] for rumorName in rumorsName: rumorPath = os.path.join(rnrPath, rumorName) # print("rumorPath ", rumorPath) reactionPath = os.path.join(rumorPath, "reactions") # print("reactionPath ", reactionPath) sourceTweetsPath = os.path.join(rumorPath, "source-tweets") # print("sourceTweetsPath ", sourceTweetsPath) reactions = [ reaction for reaction in os.listdir(reactionPath) if os.path.isfile(os.path.join(reactionPath, reaction)) ] numReaction = len(reactions) for sourceTweet in os.listdir(sourceTweetsPath): sourceTweetPath = os.path.join(sourceTweetsPath, sourceTweet) # print("sourceTweetPath ", sourceTweetPath) # avoid .DS_store if os.path.isfile( sourceTweetPath) and sourceTweet[0] != ".": # print("sourceTweetPath ", sourceTweetPath) data = self.loadJson(sourceTweetPath) idx = data['id_str'] hashtags = [ '#' + hashtag['text'] for hashtag in data['entities']['hashtags'] ] rumor = True if rnrName == 'rumours' else False date = data['created_at'] resultDict[idx]['text'] = data['text'] resultDict[idx]['favorite_count'] = data[ 'favorite_count'] resultDict[idx]['retweet_count'] = data[ 'retweet_count'] resultDict[idx]['hashtags'] = hashtags resultDict[idx]['comment'] = numReaction resultDict[idx]['rumor'] = rumor resultDict[idx]['date'] = date tweet = Twitter.Tweet() tweet.id = idx tweet.text = data['text'] tweet.favorites = data['favorite_count'] tweet.retweets = data['retweet_count'] tweet.reply = numReaction tweet.rumor = rumor tweet.setHashtags(hashtags) tweet.setDate(date) resultPickle.append(tweet) self.dumpJson( folderpath + "/../../pheme_dataset/" + foldername + "/final/rawData", "raw_data.json", resultDict) print("raw_data.json has been saved.") self.dumpPickle( folderpath + "/../../pheme_dataset/" + foldername + "/final/rawData", "tweets.pkl", resultPickle) print("tweets.pkl has been saved.")