def main(params): try: msg = InputMessage.from_params(params) text = msg.payload.get('text') return OutputMessage.create().with_type('cortex/Text').with_payload({'text': 'Echo: '+ text}).to_params() except: return OutputMessage.create().with_type('cortex/Text').with_payload({'text': 'error: ' + params}).to_params()
def main(params): msg = InputMessage.from_params(params) text = msg.payload.get('text') return OutputMessage.create().with_type('cortex/Text').with_payload({ 'text': 'Got: ' + text }).to_params()
def main(params): # Parse the function params msg = InputMessage.from_params(params) # Get text and payload text = msg.payload.get('text') # Compute and create output return OutputMessage.create().with_type('cortex/Text').with_payload({ 'text': text }).to_params()
def main(params): try: cortex_params = InputMessage.from_params(params) profile_key = "newsAgent/profiles/{}.json".format( cortex_params.payload.get("profileId")) # Download users current profile attributes (if it exists ...) profileAttributes = safe_load_list_of_jsons_from_managed_content( cortex_params, profile_key) eprint(profileAttributes) user_keyword_affinities = determine_user_keyword_affinity_score( profileAttributes) # List the users keywords sorted userKeywordLikes = [{ "keyword": y[0], "score": y[1] } for y in sorted(user_keyword_affinities.items(), key=lambda x: -1 * x[1])] eprint(userKeywordLikes) # List the users topics sorted topics = { topic["topic"]: topic["keywords"] for topic in safe_load_list_of_jsons_from_managed_content( cortex_params, "newsAgent/datasets/topics.json") } eprint(topics) # Determine User Topic Affinities ... userTopicLikes = [{ "topic": topic, "score": joint_score_of_affinities(user_keyword_affinities, keywords) } for topic, keywords in topics.items()] eprint(userTopicLikes) return OutputMessage.create().with_payload({ "profileId": cortex_params.payload.get("profileId"), "profileAttributes": { "userAffinityTowardsInsightsWithKeywords": userKeywordLikes, "userAffinityTowardsInsightsAboutTopics": userTopicLikes, } }).to_params() except Exception as e: return OutputMessage.create().with_payload({ 'message': str(e) }).to_params()
def main(params): msg = InputMessage.from_params(params) fake = Faker() name_of_movie = msg.payload.get('name_of_movie') movie = { 'name_of_movie': name_of_movie, 'lead_1': fake.name(), 'lead_2': fake.name(), 'support_1': fake.name(), 'support_2': fake.name() } return OutputMessage.create().with_type('cortex/Text').with_payload({ 'movie': movie }).to_params()
def main(params): # Parse the function params msg = InputMessage.from_params(params) # Get source and n source = msg.payload.get('source') n = msg.payload.get('n') # get api token from properties api = msg.properties.get('token') # get the top headlines using the news api top_head = news(source.lower(), n, api) # Compute and create output return OutputMessage.create().with_payload({'route':'headline','top_head': top_head,'api':api}).to_params()
def main(params): datasetReferences = get_dataset_refs(params) parsedArgs = { NAME_COMPANIES_REF: datasetReferences.get(NAME_COMPANIES_REF), "api_endpoint": params.get("apiEndpoint") } jwt = params.get("token") datasetStream = read_dataset(parsedArgs, jwt, parsedArgs[NAME_COMPANIES_REF]) msg = InputMessage.from_params(params) text = msg.payload.get('text') return OutputMessage.create().with_payload({ 'data': datasetStream }).to_params()
def main(params): try: cortex_params = InputMessage.from_params(params) # Download profile attributes (if it exists ...) profileAttributes = safe_load_list_of_jsons_from_managed_content(cortex_params, "newsAgent/profiles/{}.json".format(cortex_params.payload.get("profileId"))) # eprint(profileAttributes) # Download news articles to present ... artilces = safe_load_list_of_jsons_from_managed_content(cortex_params, "newsAgent/datasets/newsArticles.json") # eprint(artilces) # if the user does not have a profile, return 10 random artilces ... if not profileAttributes: scored_articles = [(0, x) for x in random.sample(artilces, 10)] eprint("No Profile for User...") # Here on out, we are assuming the user has a profile ... else: # Get User Affinity Score Per Keyword user_keyword_affinities = determine_user_keyword_affinity_score(profileAttributes) # eprint(user_keyword_affinities) # Score Each Article Against Users Afinnity Scores & Sort Them... scored_articles = list(sorted( [score_article(a, user_keyword_affinities) for a in artilces], key=lambda x: -1 * x[0] )) eprint(scored_articles) # Take the top 3 Articles ... insights = turn_scored_articles_into_insights( cortex_params.payload.get("profileId"), scored_articles[:3] ) # - [ ] TODO: Download Insights File and Append Newly Generated Insights to it eprint(insights) return OutputMessage.create().with_payload({ "profileId": cortex_params.payload.get("profileId"), "insights": insights }).to_params() except Exception as e: return OutputMessage.create().with_payload({'message': str(e)}).to_params()
def main(params): # Parse the function params msg = InputMessage.from_params(params) # Get input parameters source = msg.payload.get('source') category = msg.payload.get('category') country = msg.payload.get('country') query = msg.payload.get('query') # Get properties batch_size = msg.properties.get('batch_size') batch_no = msg.properties.get('batch_no') api_token = msg.properties.get('api_token') filter_type = msg.properties.get('filter_type') # get an API response response = \ filter_news(country, category, source, query, batch_size, batch_no, api_token, filter_type) # convert response to strings url, status, results, error_code, error_message = (str(x) for x in response) # return the status and results if no error if error_code == "None": return OutputMessage.create().with_payload({ 'status': status + ": " + url, 'results': results }).to_params() # return error code and message if error else: return OutputMessage.create().with_payload({ 'status': status + ": " + url, 'error_code': error_code, 'error_message': error_message }).to_params()
def main(params): try: cortex_params = InputMessage.from_params(params) profile_key = "newsAgent/profiles/{}.json".format( cortex_params.payload.get("profileId")) # Download users current profile attributes (if it exists ...) profileAttributes = safe_load_list_of_jsons_from_managed_content( cortex_params, profile_key) eprint(profileAttributes) userKeywordLikes = Counter( determine_user_keyword_affinity_score(profileAttributes)) eprint(userKeywordLikes) keywordsOfInsightsLiked = Counter( list( itertools.chain.from_iterable([ set(x["content"]["keywords"]) for x in cortex_params.payload.get("insightsLiked", []) ]))) eprint(keywordsOfInsightsLiked) updatedKeywordLikeCounts = userKeywordLikes + keywordsOfInsightsLiked new_profile_attributes = recreate_keyword_based_profile_attributes( cortex_params.payload.get("profileId"), updatedKeywordLikeCounts) eprint(new_profile_attributes) safe_save_list_of_jsons_to_managed_content(cortex_params, profile_key, new_profile_attributes) return OutputMessage.create().with_payload({ "profileId": cortex_params.payload.get("profileId") }).to_params() except Exception as e: return OutputMessage.create().with_payload({ 'message': str(e) }).to_params()