コード例 #1
0
        /// <summary>
        /// Example code to call Rosette API to get a document's (located at given URL) categories.
        /// 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:
            //categories yourapikeyhere
            if (args.Length != 0)
            {
                apikey = args[0];
            }
            try
            {
                CAPI   CategoriesCAPI       = new CAPI(apikey);
                string categories_text_data = @"Sony Pictures is planning to shoot a good portion of the new " Ghostbusters " in Boston as well.";
                //The results of the API call will come back in the form of a Dictionary
                Dictionary <string, Object> CategoriesResult = CategoriesCAPI.Categories(null, null, null, null, categories_text_data);
                Console.WriteLine(new JavaScriptSerializer().Serialize(CategoriesResult));

                //Rosette API also supports Dictionary inputs
                //Simply instantiate a new dictionary object with the fields options as keys and inputs as values
                string categories_url_data = @"http://www.onlocationvacations.com/2015/03/05/the-new-ghostbusters-movie-begins-filming-in-boston-in-june/";
                Dictionary <string, Object> CategoriesResultDic = CategoriesCAPI.Categories(new Dictionary <object, object>()
                {
                    { "contentUri", categories_url_data }
                });
                Console.WriteLine(new JavaScriptSerializer().Serialize(CategoriesResultDic));
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
コード例 #2
0
        /// <summary>
        /// Example code to call Rosette API to get a document's (located at given URL) categories.
        /// 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:
            //categories yourapikeyhere
            if (args.Length != 0)
            {
                apikey = args[0];
                alturl = args.Length > 1 ? args[1] : string.Empty;
            }
            try
            {
                CAPI   CategoriesCAPI       = string.IsNullOrEmpty(alturl) ? new CAPI(apikey) : new CAPI(apikey, alturl);
                string categories_text_data = @"Sony Pictures is planning to shoot a good portion of the new ""Ghostbusters"" in Boston as well.";
                //The results of the API call will come back in the form of a Dictionary
                CategoriesResponse response = CategoriesCAPI.Categories(categories_text_data, null, null, null);
                Console.WriteLine(response.ContentAsJson);

                //Rosette API also supports Dictionary inputs
                //Simply instantiate a new dictionary object with the fields options as keys and inputs as values
                string categories_url_data = @"http://www.onlocationvacations.com/2015/03/05/the-new-ghostbusters-movie-begins-filming-in-boston-in-june/";
                response = CategoriesCAPI.Categories(new Dictionary <object, object>()
                {
                    { "contentUri", categories_url_data }
                });
                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(e.Message);
            }
        }
コード例 #3
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.");
                    }
                }
            }
        }