using System.Text; using PdfService.Models; using System.IO; using System; using System.Linq; using System.Collections.Generic; namespace PdfService.Utility { public static class TemplateGenerator { // calculates standard deviation for list // from: https://stackoverflow.com/questions/3141692/standard-deviation-of-generic-list private static float CalculateStdDev(IEnumerable values) { double ret = 0; if (values.Count() > 0) { //Compute the Average double avg = values.Average(); //Perform the Sum of (value-avg)_2_2 double sum = values.Sum(d => Math.Pow(d - avg, 2)); //Put it all together ret = Math.Sqrt((sum) / (values.Count()-1)); } return (float)ret; } public static string GetHTMLString(Term[] terms) { var sb = new StringBuilder(); sb.AppendFormat(@"

Sentiment Analysis - Advanced Report

For terms: {0}

Overall statistics

Amount of terms: {1}
Mean sentiment value: {2:0.00}
Standard deviation: {3:0.00}
Date: {4}

Detail results:

Legend:
Negative Sentiment Value: 0-0.4
Neutral Sentiment Value: 0.4-0.6
Positive Sentiment Value: 0.6-1.0
", terms.Select(i => i.Name).Aggregate((i, j) => i + ',' + ' ' + j), terms.Length, terms.Select(i => i.Sentiment).Average(), CalculateStdDev(terms.Select(i => (double)i.Sentiment)), DateTime.Now, Path.Combine(Directory.GetCurrentDirectory(), "assets", "indico.png"), Path.Combine(Directory.GetCurrentDirectory(), "assets", "twitter.png")); foreach (Term term in terms) { string color = ""; if(term.Sentiment < 0.40) color = "is-danger"; else if(term.Sentiment < 0.60) color = "is-warning"; else color = "is-success"; sb.AppendFormat(@"

{0}

Calculated sentiment: {3:0.00}


{1}%
", term.Name, System.Math.Ceiling(term.Sentiment*100), color, term.Sentiment, Path.Combine(Directory.GetCurrentDirectory(), "assets", "thumbs_down.png"), Path.Combine(Directory.GetCurrentDirectory(), "assets", "thumbs_up.png")); } sb.Append(@"
"); return sb.ToString(); } } }