private void OnGetToneAnalyze(ToneAnalyzerResponse resp, string data) { /* * Response: [ToneAnalyzerResponse: document_tone=[DocumentTone: tone_categories=[ToneCategory: category_id= * emotion_tone, category_name=Emotion Tone, tones=[Tone: score=0, tone_id=anger, tone_name=Anger][Tone: score= * 0, tone_id=disgust, tone_name=Disgust][Tone: score=0, tone_id=fear, tone_name=Fear][Tone: score=0, tone_id= * joy, tone_name=Joy][Tone: score=0, tone_id=sadness, tone_name=Sadness]][ToneCategory: category_id= * writing_tone, category_name=Writing Tone, tones=[Tone: score=0, tone_id=analytical, tone_name=Analytical][Tone: score= * 0, tone_id=confident, tone_name=Confident][Tone: score=0, tone_id=tentative, tone_name=Tentative]][ToneCategory: category_id= * social_tone, category_name=Social Tone, tones=[Tone: score=0.288567, tone_id=openness_big5, tone_name=Openness][Tone: score= * 0.274434, tone_id=conscientiousness_big5, tone_name=Conscientiousness][Tone: score=0.543322, tone_id=extraversion_big5, tone_name= * Extraversion][Tone: score=0.599483, tone_id=agreeableness_big5, tone_name=Agreeableness][Tone: score=0.292921, tone_id= * neuroticism_big5, tone_name=Emotional Range]]], sentenceTone=No Sentence Tone] - okay * */ Debug.Log("Response: " + resp + " - " + data); // regex score=([0-1]\.*[0-9]*), tone_id=(\S+), string expr = "score=([0-1]\\.*[0-9]*), tone_id=(\\S+),"; MatchCollection mc = Regex.Matches(resp.ToString(), expr); foreach (Match m in mc) { //Debug.Log(m.ToString()); // 1 is score, 2 is tone id name //Debug.Log("In match " + m + ", score is " + m.Groups[1].Value); //Debug.Log("In match " + m + ", tone_id is " + m.Groups[2].Value); double x; double.TryParse(m.Groups [1].Value, out x); tone_dict [m.Groups [2].Value] = x; } tone_dict ["Surprise"] = faceNormalizeVector [0]; tone_dict ["Joy"] = faceNormalizeVector [1]; tone_dict ["Sadness"] = faceNormalizeVector [2]; tone_dict ["Anger"] = faceNormalizeVector [3]; tone_dict ["Contempt"] = faceNormalizeVector [4]; tone_dict ["Disgust"] = faceNormalizeVector [5]; tone_dict ["Fear"] = faceNormalizeVector [6]; tone_dict ["Valence"] = faceNormalizeVector [7]; tone_dict ["Engagement"] = faceNormalizeVector [8]; tone_dict ["Smile"] = faceNormalizeVector [9]; tone_dict ["LipSuck"] = faceNormalizeVector [10]; tone_dict ["Attention"] = faceNormalizeVector [11]; tone_dict ["BrowFurrow"] = faceNormalizeVector [12]; tone_dict ["BrowRaise"] = faceNormalizeVector [13]; tone_dict ["ChinRaise"] = faceNormalizeVector [14]; tone_dict ["EyeClosure"] = faceNormalizeVector [15]; tone_dict ["InnerEyeBrowRaise"] = faceNormalizeVector [16]; tone_dict ["LipCornerDepressor"] = faceNormalizeVector [17]; tone_dict ["LipPress"] = faceNormalizeVector [18]; tone_dict ["LipPucker"] = faceNormalizeVector [19]; tone_dict ["MouthOpen"] = faceNormalizeVector [20]; tone_dict ["NoseWrinkler"] = faceNormalizeVector [21]; tone_dict ["Smirk"] = faceNormalizeVector [22]; tone_dict ["UpperLipRaise"] = faceNormalizeVector [23]; mapper.emotDict = tone_dict; mapper.map(); int y = 0; double currentMax = 0; string maxEmotWatson = "Happy"; string maxBig5 = null; string maxBigThree = null; string maxEmotFace = null; foreach (KeyValuePair <string, double> entry in tone_dict) { Debug.Log(entry.Key + " : " + entry.Value); if ((y == 0 && y == 5) && ((y == 8 && y == 13) && y == 20)) { currentMax = 0; } if (y < 5) { if (entry.Value > currentMax) { currentMax = entry.Value; maxEmotWatson = entry.Key; } } else if (y < 8) { if (entry.Value > currentMax) { currentMax = entry.Value; maxBig5 = entry.Key; } } else if (y < 13) { if (entry.Value > currentMax) { currentMax = entry.Value; maxBigThree = entry.Key; } } else if (y < 20) { if (entry.Value > currentMax) { currentMax = entry.Value; maxEmotFace = entry.Key; } } y++; } emotionText.text = "Primary Emotion: " + "\n" + maxEmotWatson + "\n" + maxBig5 + "\n" + maxBigThree + "\n" + maxEmotFace; }
private void OnGetToneAnalyze(ToneAnalyzerResponse resp, string data) { Debug.Log(resp); string response = resp.ToString(); response.ToCharArray(); double[] arr = new double[5]; int i = 0; string sub; for (int j = 0; j < 5; j++) { while (response[i] != '0') { i++; } sub = response.Substring(i, 5); i = i + 11; Debug.Log(sub); arr[j] = Convert.ToDouble(sub); } int max = 0; for (int k = 1; k < 5; k++) { if (arr[k] > arr[max]) { max = k; } } string result = ""; if (max == 0) { result = "anger"; funct = result; } else if (max == 1) { result = "disgust"; funct = result; } else if (max == 2) { result = "fear"; funct = result; } else if (max == 3) { result = "joy"; funct = result; } else if (max == 4) { result = "sadness"; funct = result; } Debug.Log(result); }