/// <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); } }
// 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 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."); } } } }
//[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); } }