Search in sources :

Example 6 with ToneScore

use of com.ibm.watson.tone_analyzer.v3.model.ToneScore in project java-sdk by watson-developer-cloud.

the class ToneDetection method updateLanguageTone.

/**
 * updateLanguageTone updates the user with the language tones interpreted based on the specified
 * thresholds.
 *
 * @param user a json object representing user information (tone) to be used in conversing with
 *     the Assistant Service
 * @param languageTone a json object containing the language tones in the payload returned by the
 *     Tone Analyzer
 */
@SuppressWarnings("unchecked")
private static void updateLanguageTone(Map<String, Object> user, List<ToneScore> languageTone, boolean maintainHistory) {
    List<String> currentLanguage = new ArrayList<String>();
    Map<String, Object> currentLanguageObject = new HashMap<String, Object>();
    // Process each language tone and determine if it is high or low
    for (ToneScore tone : languageTone) {
        if (tone.getScore() >= LANGUAGE_HIGH_SCORE_THRESHOLD) {
            currentLanguage.add(tone.getToneName().toLowerCase() + "_high");
            currentLanguageObject.put("tone_name", tone.getToneName().toLowerCase());
            currentLanguageObject.put("score", tone.getScore());
            currentLanguageObject.put("interpretation", "likely high");
        } else if (tone.getScore() <= LANGUAGE_NO_SCORE_THRESHOLD) {
            currentLanguageObject.put("tone_name", tone.getToneName().toLowerCase());
            currentLanguageObject.put("score", tone.getScore());
            currentLanguageObject.put("interpretation", "no evidence");
        } else {
            currentLanguageObject.put("tone_name", tone.getToneName().toLowerCase());
            currentLanguageObject.put("score", tone.getScore());
            currentLanguageObject.put("interpretation", "likely medium");
        }
    }
    // update user language tone
    Map<String, Object> language = (Map<String, Object>) ((Map<String, Object>) user.get("tone")).get("language");
    // the current language pulled from tone
    language.put("current", currentLanguage);
    // if history needs to be maintained
    if (maintainHistory) {
        List<Map<String, Object>> history = new ArrayList<Map<String, Object>>();
        if (language.get("history") != null) {
            history = (List<Map<String, Object>>) language.get("history");
        }
        history.add(currentLanguageObject);
        language.put("history", history);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) ToneScore(com.ibm.watson.tone_analyzer.v3.model.ToneScore)

Example 7 with ToneScore

use of com.ibm.watson.tone_analyzer.v3.model.ToneScore in project java-sdk by watson-developer-cloud.

the class ToneDetection method updateUserTone.

/**
 * updateUserTone processes the Tone Analyzer payload to pull out the emotion, language and social
 * tones, and identify the meaningful tones (i.e., those tones that meet the specified
 * thresholds). The assistantPayload json object is updated to include these tones.
 *
 * @param context the context
 * @param toneAnalyzerPayload json object returned by the Watson Tone Analyzer Service
 * @param maintainHistory the maintain history
 * @return the map
 * @returns assistantPayload where the user object has been updated with tone information from the
 *     toneAnalyzerPayload
 */
public static Map<String, Object> updateUserTone(Context context, ToneAnalysis toneAnalyzerPayload, boolean maintainHistory) {
    List<ToneScore> emotionTone = new ArrayList<ToneScore>();
    List<ToneScore> languageTone = new ArrayList<ToneScore>();
    List<ToneScore> socialTone = new ArrayList<ToneScore>();
    // If the context doesn't already contain the user object, initialize it
    if (context.getSystem().containsKey("user")) {
        context.put("user", initUser());
    }
    // For convenience sake, define a variable for the user object to
    @SuppressWarnings("unchecked") Map<String, Object> user = (Map<String, Object>) context.get("user");
    if (toneAnalyzerPayload != null && toneAnalyzerPayload.getDocumentTone() != null) {
        List<ToneCategory> tones = toneAnalyzerPayload.getDocumentTone().getToneCategories();
        for (ToneCategory tone : tones) {
            if (tone.getCategoryId().equals(EMOTION_TONE_LABEL)) {
                emotionTone = tone.getTones();
            }
            if (tone.getCategoryId().equals(LANGUAGE_TONE_LABEL)) {
                languageTone = tone.getTones();
            }
            if (tone.getCategoryId().equals(SOCIAL_TONE_LABEL)) {
                socialTone = tone.getTones();
            }
        }
        updateEmotionTone(user, emotionTone, maintainHistory);
        updateLanguageTone(user, languageTone, maintainHistory);
        updateSocialTone(user, socialTone, maintainHistory);
    }
    context.put("user", user);
    return user;
}
Also used : ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) ToneScore(com.ibm.watson.tone_analyzer.v3.model.ToneScore) ToneCategory(com.ibm.watson.tone_analyzer.v3.model.ToneCategory)

Example 8 with ToneScore

use of com.ibm.watson.tone_analyzer.v3.model.ToneScore in project java-sdk by watson-developer-cloud.

the class ToneDetection method updateSocialTone.

/**
 * updateSocialTone updates the user with the social tones interpreted based on the specified
 * thresholds.
 *
 * @param user a json object representing user information (tone) to be used in conversing with
 *     the Assistant Service
 * @param socialTone a json object containing the social tones in the payload returned by the Tone
 *     Analyzer
 * @param maintainHistory the maintain history
 */
@SuppressWarnings("unchecked")
public static void updateSocialTone(Map<String, Object> user, List<ToneScore> socialTone, boolean maintainHistory) {
    List<String> currentSocial = new ArrayList<String>();
    Map<String, Object> currentSocialObject = new HashMap<String, Object>();
    for (ToneScore tone : socialTone) {
        if (tone.getScore() >= SOCIAL_HIGH_SCORE_THRESHOLD) {
            currentSocial.add(tone.getToneName().toLowerCase() + "_high");
            currentSocialObject.put("tone_name", tone.getToneName().toLowerCase());
            currentSocialObject.put("score", tone.getScore());
            currentSocialObject.put("interpretation", "likely high");
        } else if (tone.getScore() <= SOCIAL_LOW_SCORE_THRESHOLD) {
            currentSocial.add(tone.getToneName().toLowerCase() + "_low");
            currentSocialObject.put("tone_name", tone.getToneName().toLowerCase());
            currentSocialObject.put("score", tone.getScore());
            currentSocialObject.put("interpretation", "likely low");
        } else {
            currentSocialObject.put("tone_name", tone.getToneName().toLowerCase());
            currentSocialObject.put("score", tone.getScore());
            currentSocialObject.put("interpretation", "likely medium");
        }
    }
    // update user language tone
    Map<String, Object> social = (Map<String, Object>) ((Map<String, Object>) user.get("tone")).get("social");
    social.put("current", currentSocial);
    // if history needs to be maintained
    if (maintainHistory) {
        List<Map<String, Object>> history = new ArrayList<Map<String, Object>>();
        if (social.get("history") != null) {
            history = (List<Map<String, Object>>) social.get("history");
        }
        history.add(currentSocialObject);
        social.put("history", history);
    }
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) ToneScore(com.ibm.watson.tone_analyzer.v3.model.ToneScore)

Aggregations

ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)8 Map (java.util.Map)8 ToneScore (com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneScore)4 ToneScore (com.ibm.watson.tone_analyzer.v3.model.ToneScore)4 ToneCategory (com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneCategory)1 ToneCategory (com.ibm.watson.tone_analyzer.v3.model.ToneCategory)1