Search in sources :

Example 1 with ToneCategory

use of com.ibm.watson.tone_analyzer.v3.model.ToneCategory 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(Map<String, Object> 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.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.developer_cloud.tone_analyzer.v3.model.ToneScore) ToneCategory(com.ibm.watson.developer_cloud.tone_analyzer.v3.model.ToneCategory)

Example 2 with ToneCategory

use of com.ibm.watson.tone_analyzer.v3.model.ToneCategory 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)

Aggregations

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