|
| 1 | +/** |
| 2 | +* Copyright 2015 IBM Corp. All Rights Reserved. |
| 3 | +* |
| 4 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +* you may not use this file except in compliance with the License. |
| 6 | +* You may obtain a copy of the License at |
| 7 | +* |
| 8 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +* |
| 10 | +* Unless required by applicable law or agreed to in writing, software |
| 11 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +* See the License for the specific language governing permissions and |
| 14 | +* limitations under the License. |
| 15 | +* |
| 16 | +*/ |
| 17 | + |
| 18 | +using UnityEngine; |
| 19 | +using IBM.Watson.DeveloperCloud.Services.PersonalityInsights.v3; |
| 20 | +using IBM.Watson.DeveloperCloud.Logging; |
| 21 | + |
| 22 | +public class ExamplePersonalityInsightsV3 : MonoBehaviour |
| 23 | +{ |
| 24 | + PersonalityInsights m_personalityInsights = new PersonalityInsights(); |
| 25 | + private string testString = "<text-here>"; |
| 26 | + private string dataPath; |
| 27 | + |
| 28 | + void Start() |
| 29 | + { |
| 30 | + LogSystem.InstallDefaultReactors(); |
| 31 | + |
| 32 | + dataPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/personalityInsights.json"; |
| 33 | + |
| 34 | + if (!m_personalityInsights.GetProfile(OnGetProfileJson, dataPath, ContentType.TEXT_HTML, ContentLanguage.ENGLISH, ContentType.APPLICATION_JSON, AcceptLanguage.ENGLISH, true, true, true)) |
| 35 | + Log.Debug("ExamplePersonalityInsights", "Failed to get profile!"); |
| 36 | + |
| 37 | + if (!m_personalityInsights.GetProfile(OnGetProfileText, testString, ContentType.TEXT_HTML, ContentLanguage.ENGLISH, ContentType.APPLICATION_JSON, AcceptLanguage.ENGLISH, true, true, true)) |
| 38 | + Log.Debug("ExamplePersonalityInsights", "Failed to get profile!"); |
| 39 | + } |
| 40 | + private void OnGetProfileText(Profile profile, string data) |
| 41 | + { |
| 42 | + if (profile != null) |
| 43 | + { |
| 44 | + if (!string.IsNullOrEmpty(profile.processed_language)) |
| 45 | + Log.Debug("TestPersonalityInsightsV3", "processed_language: {0}", profile.processed_language); |
| 46 | + |
| 47 | + Log.Debug("TestPersonalityInsightsV3", "word_count: {0}", profile.word_count); |
| 48 | + |
| 49 | + if (!string.IsNullOrEmpty(profile.word_count_message)) |
| 50 | + Log.Debug("TestPersonalityInsightsV3", "word_count_message: {0}", profile.word_count_message); |
| 51 | + |
| 52 | + if (profile.personality != null && profile.personality.Length > 0) |
| 53 | + { |
| 54 | + Log.Debug("TestPersonalityInsightsV3", "Personality trait tree"); |
| 55 | + foreach (TraitTreeNode node in profile.personality) |
| 56 | + LogTraitTree(node); |
| 57 | + } |
| 58 | + |
| 59 | + if (profile.values != null && profile.values.Length > 0) |
| 60 | + { |
| 61 | + Log.Debug("TestPersonalityInsightsV3", "Values trait tree"); |
| 62 | + foreach (TraitTreeNode node in profile.values) |
| 63 | + LogTraitTree(node); |
| 64 | + } |
| 65 | + |
| 66 | + if (profile.needs != null && profile.personality.Length > 0) |
| 67 | + { |
| 68 | + Log.Debug("TestPersonalityInsightsV3", "Needs trait tree"); |
| 69 | + foreach (TraitTreeNode node in profile.needs) |
| 70 | + LogTraitTree(node); |
| 71 | + } |
| 72 | + |
| 73 | + if (profile.behavior != null && profile.behavior.Length > 0) |
| 74 | + { |
| 75 | + Log.Debug("TestPersonalityInsightsV3", "Behavior tree"); |
| 76 | + foreach (BehaviorNode behavior in profile.behavior) |
| 77 | + { |
| 78 | + Log.Debug("TestPersonalityInsightsV3", "trait_id: {0}", behavior.trait_id); |
| 79 | + Log.Debug("TestPersonalityInsightsV3", "name: {0}", behavior.name); |
| 80 | + Log.Debug("TestPersonalityInsightsV3", "category: {0}", behavior.category); |
| 81 | + Log.Debug("TestPersonalityInsightsV3", "percentage: {0}", behavior.percentage.ToString()); |
| 82 | + Log.Debug("TestPersonalityInsightsV3", "----------------"); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if (profile.consumption_preferences != null && profile.consumption_preferences.Length > 0) |
| 87 | + { |
| 88 | + Log.Debug("TestPersonalityInsightsV3", "ConsumptionPreferencesCategories"); |
| 89 | + foreach (ConsumptionPreferencesCategoryNode categoryNode in profile.consumption_preferences) |
| 90 | + LogConsumptionPreferencesCategory(categoryNode); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private void OnGetProfileJson(Profile profile, string data) |
| 96 | + { |
| 97 | + if (profile != null) |
| 98 | + { |
| 99 | + if (!string.IsNullOrEmpty(profile.processed_language)) |
| 100 | + Log.Debug("TestPersonalityInsightsV3", "processed_language: {0}", profile.processed_language); |
| 101 | + |
| 102 | + Log.Debug("TestPersonalityInsightsV3", "word_count: {0}", profile.word_count); |
| 103 | + |
| 104 | + if (!string.IsNullOrEmpty(profile.word_count_message)) |
| 105 | + Log.Debug("TestPersonalityInsightsV3", "word_count_message: {0}", profile.word_count_message); |
| 106 | + |
| 107 | + if (profile.personality != null && profile.personality.Length > 0) |
| 108 | + { |
| 109 | + Log.Debug("TestPersonalityInsightsV3", "Personality trait tree"); |
| 110 | + foreach (TraitTreeNode node in profile.personality) |
| 111 | + LogTraitTree(node); |
| 112 | + } |
| 113 | + |
| 114 | + if (profile.values != null && profile.values.Length > 0) |
| 115 | + { |
| 116 | + Log.Debug("TestPersonalityInsightsV3", "Values trait tree"); |
| 117 | + foreach (TraitTreeNode node in profile.values) |
| 118 | + LogTraitTree(node); |
| 119 | + } |
| 120 | + |
| 121 | + if (profile.needs != null && profile.personality.Length > 0) |
| 122 | + { |
| 123 | + Log.Debug("TestPersonalityInsightsV3", "Needs trait tree"); |
| 124 | + foreach (TraitTreeNode node in profile.needs) |
| 125 | + LogTraitTree(node); |
| 126 | + } |
| 127 | + |
| 128 | + if (profile.behavior != null && profile.behavior.Length > 0) |
| 129 | + { |
| 130 | + Log.Debug("TestPersonalityInsightsV3", "Behavior tree"); |
| 131 | + foreach (BehaviorNode behavior in profile.behavior) |
| 132 | + { |
| 133 | + Log.Debug("TestPersonalityInsightsV3", "trait_id: {0}", behavior.trait_id); |
| 134 | + Log.Debug("TestPersonalityInsightsV3", "name: {0}", behavior.name); |
| 135 | + Log.Debug("TestPersonalityInsightsV3", "category: {0}", behavior.category); |
| 136 | + Log.Debug("TestPersonalityInsightsV3", "percentage: {0}", behavior.percentage.ToString()); |
| 137 | + Log.Debug("TestPersonalityInsightsV3", "----------------"); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + if (profile.consumption_preferences != null && profile.consumption_preferences.Length > 0) |
| 142 | + { |
| 143 | + Log.Debug("TestPersonalityInsightsV3", "ConsumptionPreferencesCategories"); |
| 144 | + foreach (ConsumptionPreferencesCategoryNode categoryNode in profile.consumption_preferences) |
| 145 | + LogConsumptionPreferencesCategory(categoryNode); |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + private void LogTraitTree(TraitTreeNode traitTreeNode) |
| 151 | + { |
| 152 | + Log.Debug("TestPersonalityInsightsV3", "trait_id: {0} | name: {1} | category: {2} | percentile: {3} | raw_score: {4}", |
| 153 | + string.IsNullOrEmpty(traitTreeNode.trait_id) ? "null" : traitTreeNode.trait_id, |
| 154 | + string.IsNullOrEmpty(traitTreeNode.name) ? "null" : traitTreeNode.name, |
| 155 | + string.IsNullOrEmpty(traitTreeNode.category) ? "null" : traitTreeNode.category, |
| 156 | + string.IsNullOrEmpty(traitTreeNode.percentile.ToString()) ? "null" : traitTreeNode.percentile.ToString(), |
| 157 | + string.IsNullOrEmpty(traitTreeNode.raw_score.ToString()) ? "null" : traitTreeNode.raw_score.ToString()); |
| 158 | + |
| 159 | + if (traitTreeNode.children != null && traitTreeNode.children.Length > 0) |
| 160 | + foreach (TraitTreeNode childNode in traitTreeNode.children) |
| 161 | + LogTraitTree(childNode); |
| 162 | + } |
| 163 | + |
| 164 | + private void LogConsumptionPreferencesCategory(ConsumptionPreferencesCategoryNode categoryNode) |
| 165 | + { |
| 166 | + Log.Debug("TestPersonalityInsightsV3", "consumption_preference_category_id: {0} | name: {1}", categoryNode.consumption_preference_category_id, categoryNode.name); |
| 167 | + |
| 168 | + foreach (ConsumptionPreferencesNode preferencesNode in categoryNode.consumption_preferences) |
| 169 | + Log.Debug("TestPersonalityInsightsV3", "\t consumption_preference_id: {0} | name: {1} | score: {2}", |
| 170 | + string.IsNullOrEmpty(preferencesNode.consumption_preference_id) ? "null" : preferencesNode.consumption_preference_id, |
| 171 | + string.IsNullOrEmpty(preferencesNode.name) ? "null" : preferencesNode.name, |
| 172 | + string.IsNullOrEmpty(preferencesNode.score.ToString()) ? "null" : preferencesNode.score.ToString()); |
| 173 | + } |
| 174 | +} |
0 commit comments