Skip to content

Instantly share code, notes, and snippets.

@prombouts
Created July 22, 2017 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prombouts/2c723620cb6ea6afc4d7a3df5dc3c40d to your computer and use it in GitHub Desktop.
Save prombouts/2c723620cb6ea6afc4d7a3df5dc3c40d to your computer and use it in GitHub Desktop.
using System;
using System.Configuration;
using Microsoft.ProjectOxford.Text.Sentiment;
using System.Xml;
using System.ServiceModel.Syndication;
using System.Collections.Generic;
using System.Linq;
namespace TextAnalysis
{
static class Program
{
static void Main()
{
GetSentiment(GetRssFeed("http://www.nu.nl/rss"));
Console.ReadLine();
}
/// <summary>
/// Takes 10 items from given feed
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private static List<SyndicationItem> GetRssFeed(string url)
{
XmlReader reader = XmlReader.Create(url);
SyndicationFeed feed = SyndicationFeed.Load(reader);
reader.Close();
return feed.Items.Take(10).ToList();
}
/// <summary>
/// Request sentiment analysis in Dutch for feeditems
/// </summary>
/// <param name="feedItems"></param>
static async void GetSentiment(List<SyndicationItem> feedItems)
{
var request = new SentimentRequest();
foreach(var item in feedItems)
{
request.Documents.Add(new SentimentDocument()
{
Id = item.Id,
Text = $"{item.Title.Text} {item.Summary.Text}",
Language = "nl"
});
}
var client = new SentimentClient(<Insert API key here>);
var response = await client.GetSentimentAsync(request);
foreach (var doc in response.Documents)
{
Console.WriteLine($" News item: {feedItems.First(f => f.Id == doc.Id).Title.Text}");
Console.WriteLine($" Scores: {doc.Score * 100}%");
Console.WriteLine("");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment