@@ -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 }
0 commit comments