Skip to content

Commit 8320272

Browse files
author
Dogukan Erenel
committed
* Added POST on AlchmenyAPI
* Added Keyword Extraction to AlchemyAPI
1 parent eca3dff commit 8320272

File tree

2 files changed

+144
-4
lines changed

2 files changed

+144
-4
lines changed

Scripts/Services/AlchemyAPI/AlchemyAPI.cs

Lines changed: 91 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public class AlchemyAPI : IWatsonService {
4040
private static fsSerializer sm_Serializer = new fsSerializer();
4141
#endregion
4242

43+
4344
#region Entity Extraction
4445
private const string SERVICE_ENTITY_EXTRACTION = "/calls/text/TextGetRankedNamedEntities";
4546

@@ -124,6 +125,88 @@ private void OnGetEntityExtractionResponse(RESTConnector.Request req, RESTConnec
124125

125126
#endregion
126127

128+
#region Keywoard Extraction
129+
130+
private const string SERVICE_KEYWOARD_EXTRACTION = "/calls/text/TextGetRankedKeywords";
131+
132+
public delegate void OnGetKeywoardExtraction( KeywoardExtractionData entityExtractionData, string data );
133+
134+
public bool GetKeywoardExtraction(OnGetKeywoardExtraction callback, string text, string customData = null)
135+
{
136+
if (callback == null)
137+
throw new ArgumentNullException("callback");
138+
if (string.IsNullOrEmpty(text))
139+
throw new WatsonException("GetKeywoardExtraction needs to have some text to work.");
140+
if (string.IsNullOrEmpty(mp_ApiKey))
141+
mp_ApiKey = Config.Instance.GetVariableValue("ALCHEMY_API_KEY");
142+
if (string.IsNullOrEmpty(mp_ApiKey))
143+
throw new WatsonException("GetKeywoardExtraction - ALCHEMY_API_KEY needs to be defined in config.json");
144+
145+
146+
RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, SERVICE_KEYWOARD_EXTRACTION);
147+
if (connector == null)
148+
return false;
149+
150+
GetKeywoardExtractionRequest req = new GetKeywoardExtractionRequest();
151+
req.Callback = callback;
152+
153+
req.Parameters["apikey"] = mp_ApiKey;
154+
//req.Parameters["text"] = text;
155+
req.Headers["Content-Type"] = "application/x-www-form-urlencoded";
156+
req.Forms = new Dictionary<string, RESTConnector.Form>();
157+
req.Forms["text"] = new RESTConnector.Form(text);
158+
159+
req.Parameters["url"] = "";
160+
req.Parameters["maxRetrieve"] = "1000";
161+
req.Parameters["keywordExtractMode"] = "strict"; //strict , normal
162+
req.Parameters["sentiment"] = "1";
163+
req.Parameters["outputMode"] = "json";
164+
req.Parameters["showSourceText"] = "1";
165+
//req.Parameters["baseUrl"] = "";
166+
req.Parameters["knowledgeGraph"] = "0";
167+
168+
req.OnResponse = OnGetKeywoardExtractionResponse;
169+
req.Data = string.IsNullOrEmpty(customData)? text : customData;
170+
171+
return connector.Send(req);
172+
}
173+
174+
private class GetKeywoardExtractionRequest : RESTConnector.Request
175+
{
176+
public string Data { get; set;}
177+
public OnGetKeywoardExtraction Callback { get; set; }
178+
};
179+
180+
private void OnGetKeywoardExtractionResponse(RESTConnector.Request req, RESTConnector.Response resp)
181+
{
182+
KeywoardExtractionData keywoardExtractionData = new KeywoardExtractionData();
183+
if (resp.Success)
184+
{
185+
try
186+
{
187+
fsData data = null;
188+
fsResult r = fsJsonParser.Parse(Encoding.UTF8.GetString(resp.Data), out data);
189+
if (!r.Succeeded)
190+
throw new WatsonException(r.FormattedMessages);
191+
192+
object obj = keywoardExtractionData;
193+
r = sm_Serializer.TryDeserialize(data, obj.GetType(), ref obj);
194+
if (!r.Succeeded)
195+
throw new WatsonException(r.FormattedMessages);
196+
}
197+
catch (Exception e)
198+
{
199+
Log.Error("AlchemyAPI", "OnGetKeywoardExtractionResponse Exception: {0}", e.ToString());
200+
resp.Success = false;
201+
}
202+
}
203+
204+
if (((GetKeywoardExtractionRequest)req).Callback != null)
205+
((GetKeywoardExtractionRequest)req).Callback(resp.Success ? keywoardExtractionData : null, ((GetKeywoardExtractionRequest)req).Data);
206+
}
207+
208+
#endregion
209+
127210
#region Combined Call
128211

129212
private const string SERVICE_COMBINED_CALLS = "/calls/text/TextGetCombinedData";
@@ -145,7 +228,8 @@ public bool GetCombinedCall(OnGetCombinedCall callback, string text,
145228
bool includeDocSentiment = false,
146229
bool includePageImage = false,
147230
bool includeImageKW = false,
148-
string language = "english")
231+
string language = "english",
232+
string customData = null)
149233
{
150234
if (callback == null)
151235
throw new ArgumentNullException("callback");
@@ -203,14 +287,18 @@ public bool GetCombinedCall(OnGetCombinedCall callback, string text,
203287
requestServices.Add("image-kw");
204288

205289
req.Parameters["apikey"] = mp_ApiKey;
206-
req.Parameters["text"] = text;
290+
//req.Parameters["text"] = text;
207291
req.Parameters["extract"] = string.Join(",", requestServices.ToArray());
208292
req.Parameters["outputMode"] = "json";
209293
req.Parameters["showSourceText"] = "1";
210294
req.Parameters["language"] = language;
211295

296+
req.Headers["Content-Type"] = "application/x-www-form-urlencoded";
297+
req.Forms = new Dictionary<string, RESTConnector.Form>();
298+
req.Forms["text"] = new RESTConnector.Form(text);
299+
212300
req.OnResponse = OnGetCombinedCallResponse;
213-
req.Data = text;
301+
req.Data = string.IsNullOrEmpty(customData)? text : customData;
214302

215303
return connector.Send(req);
216304
}

Scripts/Services/AlchemyAPI/DataModels.cs

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
namespace IBM.Watson.DeveloperCloud.Services.AlchemyAPI.v1
2323
{
24-
24+
2525
[fsObject]
2626
public class EntityExtractionData
2727
{
@@ -30,6 +30,7 @@ public class EntityExtractionData
3030
public string url { get; set; }
3131
public string text { get; set; }
3232
public Entity[] entities { get; set; }
33+
public KnowledgeGraph knowledgeGraph{ get; set; }
3334

3435
public bool HasData
3536
{
@@ -105,6 +106,39 @@ public override string ToString()
105106

106107
};
107108

109+
110+
[fsObject]
111+
public class KeywoardExtractionData
112+
{
113+
public string status { get; set; }
114+
public string language { get; set; }
115+
public string url { get; set; }
116+
public string text { get; set; }
117+
public Keyword[] keywords { get; set; }
118+
public KnowledgeGraph knowledgeGraph{ get; set; }
119+
120+
public bool HasData
121+
{
122+
get
123+
{
124+
return keywords != null && keywords.Length > 0;
125+
}
126+
}
127+
128+
public override string ToString()
129+
{
130+
StringBuilder stringBuilder = new StringBuilder();
131+
for (int indexKeyword = 0; keywords != null && indexKeyword < keywords.Length; indexKeyword++)
132+
{
133+
stringBuilder.Append("\n\t");
134+
stringBuilder.Append(keywords[indexKeyword].ToString());
135+
}
136+
return string.Format("[KeywoardExtractionData: status={0}, language={1}, url={2}, text={3}, keywords={4}]", status, language, url, text, stringBuilder.ToString());
137+
}
138+
139+
};
140+
141+
108142
public class PositionOnMap
109143
{
110144
public string PositionName;
@@ -765,6 +799,23 @@ public class Sentiment
765799
public string score { get; set; }
766800
public string mixed { get; set; }
767801

802+
private double m_Score = 0;
803+
public double Score
804+
{
805+
get
806+
{
807+
if (m_Score == 0)
808+
{
809+
if (!string.IsNullOrEmpty(score))
810+
{
811+
double.TryParse(score, out m_Score);
812+
}
813+
}
814+
815+
return m_Score;
816+
}
817+
}
818+
768819
public override string ToString()
769820
{
770821
return string.Format("[Sentiment: type={0}, score={1}, mixed={2}]", type, score, mixed);
@@ -857,6 +908,7 @@ public class Keyword
857908

858909
};
859910

911+
860912
[fsObject]
861913
public class Concept
862914
{

0 commit comments

Comments
 (0)