/// <summary> /// Example code to call Rosette API to get match score (similarity) for two names. /// 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: //matched_name yourapikeyhere if (args.Length != 0) { apikey = args[0]; alturl = args.Length > 1 ? args[1] : string.Empty; } try { CAPI MatchedNameCAPI = string.IsNullOrEmpty(alturl) ? new CAPI(apikey) : new CAPI(apikey, alturl); string matched_name_data1 = @"Michael Jackson"; string matched_name_data2 = @"迈克尔·杰克逊"; //The results of the API call will come back in the form of a Dictionary NameSimilarityResponse response = MatchedNameCAPI.NameSimilarity(new Name(matched_name_data1, "eng", null, "PERSON"), new Name(matched_name_data2, null, null, "PERSON")); 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 NameSimilarityTestFull() { Init(); double score = (double)0.9486632809417912; 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("score", score); NameSimilarityResponse expected = new NameSimilarityResponse(score, responseHeaders, content, null); String mockedContent = expected.ContentToString(); HttpResponseMessage mockedMessage = MakeMockedMessage(responseHeaders, HttpStatusCode.OK, mockedContent); _mockHttp.When(_testUrl + "name-similarity").Respond(mockedMessage); NameSimilarityResponse response = _rosetteApi.NameSimilarity(new Name("Влади́мир Влади́мирович Пу́тин", "rus", null, "PERSON"), new Name("Vladmir Putin", "eng", null, "PERSON")); Assert.AreEqual(expected, response); }