Skip to content

Commit 3b4f5ec

Browse files
committed
separating ToneAnalyzer into v2 and v3
1 parent 3a97282 commit 3b4f5ec

File tree

12 files changed

+251
-5
lines changed

12 files changed

+251
-5
lines changed

Scripts/Services/ToneAnalyzer/v2.meta

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

Scripts/Services/ToneAnalyzer/DataModels.cs renamed to Scripts/Services/ToneAnalyzer/v2/DataModels.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
using FullSerializer;
1919

20-
namespace IBM.Watson.DeveloperCloud.Services.ToneAnalyzer.v1
20+
namespace IBM.Watson.DeveloperCloud.Services.ToneAnalyzer.v2
2121
{
2222

2323

File renamed without changes.

Scripts/Services/ToneAnalyzer/ToneAnalyzer.cs renamed to Scripts/Services/ToneAnalyzer/v2/ToneAnalyzer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
using System;
2626
using FullSerializer;
2727

28-
namespace IBM.Watson.DeveloperCloud.Services.ToneAnalyzer.v1
28+
namespace IBM.Watson.DeveloperCloud.Services.ToneAnalyzer.v2
2929
{
3030
public class ToneAnalyzer : IWatsonService {
3131

3232
#region Private Data
33-
private const string SERVICE_ID = "ToneAnalyzerV1";
33+
private const string SERVICE_ID = "ToneAnalyzerV2";
3434
private static fsSerializer sm_Serializer = new fsSerializer();
3535
#endregion
3636

File renamed without changes.

Scripts/Services/ToneAnalyzer/v3.meta

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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+
20+
namespace IBM.Watson.DeveloperCloud.Services.ToneAnalyzer.v3
21+
{
22+
23+
#region Tone
24+
[fsObject]
25+
public class ToneAnalysis
26+
{
27+
public object document_tone { get; set; }
28+
public SentenceAnalysis[] sentences_tone { get; set; }
29+
30+
public override string ToString()
31+
{
32+
string sentenceTone = ( sentences_tone != null && sentences_tone.Length > 0 )? sentences_tone[0].ToString() : "No Sentence Tone";
33+
return string.Format("[ToneAnalyzerResponse: document_tone={0}, sentenceTone={1}]", document_tone, sentenceTone);
34+
}
35+
}
36+
37+
[fsObject]
38+
public class SentenceAnalysis
39+
{
40+
public int sentence_id { get; set; }
41+
public int input_from { get; set; }
42+
public int input_to { get; set; }
43+
public string text { get; set; }
44+
public ToneCategory[] tone_categories { get; set; }
45+
}
46+
47+
[fsObject]
48+
public class ToneCategory
49+
{
50+
public string category_name { get; set; }
51+
public string category_id { get; set; }
52+
public ToneScore[] tones { get; set; }
53+
}
54+
55+
[fsObject]
56+
public class ToneScore
57+
{
58+
public string tone_name { get; set; }
59+
public string tone_id { get; set; }
60+
public double score { get; set; }
61+
}
62+
#endregion
63+
}

Scripts/Services/ToneAnalyzer/v3/DataModels.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.
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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 System.Collections.Generic;
20+
using IBM.Watson.DeveloperCloud.Connection;
21+
using IBM.Watson.DeveloperCloud.Utilities;
22+
using IBM.Watson.DeveloperCloud.Logging;
23+
using System.Text;
24+
using MiniJSON;
25+
using System;
26+
using FullSerializer;
27+
28+
namespace IBM.Watson.DeveloperCloud.Services.ToneAnalyzer.v3
29+
{
30+
public class ToneAnalyzer : IWatsonService {
31+
32+
#region Private Data
33+
private const string SERVICE_ID = "ToneAnalyzerV3";
34+
private static fsSerializer sm_Serializer = new fsSerializer();
35+
#endregion
36+
37+
#region Get Tone
38+
39+
private const string FUNCTION_TONE = "/v3/tone";
40+
41+
public delegate void OnGetToneAnalyzed( ToneAnalysis resp, string data );
42+
43+
public bool GetToneAnalyze(OnGetToneAnalyzed callback, string text, string data = null)
44+
{
45+
if (callback == null)
46+
throw new ArgumentNullException("callback");
47+
48+
RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, FUNCTION_TONE);
49+
if (connector == null)
50+
return false;
51+
52+
GetToneAnalyzerRequest req = new GetToneAnalyzerRequest();
53+
req.Callback = callback;
54+
req.OnResponse = GetToneAnalyzerResponse;
55+
56+
Dictionary<string,string> upload = new Dictionary<string, string>();
57+
upload["text"] = "\"" + text + "\"";
58+
req.Send = Encoding.UTF8.GetBytes( Json.Serialize( upload ) );
59+
req.Data = data;
60+
req.Headers["Content-Type"] = "application/json";
61+
req.Parameters["version"] = "2016-02-11";
62+
req.Parameters["sentences"] = "true";
63+
return connector.Send(req);
64+
}
65+
66+
private class GetToneAnalyzerRequest : RESTConnector.Request
67+
{
68+
public string Data {get;set;}
69+
public OnGetToneAnalyzed Callback { get; set; }
70+
};
71+
72+
private void GetToneAnalyzerResponse(RESTConnector.Request req, RESTConnector.Response resp)
73+
{
74+
ToneAnalysis response = new ToneAnalysis();
75+
if (resp.Success)
76+
{
77+
try
78+
{
79+
fsData data = null;
80+
fsResult r = fsJsonParser.Parse(Encoding.UTF8.GetString(resp.Data), out data);
81+
if (!r.Succeeded)
82+
throw new WatsonException(r.FormattedMessages);
83+
84+
object obj = response;
85+
r = sm_Serializer.TryDeserialize(data, obj.GetType(), ref obj);
86+
if (!r.Succeeded)
87+
throw new WatsonException(r.FormattedMessages);
88+
}
89+
catch (Exception e)
90+
{
91+
Log.Error("ToneAnalyzer", "GetToneAnalyzerResponse Exception: {0}", e.ToString());
92+
resp.Success = false;
93+
}
94+
}
95+
96+
if (((GetToneAnalyzerRequest)req).Callback != null)
97+
((GetToneAnalyzerRequest)req).Callback(resp.Success ? response : null, ((GetToneAnalyzerRequest)req).Data);
98+
}
99+
100+
101+
#endregion
102+
103+
#region IWatsonService interface
104+
/// <exclude />
105+
public string GetServiceID()
106+
{
107+
return SERVICE_ID;
108+
}
109+
110+
/// <exclude />
111+
public void GetServiceStatus(ServiceStatus callback)
112+
{
113+
if ( Utilities.Config.Instance.FindCredentials( SERVICE_ID ) != null )
114+
new CheckServiceStatus( this, callback );
115+
else
116+
callback( SERVICE_ID, false );
117+
}
118+
119+
private class CheckServiceStatus
120+
{
121+
private ToneAnalyzer m_Service = null;
122+
private ServiceStatus m_Callback = null;
123+
124+
public CheckServiceStatus( ToneAnalyzer service, ServiceStatus callback )
125+
{
126+
m_Service = service;
127+
m_Callback = callback;
128+
129+
if (! m_Service.GetToneAnalyze( OnGetToneAnalyzed, "Test" ) )
130+
m_Callback( SERVICE_ID, false );
131+
}
132+
133+
private void OnGetToneAnalyzed( ToneAnalysis resp , string data)
134+
{
135+
if ( m_Callback != null )
136+
m_Callback( SERVICE_ID, resp != null );
137+
}
138+
};
139+
#endregion
140+
}
141+
}
142+

Scripts/Services/ToneAnalyzer/v3/ToneAnalyzer.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)