def test(request): context = {} client = IODClient(APIURL, APIKEY) r=client.post('analyzesentiment',{'text':'I like cats'}) analyzesentiment=r.json() sentiment = analyzesentiment['aggregate']['sentiment'] context['sentiment']=analyzesentiment hightlight_sentiment = '' for word in analyzesentiment[sentiment]: hightlight_sentiment += '{},'.format(word['topic']) print hightlight_sentiment + " here" r=client.post('highlighttext',{'text':'I like cats', 'highlight_expression':'{}'.format(hightlight_sentiment), 'start_tag':'<b>', 'end_tag':'</b>', }) context['highlight']=r.json()['text'] index = client.getIndex('mailsift') doc1={'reference':'doc1','title':'title1','content':'this is my content'} doc2={'reference':'doc2','title':'title2','content':'this is another content'} doc3={'reference':'doc3','title':'title2','content':'this is another content'} doc4={'reference':'doc2','title':'titleNew','content':'this is another content alksdjflkjasdfkljaslkdf'} docs = [doc1, doc2, doc3, doc4] index.addDocs([doc1, doc2, doc3, doc4]) for doc in docs: index.pushDoc(doc) print index.size() index.commit() print index.size() return render(request, 'parse/test.html', context)
def generate_tags(request, feedback_pk): context = {} client = IODClient(APIURL, APIKEY) feedback_email = Email.objects.get(pk=feedback_pk) index = client.getIndex('mailsift') r = client.post('findsimilar', {'text':feedback_email.message, 'indexes':'mailsift'}) similar_feedback = r.json() if len(similar_feedback['documents']) > 0: similar_email = Email.objects.get(pk=similar_feedback['documents'][0]['reference']) feedback_email.tags = similar_email.tags else: feedback_email.tags = [''] feedback_email.save() doc1={'reference':feedback_email.pk,'title':feedback_email.subject, 'content':feedback_email.message} docs = [doc1] index.addDocs([doc1]) for doc in docs: index.pushDoc(doc) print index.size() index.commit() print index.size() context['test'] = feedback_email return render(request, 'parse/test.html', context)
def generate_highlight(request, feedback_pk): context = {} client = IODClient(APIURL, APIKEY) feedback_email = Email.objects.get(pk=feedback_pk) r=client.post('analyzesentiment',{'text':feedback_email.message}) analyzesentiment=r.json() sentiment = analyzesentiment['aggregate']['sentiment'] context['sentiment']=analyzesentiment feedback_email.sentiment = 50 + 50 * analyzesentiment['aggregate']['score'] hightlight_sentiment = '' for word in analyzesentiment[sentiment]: hightlight_sentiment += '{},'.format(word['topic']) r=client.post('highlighttext',{'text':feedback_email.message, 'highlight_expression':'{}'.format(hightlight_sentiment), 'start_tag':'<b>', 'end_tag':'</b>', }) feedback_email.content=r.json()['text'] feedback_email.priority = feedback_email.sentiment + 40 feedback_email.save() context['test'] = feedback_email return render(request, 'parse/test.html', context)
def call(answers,count2): #Call client from IOD and pass in API key client = IODClient('http://api.idolondemand.com', '#*APIKEY*#') text = answers #set data to text dictionary data = {'text':text} #tell the client what API to call r = client.post('analyzesentiment', data) #Return information from IOD sentiment = r.json()['aggregate']['sentiment'] score = r.json()['aggregate']['score'] print "********************************",'\n' data = r.json() count1 = 0 #Set lists to hold topics global n_topics global p_topics n_topics = [] p_topics = [] for i in (data['negative']): #If statment to check is there is a negative topic if 'negative' not in data: print('negative key is missing') elif len(data['negative']) == 0: print('no items in the negative list') elif 'topic' not in data['negative'][count2]: print('topic is missing') elif data['negative'][count2]['topic'] == '': print('topic is empty') else: #what to return from the negative topic print "Text:", "'"+text+"'" print "Sentiment: negative " print "Topic:" , (data['negative'][count2]['topic']) print "Score:", (data['negative'][count2]['score']) n_topics.append(str(data['negative'][count2]['topic'])) if count2 == 1: print "negative topic" break logging.warning('Watch out!') print '\n',"********************************" print '\n',"Negative topics:",n_topics print '\n',"********************************", '\n' #If statment to check is there is a positive topic for i in (data['positive']): if 'positive' not in data: print('positive key is missing') elif len(data['positive']) == 0: print('no items in the positive list') elif 'topic' not in data['positive'][count1]: print('topic is missing') elif data['positive'][count1]['topic'] == '': print('topic is empty') else: #what to return from the positive topic print "Text:", "'"+text+"'" print "Sentiment: positive " print "Topic:" , (data['positive'][count1]['topic']) print "Score:", (data['positive'][count1]['score']) p_topics.append(str(data['positive'][count1]['topic'])) count1 += 1 print '\n',"********************************",'\n' print "Positive topics:",p_topics,'\n' # print statments to print the aggregate for the data_set #print "Aggregate Result" #print "sentiment: ", sentiment + '\n',"score: ", score , '\n' print "********************************"
import time import requests import collections from alchemyapi import AlchemyAPI from iodpython.iodindex import IODClient import os import sys reload(sys) sys.setdefaultencoding('utf-8') ALCHEMYAPI_KEY = os.environ["DOSSIER_ALCHEMY_KEY"] ALCHEMY_RELEVANCE_THRESHOLD = 0.7 alchemyapi = AlchemyAPI() client = IODClient("http://api.idolondemand.com/", os.environ["DOSSIER_IDOL_KEY"]) index = client.getIndex("conversations") cardIndex = client.getIndex("cards") #index a conversation def dossierConversation(transcript): information = extractInformation(transcript) if "name" in information: title = "Conversation with " + information["name"] addCardToIndex(information) else: information["name"] = "" title = "Conversation"