def make_interrogative(word): if settings.option('general', 'verboseLogging'): print "Generating an interrogative phrase for \'%s\'..." % word interrogativeDomains = [ [u"what\'s", u"=WORD"] ] if pluralizeObjects: interrogativeDomains.append( [u"what", u"are", u"=WORD"] ) else: interrogativeDomains.extend([ [u"what", u"is", u"=WORD"], [u"what", u"is", u"a", u"=WORD"] ]) domain = random.choice(interrogativeDomains) if settings.option('general', 'verboseLogging'): print "Building interrogative phrase..." sentence = [] for count, slot in enumerate(domain): print sentence + domain[count:] if slot == u"=WORD": if pluralizeObjects: sentence.append(pattern.en.pluralize(word)) else: sentence.append(word) else: sentence.append(slot) return sentence
def make_imperative(associationGroup): if settings.option('general', 'verboseLogging'): print "Generating an imperative statement for \'%s\'..." % associationGroup['word'] verbAssociations = [] for association in associationGroup['associations']: if association['type'] == "HAS-ABILITY-TO": verbAssociations.append(association) imperativeDomains = [ [u"=VERB", u"=PHRASE"] ] if mood > 0: imperativeDomains.append( [u"always", u"=VERB", u"=PHRASE"] ) else: imperativeDomains.append( [u"never", u"=VERB", u"=PHRASE"] ) if not pluralizeObjects: imperativeDomains.append( [u"=VERB", u"=PHRASE"] ) # todo: new domain: VERB a/an/the OBJECT with (its THING OBJECT HAS / a/an/the OTHER OBJECT) domain = random.choice(imperativeDomains) if settings.option('general', 'verboseLogging'): print "Building imperative statement..." sentence = [] if mood > 0.4: if random.randint(0, 1) == 0: sentence = [u"please"] for count, slot in enumerate(domain): print sentence + domain[count:] if slot == "=PHRASE": sentence.extend(make_phrase(associationGroup)) elif slot == "=VERB": sentence.append(choose_association(verbAssociations)['target']) else: sentence.append(slot) return sentence
def make_declarative(associationGroup): if settings.option('general', 'verboseLogging'): print "Generating a declarative statement for \'%s\'..." % associationGroup['word'] # Gather information about what associations we have to help us decide what domains we're allowed to use hasAssociations = [] isaAssociations = [] haspropertyAssociations = [] hasabilitytoAssociations = [] for association in associationGroup['associations']: if association['type'] == "HAS": hasAssociations.append(association) if association['type'] == "IS-A": isaAssociations.append(association) if association['type'] == "HAS-PROPERTY": haspropertyAssociations.append(association) if association['type'] == "HAS-ABILITY-TO": hasabilitytoAssociations.append(association) declarativeDomains = [] if haspropertyAssociations != []: declarativeDomains.append( [u"=PHRASE", u"=ISARE", u"=ADJECTIVE"] ) if len(haspropertyAssociations) > 1: declarativeDomains.append( [u"=PHRASE", u"=ISARE", u"=ADJECTIVE", u"and", u"=ADJECTIVE"] ) if hasabilitytoAssociations != []: declarativeDomains.extend([ [u"=PHRASE", u"=VERB"], [u"=PHRASE", u"can", u"=VERB"] ]) if hasAssociations != []: declarativeDomains.append( [u"=PHRASE", u"=HAVEHAS", u"=OBJ-HAS"] ) if isaAssociations != []: declarativeDomains.append( [u"=PHRASE", u"=ISARE", u"=OBJ-IS-A"] ) domain = random.choice(declarativeDomains) if settings.option('general', 'verboseLogging'): print "Building declarative statement..." sentence = [] # Iterate through the objects in the domain and fill them in to create the declarative statement for count, slot in enumerate(domain): print sentence + domain[count:] if slot == u"=PHRASE": sentence.extend(make_phrase(associationGroup)) elif slot == u"=ADJECTIVE": sentence.append(choose_association(haspropertyAssociations)['target']) elif slot == u"=VERB": sentence.append(choose_association(hasabilitytoAssociations)['target']) #todo: add "how" ("the snake moved (how?) quickly") elif slot == u"=OBJ-HAS": sentence.append(choose_association(hasAssociations)['target']) elif slot == u"=OBJ-IS-A": sentence.append(choose_association(isaAssociations)['target']) elif slot == u"=ISARE": if pluralizeObjects: sentence.append(u"are") else: sentence.append(u"is") elif slot == u"=HAVEHAS": if pluralizeObjects: sentence.append(u"have") else: sentence.append(u"has") else: sentence.append(slot) return sentence
def make_greeting(asker): if settings.option('general', 'verboseLogging'): print "Generating a greeting..." greetingDomains = [ [u"Hi", asker], [u"Hello", asker] ] return random.choice(greetingDomains)
def get_mood(update=False, text="", expressAsText=True): global moodHistory # If update is set to true, use text to add new mood value. Otherwise, just return the mood without touching it # By default, this function does nothing and just returns Emma's mood in human-readable form (as opposed to numbers) if update == True: sentiment = pattern.en.sentiment(text) # Get the average mood from the moods of sentences in the text lpush(moodHistory, (sum(sentiment) / float(len(sentiment)))) # Add the mood to the list of mood values with open('moodHistory.p','wb') as moodFile: pickle.dump(moodHistory, moodFile) # Save to mood values file else: with open('moodHistory.p', 'r') as moodFile: moodHistory = pickle.load(moodFile) # More recent mood values have a higher weight when calculating Emma's overall mood weightedmoodHistory = [] for i in range(0, 3): weightedmoodHistory.append(moodHistory[0]) for i in range(0, 2): weightedmoodHistory.append(moodHistory[1]) weightedmoodHistory.append(moodHistory[2]) weightedmoodHistory = weightedmoodHistory + moodHistory mood = sum(weightedmoodHistory) / float(len(weightedmoodHistory)) if settings.option('general', 'verboseLogging'): print Fore.MAGENTA + "Mood values: %s\nCalculated mood: %s" % (str(moodHistory), str(mood)) if not expressAsText: return mood else: if -0.8 > mood: moodStr = u"abysmal \ud83d\ude31" elif -0.6 > mood >= -0.8: moodStr = u"dreadful \ud83d\ude16" elif -0.4 > mood >= -0.6: moodStr = u"bad \ud83d\ude23" elif -0.2 > mood >= -0.4: moodStr = u"crummy \ud83d\ude41" elif 0.0 > mood >= -0.2: moodStr = u"blah \ud83d\ude15" elif 0.2 > mood >= 0.0: moodStr = u"alright \ud83d\ude10" elif 0.4 > mood >= 0.2: moodStr = u"good \ud83d\ude42" elif 0.6 > mood >= 0.4: moodStr = u"great \ud83d\ude09" elif 0.8 > mood >= 0.6: moodStr = u"fantastic \ud83d\ude00" elif mood >= 0.8: moodStr = u"glorious \ud83d\ude1c" return u"feeling " + moodStr
def tokenize(text): if text[-1] not in [u"!", u"?", "."]: text += u"." text = translate_netspeak(text) print "Tokenizing message..." if settings.option('general', 'verboseLogging'): pattern.en.pprint(pattern.en.parse(text, True, True, True, True, True)) taggedText = pattern.en.parse(text, True, True, True, True, True).split() parsedMessage = [] for count, taggedSentence in enumerate(taggedText): finalize_sentence(taggedSentence) posSentence = [] chunkSeries = [] lemmaSentence = [] subObj =[] for taggedWord in taggedSentence: posSentence.append(taggedWord[1]) chunkSeries.append(taggedWord[2]) lemmaSentence.append(taggedWord[5]) subObj.append(taggedWord[4]) parsedSentence = zip(lemmaSentence, posSentence, chunkSeries, subObj) for count, word in enumerate(parsedSentence): parsedSentence[count] = list(word) parsedMessage.append(parsedSentence) return parsedMessage
def read_question(sentence): questionType = "" if sentence[0][0] == u"what" and sentence[2][0] == u"be": # WHAT color BE the sky interrogativeProperty = sentence[1][0] # "color" for word in sentence[::-1]: # SKY the be color what if word[1] in utilities.nounCodes: interrogativeObject = word[0] # "sky" if settings.option('general', 'verboseLogging'): print Fore.GREEN + "Interrogative: WHAT is " + interrogativeProperty + " of " + interrogativeObject return (["what", interrogativeProperty, interrogativeObject]) elif sentence[0][0] in [u"do", u"does"] and sentence[2][0] == u"have": # DO dog HAVE paw" interrogativeProperty = sentence[1][0] # "dog" for word in sentence[::-1]: # PAW have dog do if word[1] in utilities.nounCodes: interrogativeObject = word[0] if settings.option('general', 'verboseLogging'): print Fore.GREEN + "Interrogative: DO " + interrogativeProperty + " HAVE " + interrogativeObject return (["doXhaveY", interrogativeProperty, interrogativeObject]) else: return None
def make_comparative(associationGroup, comparisonGroup): if settings.option('general', 'verboseLogging'): print "Generating a comparative statement for \'%s\' and \'%s\'..." % (associationGroup['word'], comparisonGroup['word']) comparativeDomains = [ [u"=DECLARATIVE", u"like", u"=COMPARISON"], [u"=DECLARATIVE", u",", u"and", u"=COMPARISON"], [u"=DECLARATIVE", u",", u"but", u"=COMPARISON"] ] domain = random.choice(comparativeDomains) if settings.option('general', 'verboseLogging'): print "Building comparative statement..." sentence = [] for count, slot in enumerate(domain): print sentence + domain[count:] if slot == u"=DECLARATIVE": sentence.extend(make_declarative(associationGroup)) elif slot == u"=COMPARISON": sentence.extend(make_declarative(comparisonGroup)) else: sentence.append(slot) return sentence
def run_emma(): # If we aren't in chat mode, every 15 minutes, try to make a post. Replying to asks is most likely, followed by dreams, and reblogging a post is the least likely if settings.option('general', 'enableChatMode'): emma.chat() else: if settings.option('tumblr', 'fetchRealAsks'): askList = tumblrclient.get_asks() else: askList = utilities.fakeAsks print "Choosing activity..." activities = [] if settings.option('tumblr', 'enableReblogs'): activities.append('reblogPost') if settings.option('tumblr', 'enableDreams'): activities.extend(['dream'] * 2) if settings.option('tumblr', 'enableAskReplies') and askList != []: activities.extend(['replyToAsks'] * 3) activity = random.choice(activities) if activity == 'reblogPost': print "Reblogging a post..." emma.reblog_post() elif activity == 'dream': print "Dreaming..." emma.dream() elif activity == 'replyToAsks': print "Fetched %d new asks. Responding the newest one..." % len(askList) # todo: maybe have Emma figure out if she's able to respond to an ask before calling reply_to_asks()? emma.reply_to_ask(askList[0]) if settings.option('general', 'enableSleep'): print "Sleeping for 15 minutes..." time.sleep(900)
def make_phrase(associationGroup): if settings.option('general', 'verboseLogging'): print "Generating a phrase for \'%s\'..." % associationGroup['word'] if settings.option('general', 'verboseLogging'): print "Looking for adjective associations..." adjectiveAssociations = [] for association in associationGroup['associations']: if association['type'] == "HAS-PROPERTY": adjectiveAssociations.append(association) phraseDomains = [ [u"=OBJECT"] ] if len(adjectiveAssociations) >= 1: phraseDomains.append( [u"=ADJECTIVE", u"=OBJECT"] ) if len(adjectiveAssociations) > 1: phraseDomains.append( [u"=ADJECTIVE", u"=ADJECTIVE", u"=OBJECT"] ) domain = random.choice(phraseDomains) if settings.option('general', 'verboseLogging'): print "Building phrase..." # Decide if we want to precede the phrase with a determiner ("the", "a") if random.randint(0, 1) == 0: determiners = [[u"the"]] if pluralizeObjects: determiners.extend([[u"some"], [u"many"]]) else: determiners.append([u"a"]) sentence = random.choice(determiners) else: sentence = [] # Iterate through the objects in the domain and fill them in to create the phrase for count, slot in enumerate(domain): print sentence + domain[count:] if slot == u"=OBJECT": if pluralizeObjects: sentence.append(pattern.en.pluralize(associationGroup['word'])) else: sentence.append(associationGroup['word']) elif slot == u"=ADJECTIVE": sentence.append(choose_association(adjectiveAssociations)['target']) return sentence
def latest_news_google_news_ru(): apikey = option("newsapi_apikey") url = "http://newsapi.org/v2/top-headlines?sources=google-news-ru&apiKey=%s" % apikey resp = requests.get(url=url) if resp.status_code != 200: return [] # print (__file__, resp.text) rjson = resp.json() print(f'{__file__} {__name__} ns_resp {get_pretty_json_string(rjson)}') if "articles" in rjson: arts = rjson["articles"] if arts is None: return [] return arts return []
def reply_to_ask(ask): print "Reading ask..." print Fore.BLUE + u"@" + ask['asker'] + u" >> " + ask['message'] parsedAsk = parse.tokenize(ask['message']) intents, questionPackages = consume(parsedAsk, ask['asker']) understanding = utilities.pretty_print_understanding(parsedAsk, intents) reply = sentencebuilder.generate_sentence(parsedAsk, get_mood(update=True, text=ask['message'], expressAsText=False), intents, ask['asker'], questionPackages) if "%" not in reply: print Fore.BLUE + u"emma >> %s" % reply print "Posting reply..." if settings.option('tumblr', 'enablePostPreview'): print Fore.BLUE + "\n\nTUMBLR POST PREVIEW\n\n" + Fore.RESET + "@" + ask['asker'] + " >> " + ask['message'] + "\n\n" + "emma >> " + reply + "\n- - - - - - - - - - -\n" + get_mood(update=False, expressAsText=True) + "\n\n" body = "<a href=" + ask['asker'] + ".tumblr.com/>@" + ask['asker'] + "</a>" + cgi.escape(" >> ") + cgi.escape(ask['message']) + "\n\n" + cgi.escape("emma >> ") + cgi.escape(reply) + "\n<!-- more -->\n" + cgi.escape(understanding) tumblrclient.post(body.encode('utf-8'), ["dialogue", ask['asker'].encode('utf-8'), get_mood().encode('utf-8')]) else: print Fore.RED + "Reply generation failed." tumblrclient.delete_ask(ask['id'])
def make_answer(answer): print answer answerDomains = [] if answer[0] == "what": answerDomains = [ [u"The", answer[1], u"of", u"the", answer[2], u"is", answer[3]] ] elif answer[0] == "does": if answer[3]: answerDomains = [ [u"I", u"think", u"so", u",", u"yes"], [answer[1], u"have", answer[2]], [u"Yes", answer[1], u"have", answer[2]] ] else: answerDomains = [ [u"I", u"don\'t", u"think", u"so"], [answer[1], u"do", u"not", u"have", answer[2]], [u"no", answer[1], u"don\'t", u"have", answer[2]] ] domain = random.choice(answerDomains) if settings.option('general', 'verboseLogging'): print "Building an answer..." sentence = domain return sentence
The fact that you are presently reading this means that you have had knowledge of the CeCILL-C license and that you accept its terms. """ import re import os import sys import commands import optparse import subprocess from django.utils.encoding import (smart_str, smart_unicode) sys.path.append("app/") from events import (make_help, global_error, bad_source, make_success) from settings import option (folder, thumb, tag, team, announce, tmdb_api_key, tag_thumb) = option() def main(): # HELP usage = make_help() parser = optparse.OptionParser(usage=usage) (options, args) = parser.parse_args() if (len(args) != 5 and len(args) != 11): parser.print_help() parser.exit(1) source = sys.argv[1] # MANUAL TOOLS VALUES
import os import sys import shutil import glob import Image import ImageDraw import ImageFont import re import time import optparse sys.path.append("app/") from settings import option from style import color (folder, thumb, tag, team, announce, tmdb_api_key, tag_thumb) = option() (BLUE, RED, YELLOW, GREEN, END) = color() def snapshot(path, nb_lgn, nb_col): buff=os.popen('mplayer -identify -frames 0 '+path+\ ' 2>/dev/null| grep ID_') infos = buff.read() buff.close() longueur = int(re.findall('ID_LENGTH=([0-9]*)', infos)[0]) - 300 os.mkdir(os.path.expanduser(thumb) + 'rtemp') interval = int(longueur / (int(nb_lgn) * int(nb_col) + 1)) width = int(re.findall('ID_VIDEO_WIDTH=([0-9]*)', infos)[0])
generalLabel = make_label("General", color=grey, x=20, y=15) enableChatModeBox = CheckBox(x=20, y=generalLabel.bottom, title="Chat mode", action=(update_setting, 'enableChatMode')) enableSleepBox = CheckBox(x=20, y=enableChatModeBox.bottom, title="Enable sleep", action=(update_setting, 'enableSleep')) verboseLoggingBox = CheckBox(x=20, y=enableSleepBox.bottom, title="Verbose Logging", action=(update_setting, 'verboseLogging')) # Tumblr tumblrLabel = make_label("Tumblr", color=grey, x=20, y=verboseLoggingBox.bottom+15) publishOutputBox = CheckBox(x=20, y=tumblrLabel.bottom, title="Publish output", action=(update_setting, 'publishOutput')) enablePostPreviewBox = CheckBox(x=20, y=publishOutputBox.bottom, title="Show post preview", action=(update_setting, 'enablePostPreview')) enableAskRepliesBox = CheckBox(x=20, y=enablePostPreviewBox.bottom + 10, title="Enable Ask replies", action=(update_setting, 'enableAskReplies')) enableAskDeletionBox = CheckBox(x=20, y=enableAskRepliesBox.bottom, title="Enable Ask deletion", action=(update_setting, 'enableAskDeletion')) fetchRealAsksBox = CheckBox(x=20, y=enableAskDeletionBox.bottom, title="Fetch real Asks", action=(update_setting, 'fetchRealAsks')) enableReblogsBox = CheckBox(x=20, y=fetchRealAsksBox.bottom + 10, title="Enable Reblogs", action=(update_setting, 'enableReblogs')) enableDreamsBox = CheckBox(x=20, y=enableReblogsBox.bottom, title="Enable dreams", action=(update_setting, 'enableDreams')) if settings.option('general', 'enableChatMode'): enableChatModeBox.on = True if settings.option('general', 'enableSleep'): enableSleepBox.on = True if settings.option('general', 'verboseLogging'): verboseLoggingBox.on = True if settings.option('tumblr', 'publishOutput'): publishOutputBox.on = True if settings.option('tumblr', 'enablePostPreview'): enablePostPreviewBox.on = True if settings.option('tumblr', 'enableAskReplies'): enableAskRepliesBox.on = True if settings.option('tumblr', 'enableAskDeletion'): enableAskDeletionBox.on = True if settings.option('tumblr', 'fetchRealAsks'): fetchRealAsksBox.on = True if settings.option('tumblr', 'enableReblogs'): enableReblogsBox.on = True if settings.option('tumblr', 'enableDreams'): enableDreamsBox.on = True loopButton = Button(x=15, y=enableDreamsBox.bottom + 15, width=170, title="Start Emma Loop", style='default', action=loop_emma) runOnceButton = Button(x=15, y=loopButton.bottom + 5, width=170, title="Run Emma Once", style='normal', action=run_emma) win = Window(width=200, height=runOnceButton.bottom + 20, title="Emma", auto_position=True, resizable=False, zoomable=False)
def reblog(postid, reblogKey, comment, tags): print "Reblogging post & adding comment..." if settings.option('tumblr', 'publishOutput'): client.reblog('emmacanlearn', id=postid, reblog_key=reblogKey, comment=cgi.escape(comment), tags=tags)
def post(body, tags=[]): if settings.option('tumblr', 'enablePostPreview'): tagsAsString = "" for tag in tags: tagsAsString += "#%s " % tag if settings.option('tumblr', 'publishOutput'): client.create_text('emmacanlearn', state="published", body=body, tags=tags)
def delete_ask(askid): if settings.option('tumblr', 'enableAskDeletion'): print "Deleting ask with ID %d..." % askid client.delete_post('emmacanlearn', askid)