/// <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); } }
/// <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); } }
/// <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"; //You may set the API key via command line argument: //matched_name yourapikeyhere if (args.Length != 0) { apikey = args[0]; } try { CAPI MatchedNameCAPI = new CAPI(apikey); 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 Dictionary<string, Object> MatchedNameResult = MatchedNameCAPI.MatchedName(new Name(matched_name_data1, "eng", null, "PERSON"), new Name(matched_name_data2, null, null, "PERSON")); Console.WriteLine(new JavaScriptSerializer().Serialize(MatchedNameResult)); } catch (Exception e) { Console.WriteLine("Exception: " + e.Message); } }
/// <summary> /// Example code to call Rosette API to get information such as version and build. /// 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: //info yourapikeyhere if (args.Length != 0) { apikey = args[0]; } try { CAPI InfoCAPI = new CAPI(apikey); //The results of the API call will come back in the form of a Dictionary Dictionary<string, Object> infoResult = InfoCAPI.Info(); Console.WriteLine(new JavaScriptSerializer().Serialize(infoResult)); } catch (Exception e) { Console.WriteLine("Exception: " + e.Message); } }
/// <summary> /// Example code to call Rosette API to get tokens (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"; //You may set the API key via command line argument: //tokens yourapikeyhere if (args.Length != 0) { apikey = args[0]; } try { CAPI TokensCAPI = new CAPI(apikey); string tokens_data = @"北京大学生物系主任办公室内部会议"; //The results of the API call will come back in the form of a Dictionary Dictionary<string, Object> TokensResult = TokensCAPI.Tokens(tokens_data, null, null, "sentence", null); Console.WriteLine(new JavaScriptSerializer().Serialize(TokensResult)); } catch (Exception e) { Console.WriteLine("Exception: " + e.Message); } }
/// <summary> /// Example code to call Rosette API to get sentences 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"; //You may set the API key via command line argument: //relationships yourapikeyhere if (args.Length != 0) { apikey = args[0]; } try { CAPI RelationshipsCAPI = new CAPI(apikey); string relationships_text_data = @"Bill Murray is in the new Ghostbusters film!"; //The results of the API call will come back in the form of a Dictionary Dictionary<string, Object> RelationshipsResult = RelationshipsCAPI.Relationships(relationships_text_data); Console.WriteLine(new JavaScriptSerializer().Serialize(RelationshipsResult)); } catch (Exception e) { Console.WriteLine("Exception: " + e.Message); } }
/// <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"; //You may set the API key via command line argument: //morphology_parts_of_speech yourapikeyhere if (args.Length != 0) { apikey = args[0]; } try { CAPI MorphologyCAPI = new CAPI(apikey); 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 Dictionary<string, Object> MorphologyResult = MorphologyCAPI.Morphology(morphology_parts_of_speech_data, null, null, null, null, "parts-of-speech"); Console.WriteLine(new JavaScriptSerializer().Serialize(MorphologyResult)); } catch (Exception e) { Console.WriteLine("Exception: " + e.Message); } }
/// <summary> /// Example code to call Rosette API to get a document's sentiment /// 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: //sentiment yourapikeyhere if (args.Length != 0) { apikey = args[0]; } try { CAPI SentimentCAPI = new CAPI(apikey); var newFile = Path.GetTempFileName(); StreamWriter sw = new StreamWriter(newFile); string sentiment_file_data = @"<html><head><title>New Ghostbusters Film</title></head><body><p>Original Ghostbuster Dan Aykroyd, who also co-wrote the 1984 Ghostbusters film, couldn’t be more pleased with the new all-female Ghostbusters cast, telling The Hollywood Reporter, “The Aykroyd family is delighted by this inheritance of the Ghostbusters torch by these most magnificent women in comedy.”</p></body></html>"; sw.WriteLine(sentiment_file_data); sw.Flush(); sw.Close(); //Rosette API provides File upload options (shown here) //Simply create a new RosetteFile using the path to a file //The results of the API call will come back in the form of a Dictionary Dictionary<string, Object> SentimentResult = SentimentCAPI.Sentiment(new RosetteFile(newFile)); Console.WriteLine(new JavaScriptSerializer().Serialize(SentimentResult)); if (File.Exists(newFile)) { File.Delete(newFile); } } 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"; //You may set the API key via command line argument: //morphology_han_readings yourapikeyhere if (args.Length != 0) { apikey = args[0]; } try { CAPI MorphologyCAPI = new CAPI(apikey); string morphology_han_readings_data = @"北京大学生物系主任办公室内部会议"; //The results of the API call will come back in the form of a Dictionary Dictionary<string, Object> MorphologyResult = MorphologyCAPI.Morphology(morphology_han_readings_data, null, null, null, null, "han-readings"); Console.WriteLine(new JavaScriptSerializer().Serialize(MorphologyResult)); } catch (Exception e) { Console.WriteLine("Exception: " + e.Message); } }
/// <summary> /// Example code to call Rosette API to get de-compounded words 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: //morphology_compound_components yourapikeyhere if (args.Length != 0) { apikey = args[0]; } try { CAPI MorphologyCAPI = new CAPI(apikey); string morphology_compound_components_data = @"Rechtsschutzversicherungsgesellschaften"; //The results of the API call will come back in the form of a Dictionary Dictionary<string, Object> MorphologyResult = MorphologyCAPI.Morphology(morphology_compound_components_data, null, null, null, null, "compound-components"); Console.WriteLine(new JavaScriptSerializer().Serialize(MorphologyResult)); } catch (Exception e) { Console.WriteLine("Exception: " + e.Message); } }
/// <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"; //You may set the API key via command line argument: //morphology_complete yourapikeyhere if (args.Length != 0) { apikey = args[0]; } try { CAPI MorphologyCAPI = new CAPI(apikey); string morphology_complete_data = @"The quick brown fox jumped over the lazy dog. Yes he did."; //The results of the API call will come back in the form of a Dictionary Dictionary<string, Object> MorphologyResult = MorphologyCAPI.Morphology(morphology_complete_data); Console.WriteLine(new JavaScriptSerializer().Serialize(MorphologyResult)); } catch (Exception e) { Console.WriteLine("Exception: " + e.Message); } }
/// <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"; //You may set the API key via command line argument: //entities_linked yourapikeyhere if (args.Length != 0) { apikey = args[0]; } try { CAPI EntitiesLinkedCAPI = new CAPI(apikey); 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 Dictionary<string, Object> EntitiesLinkedResult = EntitiesLinkedCAPI.EntitiesLinked(entities_linked_text_data); Console.WriteLine(new JavaScriptSerializer().Serialize(EntitiesLinkedResult)); } catch (Exception e) { Console.WriteLine("Exception: " + e.Message); } }
/// <summary> /// Example code to call Rosette API to translate a name from language to another. /// 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: //translated_name yourapikeyhere if (args.Length != 0) { apikey = args[0]; } try { CAPI TranslatedNameCAPI = new CAPI(apikey); string translated_name_data = @"معمر محمد أبو منيار القذاف"; //The results of the API call will come back in the form of a Dictionary Dictionary<string, Object> TranslatedNameResult = TranslatedNameCAPI.TranslatedName("translated_name_data, null, null, "eng", null, null, null, "PERSON"); Console.WriteLine(new JavaScriptSerializer().Serialize(TranslatedNameResult)); } catch (Exception e) { Console.WriteLine("Exception: " + e.Message); } }
/// <summary> /// Example code to call Rosette API to detect possible languages 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"; //You may set the API key via command line argument: //language yourapikeyhere if (args.Length != 0) { apikey = args[0]; } try { CAPI LanguageCAPI = new CAPI(apikey); string language_data = @"Por favor Señorita, says the man."; //The results of the API call will come back in the form of a Dictionary Dictionary<string, Object> LanguageResult = LanguageCAPI.Language(language_data); Console.WriteLine(new JavaScriptSerializer().Serialize(LanguageResult)); } catch (Exception e) { Console.WriteLine("Exception: " + e.Message); } }
/// <summary> /// Example code to call Rosette API to get sentences 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"; //You may set the API key via command line argument: //sentences yourapikeyhere if (args.Length != 0) { apikey = args[0]; } try { CAPI SentencesCAPI = new CAPI(apikey); string sentences_data = @"This land is your land. This land is my land\nFrom California to the New York island;\nFrom the red wood forest to the Gulf Stream waters\n\nThis land was made for you and Me.\n\nAs I was walking that ribbon of highway,\nI saw above me that endless skyway:\nI saw below me that golden valley:\nThis land was made for you and me."; //The results of the API call will come back in the form of a Dictionary Dictionary<string, Object> SentencesResult = SentencesCAPI.Sentences(sentences_data); Console.WriteLine(new JavaScriptSerializer().Serialize(SentencesResult)); } catch (Exception e) { Console.WriteLine("Exception: " + e.Message); } }
public void Init() { _mockHttp = new MockHttpMessageHandler(); var client = new HttpClient(_mockHttp); string jsonResponse = string.Format("{{'response': 'OK', 'version': '{0}'}}", CAPI.Version); _mockHttp.When(_testUrl + "info") .WithQueryString(string.Format("clientVersion={0}", CAPI.Version)) .Respond("applciation/json", jsonResponse); _rosetteApi = new CAPI("userkey", null, 1, client); }
// uncomment [Test] to enable. It's not a true unit test, so it shouldn't run under regular conditions, but // it is useful for debugging. //[Test] public void testSentiment() { // provide a complete path to a test xls var excel = new ExcelQueryFactory(@"C:\Dev\Bindings\csharp\testdata.xls"); var worksheetNames = excel.GetWorksheetNames(); excel.ReadOnly = true; foreach (string name in worksheetNames) { var data = from cell in excel.Worksheet(name) select cell; foreach (int i in Enumerable.Range(0, 10)) { foreach (var cell in data) { CAPI rosetteApi = new CAPI("bogus_key", "https://api.rosette.com/rest/v1", 5); RosetteResponse result = rosetteApi.Sentiment(cell[0].Value.ToString()); System.Diagnostics.Debug.WriteLine(result.ContentAsJson); } } } }
public void Init() { // Create a temporary file for use with file testing _tmpFile = Path.GetTempFileName(); StreamWriter sw = File.AppendText(_tmpFile); sw.WriteLine("Rosette API Unit Test. This file is used for testing file operations."); sw.Flush(); sw.Close(); _mockHttp = new MockHttpMessageHandler(); var client = new HttpClient(_mockHttp); string jsonResponse = string.Format("{{'response': 'OK', 'version': '{0}'}}", CAPI.Version); _mockHttp.When(_testUrl + "info") .WithQueryString(string.Format("clientVersion={0}", CAPI.Version)) .Respond("applciation/json", jsonResponse); _rosetteApi = new CAPI("userkey", null, 1, client); }
//[Test] public void doTest() { string tmpFile = Path.GetTempFileName(); StreamWriter sw = File.AppendText(tmpFile); sw.WriteLine(@"<html><head><title>New Ghostbusters Film</title></head><body><p>Original Ghostbuster Dan Aykroyd, who also co-wrote the 1984 Ghostbusters film, couldn’t be more pleased with the new all-female Ghostbusters cast, telling The Hollywood Reporter, “The Aykroyd family is delighted by this inheritance of the Ghostbusters torch by these most magnificent women in comedy.”</p></body></html>"); sw.Flush(); sw.Close(); RosetteFile rf = new RosetteFile(tmpFile, "text/plain", @"{""language"":""eng""}"); CAPI rosetteApi = new CAPI("boguskey", "http://seuss.basistech.net:8181/rest/v1", 1); RosetteResponse result = rosetteApi.Sentiment(rf); System.Diagnostics.Debug.WriteLine(result.ContentAsJson); if (File.Exists(tmpFile)) { File.Delete(tmpFile); } }
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."); } } } }