示例#1
0
        /// <summary>
        /// Example code to call Rosette API to get part-of-speech tags for words 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:
            //morphology_parts_of_speech yourapikeyhere
            if (args.Length != 0)
            {
                apikey = args[0];
                alturl = args.Length > 1 ? args[1] : string.Empty;
            }
            try
            {
                CAPI   MorphologyCAPI = string.IsNullOrEmpty(alturl) ? new CAPI(apikey) : new CAPI(apikey, alturl);
                string morphology_parts_of_speech_data = @"The fact is that the geese just went back to get a rest and I'm not banking on their return soon";
                //The results of the API call will come back in the form of a Dictionary
                MorphologyResponse response = MorphologyCAPI.Morphology(morphology_parts_of_speech_data, null, null, null, MorphologyFeature.partsOfSpeech);
                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);
            }
        }
        /// <summary>
        /// Example code to call Rosette API to get Chinese readings for words in 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:
            //morphology_han_readings yourapikeyhere
            if (args.Length != 0)
            {
                apikey = args[0];
                alturl = args.Length > 1 ? args[1] : string.Empty;
            }
            try
            {
                CAPI   MorphologyCAPI = string.IsNullOrEmpty(alturl) ? new CAPI(apikey) : new CAPI(apikey, alturl);
                string morphology_han_readings_data = @"北京大学生物系主任办公室内部会议";
                //The results of the API call will come back in the form of a Dictionary
                MorphologyResponse response = MorphologyCAPI.Morphology(morphology_han_readings_data, null, null, null, MorphologyFeature.hanReadings);
                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);
            }
        }
示例#3
0
        /// <summary>
        /// Example code to call Rosette API to get the complete set of morphological analysis results for 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:
            //morphology_complete yourapikeyhere
            if (args.Length != 0)
            {
                apikey = args[0];
                alturl = args.Length > 1 ? args[1] : string.Empty;
            }
            try
            {
                CAPI   MorphologyCAPI           = string.IsNullOrEmpty(alturl) ? new CAPI(apikey) : new CAPI(apikey, alturl);
                string morphology_complete_data = @"The quick brown fox jumped over the lazy dog. 👍🏾 Yes he did. B)";
                //The results of the API call will come back in the form of a Dictionary
                MorphologyResponse response = MorphologyCAPI.Morphology(morphology_complete_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("Exception: " + e.Message);
            }
        }
 public void MorphologyTestFullLemmas()
 {
     Init();
     MorphologyItem m0 = new MorphologyItem("The", null, "the", null, null);
     MorphologyItem m1 = new MorphologyItem("quick", null, "quick", null, null);
     MorphologyItem m2 = new MorphologyItem("brown", null, "brown", null, null);
     MorphologyItem m3 = new MorphologyItem("fox", null, "fox", null, null);
     MorphologyItem m4 = new MorphologyItem("jumped", null, "jump", null, null);
     MorphologyItem m5 = new MorphologyItem(".", null, ".", null, null);
     List<MorphologyItem> morphology = new List<MorphologyItem>() { m0, m1, m2, m3, m4, m5 };
     string headersAsString = " { \"Content-Type\": \"application/json\", \"date\": \"Thu, 11 Aug 2016 15:47:32 GMT\", \"server\": \"openresty\", \"strict-transport-security\": \"max-age=63072000; includeSubdomains; preload\", \"x-rosetteapi-app-id\": \"1409611723442\", \"x-rosetteapi-concurrency\": \"50\", \"x-rosetteapi-request-id\": \"d4176692-4f14-42d7-8c26-4b2d8f7ff049\", \"content-length\": \"72\", \"connection\": \"Close\" }";
     Dictionary<string, string> responseHeaders = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(headersAsString);
     Dictionary<string, object> content = new Dictionary<string, object>();
     content.Add("tokens", new List<string>(morphology.Select<MorphologyItem, string>((item) => item.Token)));;
     content.Add("lemmas", new List<string>(morphology.Select<MorphologyItem, string>((item) => item.Lemma)));
     MorphologyResponse expected = new MorphologyResponse(morphology, responseHeaders, content, null);
     String mockedContent = expected.ContentAsJson;
     HttpResponseMessage mockedMessage = MakeMockedMessage(responseHeaders, HttpStatusCode.OK, mockedContent);
     _mockHttp.When(_testUrl + "morphology/lemmas").Respond(mockedMessage);
     MorphologyResponse response = _rosetteApi.Morphology("The quick brown fox jumped.", feature: MorphologyFeature.lemmas);
     Assert.AreEqual(expected, response);
 }
 public void MorphologyTestFullHanReadings()
 {
     Init();
     List<string> h0 = new List<string>() { "Bei3-jing1-Da4-xue2" };
     List<string> h1 = null;
     List<string> h2 = new List<string>() { "zhu3-ren4" };
     MorphologyItem m0 = new MorphologyItem("北京大学", null, null, null, h0);
     MorphologyItem m1 = new MorphologyItem("生物系", null, null, null, h1);
     MorphologyItem m2 = new MorphologyItem("主任", null, null, null, h2);
     List<MorphologyItem> morphology = new List<MorphologyItem>() { m0, m1, m2 };
     string headersAsString = " { \"Content-Type\": \"application/json\", \"date\": \"Thu, 11 Aug 2016 15:47:32 GMT\", \"server\": \"openresty\", \"strict-transport-security\": \"max-age=63072000; includeSubdomains; preload\", \"x-rosetteapi-app-id\": \"1409611723442\", \"x-rosetteapi-concurrency\": \"50\", \"x-rosetteapi-request-id\": \"d4176692-4f14-42d7-8c26-4b2d8f7ff049\", \"content-length\": \"72\", \"connection\": \"Close\" }";
     Dictionary<string, string> responseHeaders = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(headersAsString);
     Dictionary<string, object> content = new Dictionary<string, object>();
     content.Add("tokens", new List<string>(morphology.Select<MorphologyItem, string>((item) => item.Token))); ;
     content.Add("hanReadings", new List<List<string>>(morphology.Select<MorphologyItem, List<string>>((item) => item.HanReadings)));
     MorphologyResponse expected = new MorphologyResponse(morphology, responseHeaders, content, null);
     String mockedContent = expected.ContentAsJson;
     HttpResponseMessage mockedMessage = MakeMockedMessage(responseHeaders, HttpStatusCode.OK, mockedContent);
     _mockHttp.When(_testUrl + "morphology/han-readings").Respond(mockedMessage);
     MorphologyResponse response = _rosetteApi.Morphology("北京大学生物系主任.", feature: MorphologyFeature.hanReadings);
     Assert.AreEqual(expected, response);
 }
 public void MorphologyTestFullCompoundComponents()
 {
     Init();
     MorphologyItem m0 = new MorphologyItem("Er", null, null, new List<string>(), null);
     List<string> compoundComponents = new List<string>() { "Rechts", "Schutz", "Versicherungs", "Gesellschaft" };
     MorphologyItem m1 = new MorphologyItem("Rechtsschutzversicherungsgesellschaft", null, null, compoundComponents, null);
     List<MorphologyItem> morphology = new List<MorphologyItem>() { m0, m1 };
     string headersAsString = " { \"Content-Type\": \"application/json\", \"date\": \"Thu, 11 Aug 2016 15:47:32 GMT\", \"server\": \"openresty\", \"strict-transport-security\": \"max-age=63072000; includeSubdomains; preload\", \"x-rosetteapi-app-id\": \"1409611723442\", \"x-rosetteapi-concurrency\": \"50\", \"x-rosetteapi-request-id\": \"d4176692-4f14-42d7-8c26-4b2d8f7ff049\", \"content-length\": \"72\", \"connection\": \"Close\" }";
     Dictionary<string, string> responseHeaders = new JavaScriptSerializer().Deserialize<Dictionary<string, string>>(headersAsString);
     Dictionary<string, object> content = new Dictionary<string, object>();
     content.Add("tokens", new List<string>(morphology.Select<MorphologyItem, string>((item) => item.Token))); ;
     content.Add("compoundComponents", new List<List<string>>(morphology.Select<MorphologyItem, List<string>>((item) => item.CompoundComponents)));
     MorphologyResponse expected = new MorphologyResponse(morphology, responseHeaders, content, null);
     String mockedContent = expected.ContentAsJson;
     HttpResponseMessage mockedMessage = MakeMockedMessage(responseHeaders, HttpStatusCode.OK, mockedContent);
     _mockHttp.When(_testUrl + "morphology/compound-components").Respond(mockedMessage);
     MorphologyResponse response = _rosetteApi.Morphology("Er Rechtsschutzversicherungsgesellschaft.", feature: MorphologyFeature.compoundComponents);
     Assert.AreEqual(expected, response);
 }