Assets/Watson/Scripts/Services/AlchemyAPI/AlchemyAPI.cs
/// <summary>
/// Extracts the keywords from a source.
/// </summary>
/// <returns><c>true</c>, if keywords was extracted, <c>false</c> otherwise.</returns>
/// <param name="callback">Callback.</param>
/// <param name="source">HTML, URL or Text source.</param>
/// <param name="maxRetrieve">Maximum results retreived.</param>
/// <param name="includeKnowledgeGraph">If set to <c>true</c> include knowledge graph.</param>
/// <param name="analyzeSentiment">If set to <c>true</c> analyze sentiment.</param>
/// <param name="includeSourceText">If set to <c>true</c> include source text.</param>
/// <param name="customData">Custom data.</param>
Extracts the keywords from a source.
Extracts the keywords from a source .
public bool ExtractKeywords(OnGetKeywords callback, string source,
            int maxRetrieve = 50,
            bool includeKnowledgeGraph = false,
            bool analyzeSentiment = false,
            bool includeSourceText = false,
            string customData = default(string))
        {
            if (callback == null)
                throw new ArgumentNullException("callback");
            if (string.IsNullOrEmpty(source))
                throw new WatsonException("Please provide a source for ExtractKeywords.");
            if (string.IsNullOrEmpty(mp_ApiKey))
                SetCredentials();
            GetKeywordsRequest req = new GetKeywordsRequest();
            req.Callback = callback;
            req.Data = string.IsNullOrEmpty(customData) ? source : customData;
            req.Parameters["apikey"] = mp_ApiKey;
            req.Parameters["outputMode"] = "json";
            req.Parameters["maxRetrieve"] = Convert.ToInt32(maxRetrieve).ToString();
            req.Parameters["knowledgeGraph"] = Convert.ToInt32(includeKnowledgeGraph).ToString();
            req.Parameters["sentiment"] = Convert.ToInt32(analyzeSentiment).ToString();
            req.Parameters["showSourceText"] = Convert.ToInt32(includeSourceText).ToString();
            req.Parameters["keywordExtractMode"] = "strict";
            req.Headers["Content-Type"] = "application/x-www-form-urlencoded";
            req.Forms = new Dictionary<string, RESTConnector.Form>();
            string service;
            string normalizedSource = source.Trim().ToLower();
            if (normalizedSource.StartsWith("http://") || normalizedSource.StartsWith("https://"))
            {
                service = SERVICE_GET_KEYWORD_EXTRACTION_URL;
                req.Forms["url"] = new RESTConnector.Form(source);
            }
            else if (source.StartsWith(Application.dataPath) && !normalizedSource.StartsWith("http://") && !normalizedSource.StartsWith("https://"))
            {
                if (Path.GetExtension(normalizedSource).EndsWith(".html"))
                {
                    service = SERVICE_GET_KEYWORD_EXTRACTION_HTML;
                    string htmlData = default(string);
                    htmlData = File.ReadAllText(source);
                    req.Forms["html"] = new RESTConnector.Form(htmlData);
                }
                else
                    throw new WatsonException("An HTML source is needed for Getkeywords!");
            }
            else
            {
                service = SERVICE_GET_KEYWORD_EXTRACTION_TEXT;
                req.Forms["text"] = new RESTConnector.Form(source);
            }
            RESTConnector connector = RESTConnector.GetConnector(SERVICE_ID, service);
            if (connector == null)
                return false;
            req.OnResponse = OnGetKeywordsResponse;
            return connector.Send(req);
        }
public bool ExtractKeywords ( OnGetKeywords callback , string source , int maxRetrieve = 50 , bool includeKnowledgeGraph = false , bool analyzeSentiment = false , bool includeSourceText = false , string customData = default ( string ) ) { if ( callback == null ) throw new ArgumentNullException ( " " ) ; if ( string . IsNullOrEmpty ( source ) ) throw new WatsonException ( " " ) ; if ( string . IsNullOrEmpty ( mp_ApiKey ) ) SetCredentials ( ) ; GetKeywordsRequest req = new GetKeywordsRequest ( ) ; req . Callback = callback ; req . Data = string . IsNullOrEmpty ( customData ) ? source : customData ; req . Parameters [ " " ] = mp_ApiKey ; req . Parameters [ " " ] = " " ; req . Parameters [ " " ] = Convert . ToInt32 ( maxRetrieve ) . ToString ( ) ; req . Parameters [ " " ] = Convert . ToInt32 ( includeKnowledgeGraph ) . ToString ( ) ; req . Parameters [ " " ] = Convert . ToInt32 ( analyzeSentiment ) . ToString ( ) ; req . Parameters [ " " ] = Convert . ToInt32 ( includeSourceText ) . ToString ( ) ; req . Parameters [ " " ] = " " ; req . Headers [ " " ] = " " ; req . Forms = new Dictionary < string , RESTConnector . Form > ( ) ; string service ; string normalizedSource = source . Trim ( ) . ToLower ( ) ; if ( normalizedSource . StartsWith ( " " ) || normalizedSource . StartsWith ( " " ) ) { service = SERVICE_GET_KEYWORD_EXTRACTION_URL ; req . Forms [ " " ] = new RESTConnector . Form ( source ) ; } else if ( source . StartsWith ( Application . dataPath ) && ! normalizedSource . StartsWith ( " " ) && ! normalizedSource . StartsWith ( " " ) ) { if ( Path . GetExtension ( normalizedSource ) . EndsWith ( " " ) ) { service = SERVICE_GET_KEYWORD_EXTRACTION_HTML ; string htmlData = default ( string ) ; htmlData = File . ReadAllText ( source ) ; req . Forms [ " " ] = new RESTConnector . Form ( htmlData ) ; } else throw new WatsonException ( " " ) ; } else { service = SERVICE_GET_KEYWORD_EXTRACTION_TEXT ; req . Forms [ " " ] = new RESTConnector . Form ( source ) ; } RESTConnector connector = RESTConnector . GetConnector ( SERVICE_ID , service ) ; if ( connector == null ) return false ; req . OnResponse = OnGetKeywordsResponse ; return connector . Send ( req ) ; }
Extracts the keywords from a source.
Extracts the keywords from a source .
None
callback
OnGetKeywords
None
source
string
HTML, URL or Text source.
HTML URL or Text source .
maxRetrieve
int
Maximum results retreived.
Maximum results retreived .
includeKnowledgeGraph
bool
If set to true
If set to true
analyzeSentiment
bool
If set to true
If set to true
includeSourceText
bool
If set to true
If set to true
customData
string
None