Example #1
0
        /// <summary>
        /// Example code to call Rosette API to get entities from a piece of text.
        /// Requires Nuget Package:
        /// rosette_api
        /// </summary>
        static void Main(string[] args)
        {
            //To use the C# API, you must provide an API key
            string apikey = "Your API key";
            string alturl = string.Empty;

            //You may set the API key via command line argument:
            //entities yourapikeyhere
            if (args.Length != 0)
            {
                apikey = args[0];
                alturl = args.Length > 1 ? args[1] : string.Empty;
            }
            try
            {
                CAPI   EntitiesCAPI       = string.IsNullOrEmpty(alturl) ? new CAPI(apikey) : new CAPI(apikey, alturl);
                string entities_text_data = @"Bill Murray will appear in new Ghostbusters film: Dr. Peter Venkman was spotted filming a cameo in Boston this… http://dlvr.it/BnsFfS";
                //The results of the API call will come back in the form of a Dictionary
                EntitiesResponse response = EntitiesCAPI.Entity(entities_text_data, null, null, null, false, "social-media");
                foreach (KeyValuePair <string, string> h in response.Headers)
                {
                    Console.WriteLine(string.Format("{0}:{1}", h.Key, h.Value));
                }
                Console.WriteLine(response.ToString());
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
        }
Example #2
0
        /// <summary>
        /// Example code to call Rosette API to get linked (against Wikipedia) entities from a piece of text.
        /// Requires Nuget Package:
        /// rosette_api
        /// </summary>
        static void Main(string[] args)
        {
            //To use the C# API, you must provide an API key
            string apikey = "Your API key";
            string alturl = string.Empty;

            //You may set the API key via command line argument:
            //entities_linked yourapikeyhere
            if (args.Length != 0)
            {
                apikey = args[0];
                alturl = args.Length > 1 ? args[1] : string.Empty;
            }
            try
            {
                CAPI   EntitiesLinkedCAPI        = string.IsNullOrEmpty(alturl) ? new CAPI(apikey) : new CAPI(apikey, alturl);
                string entities_linked_text_data = @"Last month director Paul Feig announced the movie will have an all-star female cast including Kristen Wiig, Melissa McCarthy, Leslie Jones and Kate McKinnon.";
                //The results of the API call will come back in the form of a Dictionary
                RosetteResponse response = EntitiesLinkedCAPI.Entity(entities_linked_text_data, null, null, null, true, "social-media");
                foreach (KeyValuePair <string, string> h in response.Headers)
                {
                    Console.WriteLine(string.Format("{0}:{1}", h.Key, h.Value));
                }
                Console.WriteLine(response.ContentAsJson);
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
        }
Example #3
0
        /// <summary>
        /// Example code to call Rosette API to get entities from a piece of text.
        /// Requires Nuget Package:
        /// rosette_api
        /// </summary>
        static void Main(string[] args)
        {
            //To use the C# API, you must provide an API key
            string apikey = "Your API key";

            //You may set the API key via command line argument:
            //entities yourapikeyhere
            if (args.Length != 0)
            {
                apikey = args[0];
            }
            try
            {
                CAPI   EntitiesCAPI       = new CAPI(apikey);
                string entities_text_data = @"Bill Murray will appear in new Ghostbusters film: Dr. Peter Venkman was spotted filming a cameo in Boston this… http://dlvr.it/BnsFfS";
                var    exampleBytes       = System.Text.Encoding.UTF8.GetBytes(entities_text_data);
                String exampleText        = System.Convert.ToBase64String(exampleBytes);
                //The results of the API call will come back in the form of a Dictionary
                Dictionary <string, Object> EntitiesResult = EntitiesCAPI.Entity(exampleText);
                Console.WriteLine(new JavaScriptSerializer().Serialize(EntitiesResult));
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
        }
Example #4
0
        /// <summary>
        /// Example code to call Rosette API to get entities from a piece of text.
        /// Requires Nuget Package:
        /// rosette_api
        /// </summary>
        static void Main(string[] args)
        {
            //To use the C# API, you must provide an API key
            string apikey = "Your API key";
            string alturl = string.Empty;

            //You may set the API key via command line argument:
            //entities yourapikeyhere
            if (args.Length != 0)
            {
                apikey = args[0];
                alturl = args.Length > 1 ? args[1] : string.Empty;
            }
            try
            {
                CAPI   EntitiesCAPI       = string.IsNullOrEmpty(alturl) ? new CAPI(apikey) : new CAPI(apikey, alturl);
                string entities_text_data = @"The Securities and Exchange Commission today announced the leadership of the agency’s trial unit.  Bridget Fitzpatrick has been named Chief Litigation Counsel of the SEC and David Gottesman will continue to serve as the agency’s Deputy Chief Litigation Counsel. Since December 2016, Ms. Fitzpatrick and Mr. Gottesman have served as Co-Acting Chief Litigation Counsel.  In that role, they were jointly responsible for supervising the trial unit at the agency’s Washington D.C. headquarters as well as coordinating with litigators in the SEC’s 11 regional offices around the country.";
                //The results of the API call will come back in the form of a Dictionary
                EntitiesResponse response = EntitiesCAPI.Entity(entities_text_data, null, null, null, "social-media");
                foreach (KeyValuePair <string, string> h in response.Headers)
                {
                    Console.WriteLine(string.Format("{0}:{1}", h.Key, h.Value));
                }
                Console.WriteLine(response.ToString());

                // Entities with full ADM
                EntitiesCAPI.SetUrlParameter("output", "rosette");
                response = EntitiesCAPI.Entity(entities_text_data, null, null, null, "social-media");
                // response.Content contains the IDictionary results of the full ADM.
                // PrintContent() is a provided method to print the Dictionary to the console
                response.PrintContent();
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
        }
Example #5
0
        private static Task runLookup(int taskId, CAPI api)
        {
            string entities_text_data = @"The Securities and Exchange Commission today announced the leadership of the agency’s trial unit.  Bridget Fitzpatrick has been named Chief Litigation Counsel of the SEC and David Gottesman will continue to serve as the agency’s Deputy Chief Litigation Counsel. Since December 2016, Ms. Fitzpatrick and Mr. Gottesman have served as Co-Acting Chief Litigation Counsel.  In that role, they were jointly responsible for supervising the trial unit at the agency’s Washington D.C. headquarters as well as coordinating with litigators in the SEC’s 11 regional offices around the country.";

            //string contentUri = "http://www.foxsports.com/olympics/story/chad-le-clos-showed-why-you-never-talk-trash-to-michael-phelps-080916";
            foreach (int call in Enumerable.Range(0, calls))
            {
                Console.WriteLine("Task ID: {0} call {1}", taskId, call);
                try {
                    var result = api.Entity(content: entities_text_data);
                    Console.WriteLine("Concurrency: {0},Rresult: {1}", api.Concurrency, result);
                }
                catch (Exception ex) {
                    Console.WriteLine(ex);
                }
            }
            return(Task.CompletedTask);
        }
        private static Task runLookup(int taskId, CAPI api)
        {
            string entities_text_data = @"Bill Murray will appear in new Ghostbusters film: Dr. Peter Venkman was spotted filming a cameo in Boston this… http://dlvr.it/BnsFfS";

            //string contentUri = "http://www.foxsports.com/olympics/story/chad-le-clos-showed-why-you-never-talk-trash-to-michael-phelps-080916";
            foreach (int call in Enumerable.Range(0, calls))
            {
                Console.WriteLine("Task ID: {0} call {1}", taskId, call);
                try {
                    var result = api.Entity(content: entities_text_data);
                    Console.WriteLine("Concurrency: {0},Rresult: {1}", api.Concurrency, result);
                }
                catch (Exception ex) {
                    Console.WriteLine(ex);
                }
            }
            return(Task.CompletedTask);
        }
Example #7
0
        public void CAPITest()
        {
            List <TestDataStructure> mockData = Setup();
            CMockData cmd = new CMockData();

            foreach (TestDataStructure td in mockData)
            {
                System.Diagnostics.Debug.WriteLine("Evaluating " + td.outputDataFilename);
                Console.WriteLine("Evaluating " + td.outputDataFilename);
                Dictionary <object, object> tdInputData = null;
                if (td.inpFilename != null)
                {
                    string tdInput = cmd.getFileData(td.inpFilename);
                    if (tdInput != null)
                    {
                        tdInputData = new JavaScriptSerializer().Deserialize <Dictionary <object, object> >(tdInput);
                    }
                }

                Dictionary <string, object> result = new Dictionary <string, object>();

                var    fakeResponse = new HttpResponseMessage();
                string tdStatus     = cmd.getFileData(td.outputStatusFilename);
                fakeResponse.StatusCode = (HttpStatusCode)(Convert.ToInt32(tdStatus));

                string tdOutput = cmd.getFileData(td.outputDataFilename);
                if (tdOutput.Length > 200)
                {
                    System.Diagnostics.Debug.WriteLine("GZipping");
                    byte[] compress = Compress(Encoding.ASCII.GetBytes(tdOutput));
                    fakeResponse.Content = new ByteArrayContent(compress);
                    fakeResponse.Content.Headers.ContentEncoding.Add("gzip");
                    MemoryStream stream = new MemoryStream(Decompress(compress));
                    StreamReader reader = new StreamReader(stream);
                    tdOutput = reader.ReadToEnd();
                }
                else
                {
                    fakeResponse.Content = new StringContent(tdOutput);
                }

                var        fakeHandler = new FakeHttpMessageHandler(fakeResponse, td.inpFilename);
                HttpClient httpClient  = new HttpClient(fakeHandler);
                CAPI       c           = new CAPI(td.inpFilename, null, 3, httpClient);

                string morphofeature = null;
                if (td.endpoint.IndexOf("morphology") != -1)
                {
                    morphofeature = td.endpoint.Remove(0, td.endpoint.IndexOf("/"));
                    td.endpoint   = td.endpoint.Remove(td.endpoint.IndexOf("/"));
                }
                try
                {
                    switch (td.endpoint)
                    {
                    case "categories":
                        result = c.Categories(tdInputData);
                        break;

                    case "entities":
                        result = c.Entity(tdInputData);
                        break;

                    case "entities/linked":
                        result = c.EntitiesLinked(tdInputData);
                        break;

                    case "info":
                        result = c.Info();
                        break;

                    case "language":
                        result = c.Entity(tdInputData);
                        break;

                    case "morphology":
                        result = c.Morphology(tdInputData, morphofeature);
                        break;

                    case "name":
                        if (td.inpFilename.Contains("matched"))
                        {
                            result = c.MatchedName(tdInputData);
                        }
                        else
                        {
                            result = c.TranslatedName(tdInputData);
                        }
                        break;

                    case "ping":
                        result = c.Ping();
                        break;

                    case "relationships":
                        result = c.Relationships(tdInputData);
                        break;

                    case "sentences":
                        result = c.Sentences(tdInputData);
                        break;

                    case "sentiment":
                        result = c.Sentiment(tdInputData);
                        break;

                    case "tokens":
                        result = c.Tokens(tdInputData);
                        break;
                    }
                }
                catch (RosetteException e)
                {
                    result = new Dictionary <string, object>()
                    {
                        { "message", e.Message },
                        { "code", e.Code },
                        { "requestId", e.RequestID },
                        { "file", e.File },
                        { "line", e.Line }
                    };
                }
                Dictionary <string, object> outputDict = new JavaScriptSerializer().Deserialize <Dictionary <string, object> >(tdOutput);

                if (result.Keys.Contains("file"))
                {
                    Assert.AreEqual(result["code"], Convert.ToInt32(tdStatus));
                    System.Diagnostics.Debug.WriteLine("Status matched");
                    Console.WriteLine("Status matched");
                }
                else
                {
                    bool allResultsMatched = true;
                    foreach (string key in outputDict.Keys)
                    {
                        if (key != "requestId")
                        {
                            Assert.IsTrue(result.Keys.Contains(key));
                            if (result.Keys.Contains(key))
                            {
                                System.Diagnostics.Debug.WriteLine("Key found");
                            }
                            else
                            {
                                System.Diagnostics.Debug.WriteLine("Key NOT found");
                                allResultsMatched = false;
                            }
                            if (result[key] is Object)
                            {
                                Assert.AreEqual(new JavaScriptSerializer().Serialize(result[key]), new JavaScriptSerializer().Serialize(outputDict[key]));
                                if (new JavaScriptSerializer().Serialize(result[key]) == new JavaScriptSerializer().Serialize(outputDict[key]))
                                {
                                    System.Diagnostics.Debug.WriteLine("Results Matched");
                                }
                                else
                                {
                                    System.Diagnostics.Debug.WriteLine("Results NOT Matched");
                                    allResultsMatched = false;
                                }
                            }
                            else
                            {
                                Assert.AreEqual(outputDict[key], result[key]);
                                if (outputDict[key] == result[key])
                                {
                                    System.Diagnostics.Debug.WriteLine("Results Matched");
                                }
                                else
                                {
                                    System.Diagnostics.Debug.WriteLine("Results NOT Matched");
                                    allResultsMatched = false;
                                }
                            }
                        }
                    }
                    if (allResultsMatched)
                    {
                        Console.WriteLine("All results matched");
                    }
                    else
                    {
                        throw new Exception("An Error occurred during the test.");
                    }
                }
            }
        }