Skip to content

Commit 5350afb

Browse files
committed
added example for personality insights v3
1 parent 6e266e9 commit 5350afb

File tree

5 files changed

+232
-8
lines changed

5 files changed

+232
-8
lines changed

Examples/ServiceExamples/Scripts/ExamplePersonalityInsights.cs renamed to Examples/ServiceExamples/Scripts/ExamplePersonalityInsightsV2.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
*/
1717

1818
using UnityEngine;
19-
using System.Collections;
2019
using IBM.Watson.DeveloperCloud.Services.PersonalityInsights.v2;
2120
using IBM.Watson.DeveloperCloud.Logging;
2221

23-
public class ExamplePersonalityInsights : MonoBehaviour {
22+
public class ExamplePersonalityInsightsV2 : MonoBehaviour
23+
{
2424
PersonalityInsights m_personalityInsights = new PersonalityInsights();
2525

2626
void Start ()
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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 = "Facing certain defeat at the hands of a room-size I.B.M. computer on Wednesday evening, Ken Jennings, famous for winning 74 games in a row on the TV quiz show, acknowledged the obvious. \"I, for one, welcome our new computer overlords,\" he wrote on his video screen, borrowing a line from a \"Simpsons\" episode.\n\nFrom now on, if the answer is \"the computer champion on \"Jeopardy!,\" the question will be, \"What is Watson?\"\n\nFor I.B.M., the showdown was not merely a well-publicized stunt and a $1 million prize, but proof that the company has taken a big step toward a world in which intelligent machines will understand and respond to humans, and perhaps inevitably, replace some of them.\n\nWatson, specifically, is a \"question answering machine\" of a type that artificial intelligence researchers have struggled with for decades — a computer akin to the one on \"Star Trek\" that can understand questions posed in natural language and answer them.\n\nWatson showed itself to be imperfect, but researchers at I.B.M. and other companies are already developing uses for Watson's technologies that could have a significant impact on the way doctors practice and consumers buy products.\n\n\"Cast your mind back 20 years and who would have thought this was possible?\" said Edward Feigenbaum, a Stanford University computer scientist and a pioneer in the field.\n\nIn its \"Jeopardy!\" project, I.B.M. researchers were tackling a game that requires not only encyclopedic recall, but also the ability to untangle convoluted and often opaque statements, a modicum of luck, and quick, strategic button pressing.\n\nThe contest, which was taped in January here at the company's T. J. Watson Research Laboratory before an audience of I.B.M. executives and company clients, played out in three televised episodes concluding Wednesday. At the end of the first day, Watson was in a tie with Brad Rutter, another ace human player, at $5,000 each, with Mr. Jennings trailing with $2,000.\n\nBut on the second day, Watson went on a tear. By night's end, Watson had a commanding lead with a total of $35,734, compared with Mr. Rutter's $10,400 and Mr. Jennings's $4,800.\n\nVictory was not cemented until late in the third match, when Watson was in Nonfiction. \"Same category for $1,200,\" it said in a manufactured tenor, and lucked into a Daily Double. Mr. Jennings grimaced.\n\nEven later in the match, however, had Mr. Jennings won another key Daily Double it might have come down to Final Jeopardy, I.B.M. researchers acknowledged.\n\nThe final tally was $77,147 to Mr. Jennings's $24,000 and Mr. Rutter's $21,600.\n\nMore than anything, the contest was a vindication for the academic field of artificial intelligence, which began with great promise in the 1960s with the vision of creating a thinking machine and which became the laughingstock of Silicon Valley in the 1980s, when a series of heavily financed start-up companies went bankrupt.\n\nDespite its intellectual prowess, Watson was by no means omniscient. On Tuesday evening during Final Jeopardy, the category was U.S. Cities and the clue was: \"Its largest airport is named for a World War II hero; its second largest for a World War II battle.\"\n\nWatson drew guffaws from many in the television audience when it responded \"What is Toronto?????\"\n\nThe string of question marks indicated that the system had very low confidence in its response, I.B.M. researchers said, but because it was Final Jeopardy, it was forced to give a response. The machine did not suffer much damage. It had wagered just $947 on its result. (The correct answer is, \"What is Chicago?\")\n\n\"We failed to deeply understand what was going on there,\" said David Ferrucci, an I.B.M. researcher who led the development of Watson. \"The reality is that there's lots of data where the title is U.S. cities and the answers are countries, European cities, people, mayors. Even though it says U.S. cities, we had very little confidence that that's the distinguishing feature.\"\n\nThe researchers also acknowledged that the machine had benefited from the \"buzzer factor.\"\n\nBoth Mr. Jennings and Mr. Rutter are accomplished at anticipating the light that signals it is possible to \"buzz in,\" and can sometimes get in with virtually zero lag time. The danger is to buzz too early, in which case the contestant is penalized and \"locked out\" for roughly a quarter of a second.\n\nWatson, on the other hand, does not anticipate the light, but has a weighted scheme that allows it, when it is highly confident, to hit the buzzer in as little as 10 milliseconds, making it very hard for humans to beat. When it was less confident, it took longer to buzz in. In the second round, Watson beat the others to the buzzer in 24 out of 30 Double Jeopardy questions.\n\n\"It sort of wants to get beaten when it doesn't have high confidence,\" Dr. Ferrucci said. \"It doesn't want to look stupid.\"\n\nBoth human players said that Watson's button pushing skill was not necessarily an unfair advantage. \"I beat Watson a couple of times,\" Mr. Rutter said.\n\nWhen Watson did buzz in, it made the most of it. Showing the ability to parse language, it responded to, \"A recent best seller by Muriel Barbery is called 'This of the Hedgehog,' \" with \"What is Elegance?\"\n\nIt showed its facility with medical diagnosis. With the answer: \"You just need a nap. You don't have this sleep disorder that can make sufferers nod off while standing up,\" Watson replied, \"What is narcolepsy?\"\n\nThe coup de grâce came with the answer, \"William Wilkenson's 'An Account of the Principalities of Wallachia and Moldavia' inspired this author's most famous novel.\" Mr. Jennings wrote, correctly, Bram Stoker, but realized that he could not catch up with Watson's winnings and wrote out his surrender.\n\nBoth players took the contest and its outcome philosophically.\n\n\"I had a great time and I would do it again in a heartbeat,\" said Mr. Jennings. \"It's not about the results; this is about being part of the future.\"\n\nFor I.B.M., the future will happen very quickly, company executives said. On Thursday it plans to announce that it will collaborate with Columbia University and the University of Maryland to create a physician's assistant service that will allow doctors to query a cybernetic assistant. The company also plans to work with Nuance Communications Inc. to add voice recognition to the physician's assistant, possibly making the service available in as little as 18 months.\n\n\"I have been in medical education for 40 years and we're still a very memory-based curriculum,\" said Dr. Herbert Chase, a professor of clinical medicine at Columbia University who is working with I.B.M. on the physician's assistant. \"The power of Watson- like tools will cause us to reconsider what it is we want students to do.\"\n\nI.B.M. executives also said they are in discussions with a major consumer electronics retailer to develop a version of Watson, named after I.B.M.'s founder, Thomas J. Watson, that would be able to interact with consumers on a variety of subjects like buying decisions and technical support.\n\nDr. Ferrucci sees none of the fears that have been expressed by theorists and science fiction writers about the potential of computers to usurp humans.\n\n\"People ask me if this is HAL,\" he said, referring to the computer in \"2001: A Space Odyssey.\" \"HAL's not the focus; the focus is on the computer on 'Star Trek,' where you have this intelligent information seek dialogue, where you can ask follow-up questions and the computer can look at all the evidence and tries to ask follow-up questions. That's very cool.\"";
26+
27+
void Start () {
28+
LogSystem.InstallDefaultReactors();
29+
30+
string dataPath = Application.dataPath + "/Watson/Examples/ServiceExamples/TestData/personalityInsights.json";
31+
32+
if(!m_personalityInsights.GetProfile(OnGetProfileJson, dataPath, ContentType.TEXT_HTML, Language.ENGLISH, ContentType.APPLICATION_JSON, Language.ENGLISH, true, true, true))
33+
Log.Debug("ExamplePersonalityInsights", "Failed to get profile!");
34+
35+
if (!m_personalityInsights.GetProfile(OnGetProfileText, testString, ContentType.TEXT_HTML, Language.ENGLISH, ContentType.APPLICATION_JSON, Language.ENGLISH, true, true, true))
36+
Log.Debug("ExamplePersonalityInsights", "Failed to get profile!");
37+
}
38+
private void OnGetProfileText(Profile profile, string data)
39+
{
40+
if (profile != null)
41+
{
42+
if (!string.IsNullOrEmpty(profile.processed_language))
43+
Log.Debug("TestPersonalityInsightsV3", "processed_language: {0}", profile.processed_language);
44+
45+
Log.Debug("TestPersonalityInsightsV3", "word_count: {0}", profile.word_count);
46+
47+
if (!string.IsNullOrEmpty(profile.word_count_message))
48+
Log.Debug("TestPersonalityInsightsV3", "word_count_message: {0}", profile.word_count_message);
49+
50+
if (profile.personality != null && profile.personality.Length > 0)
51+
{
52+
Log.Debug("TestPersonalityInsightsV3", "Personality trait tree");
53+
foreach (TraitTreeNode node in profile.personality)
54+
LogTraitTree(node);
55+
}
56+
57+
if (profile.values != null && profile.values.Length > 0)
58+
{
59+
Log.Debug("TestPersonalityInsightsV3", "Values trait tree");
60+
foreach (TraitTreeNode node in profile.values)
61+
LogTraitTree(node);
62+
}
63+
64+
if (profile.needs != null && profile.personality.Length > 0)
65+
{
66+
Log.Debug("TestPersonalityInsightsV3", "Needs trait tree");
67+
foreach (TraitTreeNode node in profile.needs)
68+
LogTraitTree(node);
69+
}
70+
71+
if (profile.behavior != null && profile.behavior.Length > 0)
72+
{
73+
Log.Debug("TestPersonalityInsightsV3", "Behavior tree");
74+
foreach (BehaviorNode behavior in profile.behavior)
75+
{
76+
Log.Debug("TestPersonalityInsightsV3", "trait_id: {0}", behavior.trait_id);
77+
Log.Debug("TestPersonalityInsightsV3", "name: {0}", behavior.name);
78+
Log.Debug("TestPersonalityInsightsV3", "category: {0}", behavior.category);
79+
Log.Debug("TestPersonalityInsightsV3", "percentage: {0}", behavior.percentage.ToString());
80+
Log.Debug("TestPersonalityInsightsV3", "----------------");
81+
}
82+
}
83+
84+
if (profile.consumption_preferences != null && profile.consumption_preferences.Length > 0)
85+
{
86+
Log.Debug("TestPersonalityInsightsV3", "ConsumptionPreferencesCategories");
87+
foreach (ConsumptionPreferencesCategoryNode categoryNode in profile.consumption_preferences)
88+
LogConsumptionPreferencesCategory(categoryNode);
89+
}
90+
}
91+
}
92+
93+
private void OnGetProfileJson(Profile profile, string data)
94+
{
95+
if (profile != null)
96+
{
97+
if (!string.IsNullOrEmpty(profile.processed_language))
98+
Log.Debug("TestPersonalityInsightsV3", "processed_language: {0}", profile.processed_language);
99+
100+
Log.Debug("TestPersonalityInsightsV3", "word_count: {0}", profile.word_count);
101+
102+
if (!string.IsNullOrEmpty(profile.word_count_message))
103+
Log.Debug("TestPersonalityInsightsV3", "word_count_message: {0}", profile.word_count_message);
104+
105+
if (profile.personality != null && profile.personality.Length > 0)
106+
{
107+
Log.Debug("TestPersonalityInsightsV3", "Personality trait tree");
108+
foreach (TraitTreeNode node in profile.personality)
109+
LogTraitTree(node);
110+
}
111+
112+
if (profile.values != null && profile.values.Length > 0)
113+
{
114+
Log.Debug("TestPersonalityInsightsV3", "Values trait tree");
115+
foreach (TraitTreeNode node in profile.values)
116+
LogTraitTree(node);
117+
}
118+
119+
if (profile.needs != null && profile.personality.Length > 0)
120+
{
121+
Log.Debug("TestPersonalityInsightsV3", "Needs trait tree");
122+
foreach (TraitTreeNode node in profile.needs)
123+
LogTraitTree(node);
124+
}
125+
126+
if (profile.behavior != null && profile.behavior.Length > 0)
127+
{
128+
Log.Debug("TestPersonalityInsightsV3", "Behavior tree");
129+
foreach (BehaviorNode behavior in profile.behavior)
130+
{
131+
Log.Debug("TestPersonalityInsightsV3", "trait_id: {0}", behavior.trait_id);
132+
Log.Debug("TestPersonalityInsightsV3", "name: {0}", behavior.name);
133+
Log.Debug("TestPersonalityInsightsV3", "category: {0}", behavior.category);
134+
Log.Debug("TestPersonalityInsightsV3", "percentage: {0}", behavior.percentage.ToString());
135+
Log.Debug("TestPersonalityInsightsV3", "----------------");
136+
}
137+
}
138+
139+
if (profile.consumption_preferences != null && profile.consumption_preferences.Length > 0)
140+
{
141+
Log.Debug("TestPersonalityInsightsV3", "ConsumptionPreferencesCategories");
142+
foreach (ConsumptionPreferencesCategoryNode categoryNode in profile.consumption_preferences)
143+
LogConsumptionPreferencesCategory(categoryNode);
144+
}
145+
}
146+
}
147+
148+
private void LogTraitTree(TraitTreeNode traitTreeNode)
149+
{
150+
Log.Debug("TestPersonalityInsightsV3", "trait_id: {0} | name: {1} | category: {2} | percentile: {3} | raw_score: {4}",
151+
string.IsNullOrEmpty(traitTreeNode.trait_id) ? "null" : traitTreeNode.trait_id,
152+
string.IsNullOrEmpty(traitTreeNode.name) ? "null" : traitTreeNode.name,
153+
string.IsNullOrEmpty(traitTreeNode.category) ? "null" : traitTreeNode.category,
154+
string.IsNullOrEmpty(traitTreeNode.percentile.ToString()) ? "null" : traitTreeNode.percentile.ToString(),
155+
string.IsNullOrEmpty(traitTreeNode.raw_score.ToString()) ? "null" : traitTreeNode.raw_score.ToString());
156+
157+
if (traitTreeNode.children != null && traitTreeNode.children.Length > 0)
158+
foreach (TraitTreeNode childNode in traitTreeNode.children)
159+
LogTraitTree(childNode);
160+
}
161+
162+
private void LogConsumptionPreferencesCategory(ConsumptionPreferencesCategoryNode categoryNode)
163+
{
164+
Log.Debug("TestPersonalityInsightsV3", "consumption_preference_category_id: {0} | name: {1}", categoryNode.consumption_preference_category_id, categoryNode.name);
165+
166+
foreach (ConsumptionPreferencesNode preferencesNode in categoryNode.consumption_preferences)
167+
Log.Debug("TestPersonalityInsightsV3", "\t consumption_preference_id: {0} | name: {1} | score: {2}",
168+
string.IsNullOrEmpty(preferencesNode.consumption_preference_id) ? "null" : preferencesNode.consumption_preference_id,
169+
string.IsNullOrEmpty(preferencesNode.name) ? "null" : preferencesNode.name,
170+
string.IsNullOrEmpty(preferencesNode.score.ToString()) ? "null" : preferencesNode.score.ToString());
171+
}
172+
}

Examples/ServiceExamples/Scripts/ExamplePersonalityInsightsV3.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.

Examples/ServiceExamples/ServiceExamples.unity

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ RenderSettings:
3737
m_ReflectionIntensity: 1
3838
m_CustomReflection: {fileID: 0}
3939
m_Sun: {fileID: 0}
40-
m_IndirectSpecularColor: {r: 0.3735645, g: 0.38112062, b: 0.35887584, a: 1}
40+
m_IndirectSpecularColor: {r: 0.3735644, g: 0.38112032, b: 0.35887682, a: 1}
4141
--- !u!157 &3
4242
LightmapSettings:
4343
m_ObjectHideFlags: 0
@@ -209,7 +209,7 @@ Transform:
209209
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
210210
m_Children: []
211211
m_Father: {fileID: 0}
212-
m_RootOrder: 11
212+
m_RootOrder: 12
213213
--- !u!1 &725710367
214214
GameObject:
215215
m_ObjectHideFlags: 0
@@ -336,6 +336,46 @@ Transform:
336336
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
337337
m_Children: []
338338
m_Father: {fileID: 0}
339+
m_RootOrder: 11
340+
--- !u!1 &854511683
341+
GameObject:
342+
m_ObjectHideFlags: 0
343+
m_PrefabParentObject: {fileID: 0}
344+
m_PrefabInternal: {fileID: 0}
345+
serializedVersion: 4
346+
m_Component:
347+
- 4: {fileID: 854511685}
348+
- 114: {fileID: 854511684}
349+
m_Layer: 0
350+
m_Name: ExamplePersonalityInsightsV3
351+
m_TagString: Untagged
352+
m_Icon: {fileID: 0}
353+
m_NavMeshLayer: 0
354+
m_StaticEditorFlags: 0
355+
m_IsActive: 0
356+
--- !u!114 &854511684
357+
MonoBehaviour:
358+
m_ObjectHideFlags: 0
359+
m_PrefabParentObject: {fileID: 0}
360+
m_PrefabInternal: {fileID: 0}
361+
m_GameObject: {fileID: 854511683}
362+
m_Enabled: 1
363+
m_EditorHideFlags: 0
364+
m_Script: {fileID: 11500000, guid: cd2e3d3910ea1bd46b55eb5943a057f7, type: 3}
365+
m_Name:
366+
m_EditorClassIdentifier:
367+
--- !u!4 &854511685
368+
Transform:
369+
m_ObjectHideFlags: 0
370+
m_PrefabParentObject: {fileID: 0}
371+
m_PrefabInternal: {fileID: 0}
372+
m_GameObject: {fileID: 854511683}
373+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
374+
m_LocalPosition: {x: 0, y: 0, z: 0}
375+
m_LocalScale: {x: 1, y: 1, z: 1}
376+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
377+
m_Children: []
378+
m_Father: {fileID: 0}
339379
m_RootOrder: 10
340380
--- !u!1 &859102722
341381
GameObject:
@@ -416,7 +456,7 @@ Transform:
416456
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
417457
m_Children: []
418458
m_Father: {fileID: 0}
419-
m_RootOrder: 12
459+
m_RootOrder: 13
420460
--- !u!1 &1073418922
421461
GameObject:
422462
m_ObjectHideFlags: 0
@@ -472,7 +512,7 @@ GameObject:
472512
m_Icon: {fileID: 0}
473513
m_NavMeshLayer: 0
474514
m_StaticEditorFlags: 0
475-
m_IsActive: 1
515+
m_IsActive: 0
476516
--- !u!114 &1160237479
477517
MonoBehaviour:
478518
m_ObjectHideFlags: 0
@@ -577,7 +617,7 @@ Transform:
577617
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
578618
m_Children: []
579619
m_Father: {fileID: 0}
580-
m_RootOrder: 13
620+
m_RootOrder: 14
581621
--- !u!1 &1713392457
582622
GameObject:
583623
m_ObjectHideFlags: 0
@@ -668,7 +708,7 @@ GameObject:
668708
- 4: {fileID: 2004886373}
669709
- 114: {fileID: 2004886372}
670710
m_Layer: 0
671-
m_Name: ExamplePersonalityInsights
711+
m_Name: ExamplePersonalityInsightsV2
672712
m_TagString: Untagged
673713
m_Icon: {fileID: 0}
674714
m_NavMeshLayer: 0

0 commit comments

Comments
 (0)