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<double> values) { double ret = 0; if (values.Count() > 1) { //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.Append(@"<!doctype html> <html> <head> <meta charset='utf-8'> <meta name='viewport' content='initial-scale=1.0, user-scalable=no' /> </head> <body> <section class='hero is-info' style='padding: 3em 3em 0em 3em;'> <div class='hero-body'> <div class='container'> <h1 class='title'> Sentiment Analysis - Advanced Report </h1> <p class='subtitle'>"); if(terms.Length == 1) { sb.AppendFormat(@"For term: {0}", terms[0].Name); } else { sb.AppendFormat(@"For terms: {0}", terms.Select(i => i.Name).Aggregate((i, j) => i + ',' + ' ' + j)); } sb.AppendFormat(@" </p> </div> </section> <div class='notification is-info' style='padding-left:5em; padding-right:5em'> <table style='padding-left:2em'> <td> <tr> <img src='{6}' style='height:2em; width: auto;padding-right:2em;'/> <img src='{5}' style='height:2em; width: auto;padding-right:2em;'/> </tr> </td> </table> </div> <div class='container' style='padding-left:5em; padding-right:5em'> <h1 class='subtitle is-3'>Overall statistics:</h1> Amount of terms: <strong>{1}</strong><br/> Mean sentiment value: <strong>{2:0.00}</strong><br/> Standard deviation: <strong>{3:0.00}</strong><br/> Date: <strong>{4}</strong><br/> <hr/> <h1 class='subtitle is-3'>Detail results:</h1> <div class='notification'> <a>Legend:</a> <br/> <span class='tag is-link is-danger' style='margin-right:0.5em'></span> Negative Sentiment Value: <strong>0-0.4</strong><br/> <span class='tag is-link is-warning'style='margin-right:0.5em'></span> Neutral Sentiment Value: <strong>0.4-0.6</strong><br/> <span class='tag is-link is-success'style='margin-right:0.5em'></span> Positive Sentiment Value: <strong>0.6-1.0</strong><br/> </div> ", 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(@"<div class='container' style='margin-top:3em;'> <p class='subtitle is-4'>{0}</p> <p style='margin-top:-1.0em; margin-left: 2em;'>Calculated sentiment: {3:0.00}</p> <br/> <table class='table' style='width:100%; border: 0; margin-left: 2em;'> <tr> <td style='width:2em;border: 0; box-shadow: none;padding-right:0.5em'> <figure class='image is-16x16'> <img src='{4}'/> </figure></td> <td valign='center' style='width:20em;border: 0; box-shadow: none;'><progress class='progress {2}' value='{1}' max='100'>{1}%</progress></td> <td style='border: 0; box-shadow: none;padding-left:0.5em'><figure class='image is-16x16'> <img src='{5}'/> </figure></td> </tr> </table> </div>", 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(@" </div> </body> </html>"); return sb.ToString(); } } }