Skip to content

Commit 6e266e9

Browse files
committed
personality insights v3 GetProfile and UnitTest
1 parent 7ea8f3b commit 6e266e9

9 files changed

+482
-26
lines changed

Scripts/Editor/ConfigEditor.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,16 @@ private class ServiceSetup
5757
//new ServiceSetup() { ServiceName = "Language Translator", ServiceAPI = "language-translator/api",
5858
// URL ="https://console.ng.bluemix.net/catalog/services/language-translator/", ServiceID="LanguageTranslatorV1" },
5959
new ServiceSetup() { ServiceName = "Natural Language Classifier", ServiceAPI = "natural-language-classifier/api",
60-
URL ="https://console.ng.bluemix.net/catalog/natural-language-classifier/", ServiceID="NaturalLanguageClassifierV1" },
60+
URL ="https://console.ng.bluemix.net/catalog/natural-language-classifier/", ServiceID="NaturalLanguageClassifierV1" },
6161
new ServiceSetup() { ServiceName = "Tone Analyzer", ServiceAPI = "tone-analyzer/api",
6262
URL ="https://console.ng.bluemix.net/catalog/services/tone-analyzer/", ServiceID="ToneAnalyzerV3" },
6363
new ServiceSetup() { ServiceName = "Tradeoff Analytics", ServiceAPI = "tradeoff-analytics/api",
6464
URL ="https://console.ng.bluemix.net/catalog/services/tradeoff-analytics/", ServiceID="TradeoffAnalyticsV1" },
65-
new ServiceSetup() { ServiceName = "Personality Insights", ServiceAPI = "personality-insights/api",
65+
new ServiceSetup() { ServiceName = "Personality Insights V2", ServiceAPI = "personality-insights/api",
6666
URL ="https://console.ng.bluemix.net/catalog/services/personality-insights/", ServiceID="PersonalityInsightsV2" },
67-
new ServiceSetup() { ServiceName = "Conversation", ServiceAPI = "conversation/api",
67+
new ServiceSetup() { ServiceName = "Personality Insights V3", ServiceAPI = "personality-insights/api",
68+
URL ="https://console.ng.bluemix.net/catalog/services/personality-insights/", ServiceID="PersonalityInsightsV3" },
69+
new ServiceSetup() { ServiceName = "Conversation", ServiceAPI = "conversation/api",
6870
URL ="https://console.ng.bluemix.net/catalog/services/conversation/", ServiceID="ConversationV1" },
6971
new ServiceSetup() { ServiceName = "RetrieveAndRank", ServiceAPI = "retrieve-and-rank/api",
7072
URL ="https://console.ng.bluemix.net/catalog/services/retrieve-and-rank/", ServiceID="RetrieveAndRankV1" },

Scripts/Services/PersonalityInsights/v2/PersonalityInsights.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
using System.Text;
2121
using IBM.Watson.DeveloperCloud.Utilities;
2222
using UnityEngine;
23-
using System.Collections;
2423
using System;
2524
using IBM.Watson.DeveloperCloud.Logging;
2625
using System.IO;

Scripts/Services/PersonalityInsights/v3/DataModels.cs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public class Profile
5959
/// information about the distribution of the content over the days of the week and
6060
/// the hours of the day.
6161
/// </summary>
62-
public TraitTreeNode[] behavior { get; set; }
62+
public BehaviorNode[] behavior { get; set; }
6363
/// <summary>
6464
/// If the consumption_preferences query parameter is true, detailed results for
6565
/// each category of `consumption preferences`. Each element of the array provides
@@ -163,7 +163,7 @@ public class ConsumptionPreferencesCategoryNode
163163
/// Detailed results inferred from the input text for the individual preferences of
164164
/// the category.
165165
/// </summary>
166-
public ConsumptionPreferencesCategoryNode[] consumption_preferences { get; set; }
166+
public ConsumptionPreferencesNode[] consumption_preferences { get; set; }
167167
}
168168

169169
/// <summary>
@@ -214,4 +214,59 @@ public class ConsumptionPreferencesNode
214214
/// </summary>
215215
public double score { get; set; }
216216
}
217+
218+
/// <summary>
219+
/// The content type. Either text, html or json.
220+
/// </summary>
221+
public class ContentType
222+
{
223+
/// <summary>
224+
/// Mime type for plain text.
225+
/// </summary>
226+
public const string TEXT_PLAIN = "text/plain";
227+
228+
/// <summary>
229+
/// Mime type for HTML.
230+
/// </summary>
231+
public const string TEXT_HTML = "text/html";
232+
233+
/// <summary>
234+
/// Mime type for json.
235+
/// </summary>
236+
public const string APPLICATION_JSON = "application/json";
237+
}
238+
239+
/// <summary>
240+
/// The content language. Either English, Arabic, Spanish or Japanese.
241+
/// </summary>
242+
public class Language
243+
{
244+
/// <summary>
245+
/// English.
246+
/// </summary>
247+
public const string ENGLISH = "en";
248+
/// <summary>
249+
/// Arabic.
250+
/// </summary>
251+
public const string ARABIC = "ar";
252+
/// <summary>
253+
/// Spanish.
254+
/// </summary>
255+
public const string SPANISH = "es";
256+
/// <summary>
257+
/// Japanese
258+
/// </summary>
259+
public const string JAPANESE = "ja";
260+
}
261+
262+
/// <summary>
263+
/// The Personality Insights version.
264+
/// </summary>
265+
public class PersonalityInsightsVersion
266+
{
267+
/// <summary>
268+
/// The version.
269+
/// </summary>
270+
public const string Version = "2016-10-20";
271+
}
217272
}
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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 FullSerializer;
19+
using IBM.Watson.DeveloperCloud.Connection;
20+
using IBM.Watson.DeveloperCloud.Logging;
21+
using IBM.Watson.DeveloperCloud.Utilities;
22+
using System;
23+
using System.IO;
24+
using System.Text;
25+
using UnityEngine;
26+
27+
namespace IBM.Watson.DeveloperCloud.Services.PersonalityInsights.v3
28+
{
29+
/// <summary>
30+
/// This class wraps the Personality Insights service.
31+
/// <a href="http://www.ibm.com/watson/developercloud/personality-insights.html">Personality Insights Service</a>
32+
/// </summary>
33+
public class PersonalityInsights : IWatsonService
34+
{
35+
#region Private Data
36+
private const string SERVICE_ID = "PersonalityInsightsV3";
37+
private static fsSerializer sm_Serializer = new fsSerializer();
38+
#endregion
39+
40+
#region Profile
41+
private const string SERVICE_GET_PROFILE = "/v3/profile";
42+
43+
public delegate void OnGetProfile(Profile profile, string data);
44+
45+
public bool GetProfile(OnGetProfile callback, string source,
46+
string contentType = ContentType.TEXT_PLAIN,
47+
string contentLanguage = Language.ENGLISH,
48+
string accept = ContentType.APPLICATION_JSON,
49+
string acceptLanguage = Language.ENGLISH,
50+
bool raw_scores = false,
51+
bool csv_headers = false,
52+
bool consumption_preferences = false,
53+
string version = PersonalityInsightsVersion.Version,
54+
string data = default(string))
55+
{
56+
if (callback == null)
57+
throw new ArgumentNullException("callback");
58+
if (string.IsNullOrEmpty(source))
59+
throw new ArgumentNullException("A JSON or Text source is required for GetProfile!");
60+
61+
RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, SERVICE_GET_PROFILE);
62+
if (connector == null)
63+
return false;
64+
65+
GetProfileRequest req = new GetProfileRequest();
66+
req.Source = source;
67+
req.Callback = callback;
68+
req.Data = data;
69+
req.OnResponse = GetProfileResponse;
70+
71+
req.Parameters["raw_scores"] = raw_scores.ToString();
72+
req.Parameters["csv_headers"] = csv_headers.ToString();
73+
req.Parameters["consumption_preferences"] = consumption_preferences.ToString();
74+
req.Parameters["version"] = version;
75+
76+
req.Headers["Content-Type"] = contentType;
77+
req.Headers["Content-Language"] = contentLanguage;
78+
req.Headers["Accept"] = accept;
79+
req.Headers["Accept-Language"] = acceptLanguage;
80+
81+
if (source.StartsWith(Application.dataPath))
82+
{
83+
string jsonData = default(string);
84+
jsonData = File.ReadAllText(source);
85+
req.Send = System.Text.Encoding.UTF8.GetBytes(jsonData);
86+
}
87+
else
88+
{
89+
req.Send = System.Text.Encoding.UTF8.GetBytes(source);
90+
}
91+
92+
return connector.Send(req);
93+
}
94+
95+
/// <summary>
96+
/// Get profile request.
97+
/// </summary>
98+
public class GetProfileRequest : RESTConnector.Request
99+
{
100+
/// <summary>
101+
/// The source string.
102+
/// </summary>
103+
public string Source { get; set; }
104+
/// <summary>
105+
/// Custom data.
106+
/// </summary>
107+
public string Data { get; set; }
108+
/// <summary>
109+
/// The callback.
110+
/// </summary>
111+
public OnGetProfile Callback { get; set; }
112+
}
113+
114+
private void GetProfileResponse(RESTConnector.Request req, RESTConnector.Response resp)
115+
{
116+
Profile response = new Profile();
117+
if (resp.Success)
118+
{
119+
try
120+
{
121+
fsData data = null;
122+
fsResult r = fsJsonParser.Parse(Encoding.UTF8.GetString(resp.Data), out data);
123+
if (!r.Succeeded)
124+
throw new WatsonException(r.FormattedMessages);
125+
126+
object obj = response;
127+
r = sm_Serializer.TryDeserialize(data, obj.GetType(), ref obj);
128+
if (!r.Succeeded)
129+
throw new WatsonException(r.FormattedMessages);
130+
}
131+
catch (Exception e)
132+
{
133+
Log.Error("PersonalityInsights", "GetProfileResponse Exception: {0}", e.ToString());
134+
resp.Success = false;
135+
}
136+
}
137+
138+
if (((GetProfileRequest)req).Callback != null)
139+
((GetProfileRequest)req).Callback(resp.Success ? response : null, ((GetProfileRequest)req).Data);
140+
}
141+
#endregion
142+
143+
#region IWatsonService implementation
144+
public string GetServiceID()
145+
{
146+
return SERVICE_ID;
147+
}
148+
149+
public void GetServiceStatus(ServiceStatus callback)
150+
{
151+
if (Utilities.Config.Instance.FindCredentials(SERVICE_ID) != null)
152+
new CheckServiceStatus(this, callback);
153+
else
154+
callback(SERVICE_ID, false);
155+
}
156+
157+
private class CheckServiceStatus
158+
{
159+
private PersonalityInsights m_Service = null;
160+
private ServiceStatus m_Callback = null;
161+
162+
public CheckServiceStatus(PersonalityInsights service, ServiceStatus callback)
163+
{
164+
m_Service = service;
165+
m_Callback = callback;
166+
string dataPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/personalityInsights.json";
167+
if (!m_Service.GetProfile(OnGetProfile, dataPath, ContentType.TEXT_PLAIN, Language.ENGLISH))
168+
m_Callback(SERVICE_ID, false);
169+
}
170+
171+
private void OnGetProfile(Profile resp, string data)
172+
{
173+
if (m_Callback != null)
174+
m_Callback(SERVICE_ID, resp != null);
175+
}
176+
}
177+
#endregion
178+
}
179+
}

Scripts/Services/PersonalityInsights/v3/PersonalityInsights.cs.meta

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

0 commit comments

Comments
 (0)