2 using PdfService.Models;
6 using System.Collections.Generic;
8 namespace PdfService.Utility
10 public static class TemplateGenerator
12 // calculates standard deviation for list
13 // from: https://stackoverflow.com/questions/3141692/standard-deviation-of-generic-list
14 private static float CalculateStdDev(IEnumerable<double> values)
17 if (values.Count() > 1)
20 double avg = values.Average();
21 //Perform the Sum of (value-avg)_2_2
22 double sum = values.Sum(d => Math.Pow(d - avg, 2));
24 ret = Math.Sqrt((sum) / (values.Count()-1));
29 public static string GetHTMLString(Term[] terms)
31 var sb = new StringBuilder();
32 sb.Append(@"<!doctype html>
35 <meta charset='utf-8'>
36 <meta name='viewport' content='initial-scale=1.0, user-scalable=no' />
39 <section class='hero is-info' style='padding: 3em 3em 0em 3em;'>
40 <div class='hero-body'>
41 <div class='container'>
43 Sentiment Analysis - Advanced Report
45 <p class='subtitle'>");
46 if(terms.Length == 1) {
47 sb.AppendFormat(@"For term: {0}", terms[0].Name);
49 sb.AppendFormat(@"For terms: {0}", terms.Select(i => i.Name).Aggregate((i, j) => i + ',' + ' ' + j));
57 <div class='notification is-info' style='padding-left:5em; padding-right:5em'>
58 <table style='padding-left:2em'>
61 <img src='{6}' style='height:2em; width: auto;padding-right:2em;'/>
62 <img src='{5}' style='height:2em; width: auto;padding-right:2em;'/>
67 <div class='container' style='padding-left:5em; padding-right:5em'>
69 <h1 class='subtitle is-3'>Overall statistics:</h1>
70 Amount of terms: <strong>{1}</strong><br/>
71 Mean sentiment value: <strong>{2:0.00}</strong><br/>
72 Standard deviation: <strong>{3:0.00}</strong><br/>
73 Date: <strong>{4}</strong><br/>
75 <h1 class='subtitle is-3'>Detail results:</h1>
77 <div class='notification'>
79 <span class='tag is-link is-danger' style='margin-right:0.5em'></span> Negative Sentiment Value: <strong>0-0.4</strong><br/>
80 <span class='tag is-link is-warning'style='margin-right:0.5em'></span> Neutral Sentiment Value: <strong>0.4-0.6</strong><br/>
81 <span class='tag is-link is-success'style='margin-right:0.5em'></span> Positive Sentiment Value: <strong>0.6-1.0</strong><br/>
84 ", terms.Select(i => i.Name).Aggregate((i, j) => i + ',' + ' ' + j),
86 terms.Select(i => i.Sentiment).Average(),
87 CalculateStdDev(terms.Select(i => (double)i.Sentiment)),
89 Path.Combine(Directory.GetCurrentDirectory(), "assets", "indico.png"),
90 Path.Combine(Directory.GetCurrentDirectory(), "assets", "twitter.png"));
92 foreach (Term term in terms)
95 if(term.Sentiment < 0.40)
97 else if(term.Sentiment < 0.60)
100 color = "is-success";
102 sb.AppendFormat(@"<div class='container' style='margin-top:3em;'>
103 <p class='subtitle is-4'>{0}</p>
104 <p style='margin-top:-1.0em; margin-left: 2em;'>Calculated sentiment: {3:0.00}</p> <br/>
105 <table class='table' style='width:100%; border: 0; margin-left: 2em;'>
107 <td style='width:2em;border: 0; box-shadow: none;padding-right:0.5em'>
108 <figure class='image is-16x16'>
111 <td valign='center' style='width:20em;border: 0; box-shadow: none;'><progress class='progress {2}' value='{1}' max='100'>{1}%</progress></td>
112 <td style='border: 0; box-shadow: none;padding-left:0.5em'><figure class='image is-16x16'>
118 </div>", term.Name, System.Math.Ceiling(term.Sentiment*100), color, term.Sentiment,
119 Path.Combine(Directory.GetCurrentDirectory(), "assets", "thumbs_down.png"),
120 Path.Combine(Directory.GetCurrentDirectory(), "assets", "thumbs_up.png"));
126 return sb.ToString();