def watsonTone()

in src/plugins/utils/tone.py [0:0]


def watsonTone(KibbleBit, bodies):
    """ Sentiment analysis using IBM Watson """
    if 'watson' in KibbleBit.config:
        headers = {
            'Content-Type': 'application/json'
        }
        
        # Crop out quotes
        for body in bodies:
            lines = body.split("\n")
            body = "\n".join([x for x in lines if not x.startswith(">")])
            
            js = {
                'text': body
            }
            try:
                rv = requests.post(
                    "%s/v3/tone?version=2017-09-21&sentences=false" % KibbleBit.config['watson']['api'],
                    headers = headers,
                    data = json.dumps(js),
                    auth = (KibbleBit.config['watson']['username'], KibbleBit.config['watson']['password'])
                )
                jsout = rv.json()
            except:
                jsout = {} # borked Watson?
            mood = {}
            if 'document_tone' in jsout:
                for tone in jsout['document_tone']['tones']:
                    mood[tone['tone_id']] = tone['score']
            else:
                KibbleBit.pprint("Failed to analyze email body.")
            yield mood