Skip to content

Commit 2e5b13b

Browse files
committed
personality insights example
1 parent 1a12b39 commit 2e5b13b

File tree

3 files changed

+66
-4
lines changed

3 files changed

+66
-4
lines changed

Examples/ServiceExamples/Scripts/ExamplePersonalityInsights.cs

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public class ExamplePersonalityInsights : MonoBehaviour {
2626

2727
void Start ()
2828
{
29+
LogSystem.InstallDefaultReactors();
2930
string dataPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/personalityInsights.json";
3031

3132
if(!m_personalityInsights.GetProfile(OnGetProfile, dataPath, DataModels.ContentType.TEXT_PLAIN, DataModels.Language.ENGLISH))
@@ -34,6 +35,49 @@ void Start ()
3435

3536
private void OnGetProfile(DataModels.Profile profile, string data)
3637
{
37-
Log.Debug("ExamplePersonalityInsights", profile.id);
38+
Log.Debug("ExamplePersonalityInsights", "data: {0}", data);
39+
if(profile != null)
40+
{
41+
if(!string.IsNullOrEmpty(profile.id))
42+
Log.Debug("ExamplePersonalityInsights", "id: {0}", profile.id);
43+
if(!string.IsNullOrEmpty(profile.source))
44+
Log.Debug("ExamplePersonalityInsights", "source: {0}", profile.source);
45+
if(!string.IsNullOrEmpty(profile.processed_lang))
46+
Log.Debug("ExamplePersonalityInsights", "proccessed_lang: {0}", profile.processed_lang);
47+
if(!string.IsNullOrEmpty(profile.word_count))
48+
Log.Debug("ExamplePersonalityInsights", "word_count: {0}", profile.word_count);
49+
if(!string.IsNullOrEmpty(profile.word_count_message))
50+
Log.Debug("ExamplePersonalityInsights", "word_count_message: {0}", profile.word_count_message);
51+
52+
if(profile.tree != null)
53+
{
54+
LogTraitTree(profile.tree);
55+
}
56+
}
57+
else
58+
{
59+
Log.Debug("ExamplePersonalityInsights", "Failed to get profile!");
60+
}
61+
}
62+
63+
private void LogTraitTree(DataModels.TraitTreeNode traitTreeNode)
64+
{
65+
if(!string.IsNullOrEmpty(traitTreeNode.id))
66+
Log.Debug("ExamplePersonalityInsights", "id: {0}", traitTreeNode.id);
67+
if(!string.IsNullOrEmpty(traitTreeNode.name))
68+
Log.Debug("ExamplePersonalityInsights", "name: {0}", traitTreeNode.name);
69+
if(!string.IsNullOrEmpty(traitTreeNode.category))
70+
Log.Debug("ExamplePersonalityInsights", "category: {0}", traitTreeNode.category);
71+
if(!string.IsNullOrEmpty(traitTreeNode.percentage))
72+
Log.Debug("ExamplePersonalityInsights", "percentage: {0}", traitTreeNode.percentage);
73+
if(!string.IsNullOrEmpty(traitTreeNode.sampling_error))
74+
Log.Debug("ExamplePersonalityInsights", "sampling_error: {0}", traitTreeNode.sampling_error);
75+
if(!string.IsNullOrEmpty(traitTreeNode.raw_score))
76+
Log.Debug("ExamplePersonalityInsights", "raw_score: {0}", traitTreeNode.raw_score);
77+
if(!string.IsNullOrEmpty(traitTreeNode.raw_sampling_error))
78+
Log.Debug("ExamplePersonalityInsights", "raw_sampling_error: {0}", traitTreeNode.raw_sampling_error);
79+
if(traitTreeNode.children != null && traitTreeNode.children.Length > 0)
80+
foreach(DataModels.TraitTreeNode childNode in traitTreeNode.children)
81+
LogTraitTree(childNode);
3882
}
3983
}

Examples/ServiceExamples/TestData/personalityInsights.json.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Scripts/Services/PersonalityInsights/PersonalityInsights.cs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
using System.Collections;
2424
using System;
2525
using IBM.Watson.DeveloperCloud.Logging;
26+
using System.IO;
2627

2728
namespace IBM.Watson.DeveloperCloud.Services.PersonalityInsights.v2
2829
{
@@ -48,7 +49,6 @@ public bool GetProfile(OnGetProfile callback, string source,
4849
{
4950
if(callback == null)
5051
throw new ArgumentNullException("callback");
51-
5252
if(string.IsNullOrEmpty(source))
5353
throw new ArgumentNullException("A JSON or Text source is required for GetProfile!");
5454

@@ -68,11 +68,21 @@ public bool GetProfile(OnGetProfile callback, string source,
6868
req.Headers["Accept"] = accept;
6969
req.Headers["Accept-Language"] = acceptLanguage;
7070

71-
req.Send = System.Text.Encoding.UTF8.GetBytes(source);
71+
string normalizedSource = source.Trim().ToLower();
72+
if(Path.GetExtension(normalizedSource).EndsWith(".json"))
73+
{
74+
string jsonData = default(string);
75+
jsonData = File.ReadAllText(source);
76+
req.Send = System.Text.Encoding.UTF8.GetBytes(jsonData);
77+
}
78+
else
79+
{
80+
req.Send = System.Text.Encoding.UTF8.GetBytes(source);
81+
}
7282

7383
return connector.Send(req);
7484
}
75-
85+
7686
private class GetProfileRequest:RESTConnector.Request
7787
{
7888
public string Data { get; set; }

0 commit comments

Comments
 (0)