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_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 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)
class SentimentAnalyser: def __init__(self): self.client = IODClient("http://api.idolondemand.com/","07a106d0-ff07-496b-a1b5-288b752da744") self.hashtags = {"#jeb": "Jeb Bush", "#clinton": "Hillary Clinton", "#trump": "Donald Trump", "#hilary": "Hillary Clinton", "#berniesanders": "Bernie Sanders"} def generateSentiment(self, text): r = self.client.post('analyzesentiment',{'text':text}) docs = r.json() if(docs['aggregate']['score'] != 0): dictJson = {"candidate": self.getCandidate(text)} if docs['positive'] != []: for doc in docs['positive']: dictJson[doc['topic']] = doc['score'] if docs['negative'] != []: for doc in docs['negative']: dictJson[doc['topic']] = doc['score'] return dictJson def getCandidate(self, tweet): try: x = next(value for key, value in self.hashtags.iteritems() if key in tweet) except StopIteration: x = "Jeb Bush" return x
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 "********************************"
GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) ## initialize print "Initializing..." GPIO.output(LED1_PIN, True) GPIO.output(LED2_PIN, True) time.sleep(2) GPIO.output(LED1_PIN, False) GPIO.output(LED2_PIN, False) time.sleep(2) ## ## repeat over and over again while True: camera.capture("image1.jpg") r = client.post("detectfaces", files={"file": open("./image1.jpg", "rb")}) print "------------" print "POST to IDOL" if len(r.json()["face"]) > 0: # is there a face in the image print "Face(s) detected" GPIO.output(LED1_PIN, True) # turn light on input_state = GPIO.input(BUTTON_PIN) if input_state == False: # i.e. the button was pushed print ("Button pressed") print ("Saving to S3") GPIO.output(LED2_PIN, True) k = Key(b) k.key = "image1_s3.jpg" # this is what you want to name it in S3 k.set_contents_from_filename("image1.jpg") # this is name of the lcoal file k.set_acl("public-read") time.sleep(2) # make sure it actually gets up there