Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Łukasz Giergielewicz
Łukasz Giergielewicz
4,059 Points

Same score for every article

The application seems to be working correctly, however when checking the sentiment, the returned score always has a value of 0.5, but the web app tells me it is actually 77% when I paste the same text there. Has anyone any ideas what could be causing this to happen?

Method:

public static SentimentResponse GetSentimentResponse(List<NewsResult> newsResults)
        {
            var sentimentResponse = new SentimentResponse();
            var sentimentRequest = new SentimentRequest();
            sentimentRequest.Documents = new List<Document>();

            foreach(var result in newsResults)
            {
                sentimentRequest.Documents.Add(new Document { Id = result.Headline, Text = result.Summary });
            }

            var webClient = new WebClient();

            webClient.Headers["Ocp-Apim-Subscription-Key"] = TAaccesskey;
            webClient.Headers.Add(HttpRequestHeader.Accept, "application/json");
            webClient.Headers.Add(HttpRequestHeader.ContentType, "application/json");

            string requestJson = JsonConvert.SerializeObject(sentimentRequest);
            byte[] requestBytes = Encoding.UTF8.GetBytes(requestJson);
            byte[] response = 
 webClient.UploadData("https://westeurope.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment", requestBytes);

            string sentiments = Encoding.UTF8.GetString(response);
            sentimentResponse = JsonConvert.DeserializeObject<SentimentResponse>(sentiments);

            return sentimentResponse;
        }

Main:

foreach (var player in topTenPlayers)
            {
                Thread.Sleep(1000);
                List<NewsResult> newsResults = GetNewsForPlayer(string.Format("{0} {1}", player.FirstName, player.SecondName));

                if (newsResults.Any())
                {
                    SentimentResponse sentimentResponse = GetSentimentResponse(newsResults);
                    foreach (var sentiment in sentimentResponse.Sentiments)
                    {
                        foreach (var newsResult in newsResults)
                        {
                            if (newsResult.Headline == sentiment.Id)
                            {
                                double score;
                                if (double.TryParse(sentiment.Score, NumberStyles.Any, CultureInfo.GetCultureInfo("en-US"), out score))
                                {
                                    newsResult.SentimentScore = score;
                                }

                                break;
                            }
                        }
                    }

1 Answer

Łukasz Giergielewicz
Łukasz Giergielewicz
4,059 Points

Found the answer. There is a language parameter missing in the SentimentRequest class. You have to either first use text analysis to determine the language of the article, or define it yourself:

public class SentimentRequest
    {
        [JsonProperty(PropertyName = "documents")]
        public List<Document> Documents { get; set; }
    }

    public class Document
    {
        [JsonProperty(PropertyName = "id")]
        public string Id { get; set; }
        [JsonProperty(PropertyName = "text")]
        public string Text { get; set; }
        [JsonProperty(PropertyName = "language")]
        public string Language = "pl";

    }

This worked for me.