]> git.somenet.org - pub/jan/aic18.git/blob - service-reporting/Utility/TemplateGenerator.cs
issue #49 for service analysis: indico api key is now in a .env file. readmes, docker...
[pub/jan/aic18.git] / service-reporting / Utility / TemplateGenerator.cs
1 using System.Text;
2 using PdfService.Models;
3 using System.IO;
4 using System;
5 using System.Linq;
6 using System.Collections.Generic;
7
8 namespace PdfService.Utility
9 {
10     public static class TemplateGenerator
11     {
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)
15         {   
16             double ret = 0;
17             if (values.Count() > 0) 
18             {      
19                 //Compute the Average      
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));
23                 //Put it all together      
24                 ret = Math.Sqrt((sum) / (values.Count()-1));   
25             }   
26             return (float)ret;
27         }
28
29         public static string GetHTMLString(Term[] terms)
30         {
31             var sb = new StringBuilder();
32             sb.AppendFormat(@"<!doctype html>
33                         <html>
34                             <head>
35                                 <meta charset='utf-8'>
36                                 <meta name='viewport' content='initial-scale=1.0, user-scalable=no' />
37                             </head>
38                             <body>
39                             <section class='hero is-info' style='padding: 3em 3em 0em 3em;'>
40                             <div class='hero-body'>
41                                 <div class='container'>
42                                 <h1 class='title'>
43                                     Sentiment Analysis - Advanced Report
44                                 </h1>
45                                 <p class='subtitle'>
46                                     For terms: {0}
47                                 </p>
48                             </div>
49                             </section>
50                             <div class='notification is-info' style='padding-left:5em; padding-right:5em'>
51                                 <table style='padding-left:2em'>
52                                     <td>
53                                         <tr>
54                                 <img src='{6}' style='height:2em; width: auto;padding-right:2em;'/>
55                                 <img src='{5}' style='height:2em; width: auto;padding-right:2em;'/>
56                                         </tr>
57                                     </td>
58                                 </table>
59                             </div>
60                             <div class='container' style='padding-left:5em; padding-right:5em'>
61                                 
62                                 <h1 class='subtitle is-3'>Overall statistics</h1>
63                                 Amount of terms: <strong>{1}</strong><br/>
64                                 Mean sentiment value: <strong>{2:0.00}</strong><br/>
65                                 Standard deviation: <strong>{3:0.00}</strong><br/>
66                                 Date: <strong>{4}</strong><br/>
67                                 <hr/>
68                                 <h1 class='subtitle is-3'>Detail results:</h1>
69
70                                 <div class='notification'>
71                                 <a>Legend:</a> <br/>
72                                 <span class='tag is-link is-danger' style='margin-right:0.5em'></span> Negative Sentiment Value: <strong>0-0.4</strong><br/>
73                                 <span class='tag is-link is-warning'style='margin-right:0.5em'></span> Neutral Sentiment Value: <strong>0.4-0.6</strong><br/>
74                                 <span class='tag is-link is-success'style='margin-right:0.5em'></span> Positive Sentiment Value: <strong>0.6-1.0</strong><br/>
75                                 </div>
76
77                                 ", terms.Select(i => i.Name).Aggregate((i, j) => i + ',' + ' ' + j),
78                                 terms.Length,
79                                 terms.Select(i => i.Sentiment).Average(),
80                                 CalculateStdDev(terms.Select(i => (double)i.Sentiment)),
81                                 DateTime.Now,
82                                 Path.Combine(Directory.GetCurrentDirectory(), "assets", "indico.png"),
83                                 Path.Combine(Directory.GetCurrentDirectory(), "assets", "twitter.png"));
84  
85             foreach (Term term in terms)
86             {
87                 string color = "";
88                 if(term.Sentiment < 0.40)
89                     color = "is-danger";
90                 else if(term.Sentiment < 0.60)
91                     color = "is-warning";
92                 else 
93                     color = "is-success";
94
95                 sb.AppendFormat(@"<div class='container' style='margin-top:3em;'>  
96                             <p class='subtitle is-4'>{0}</p>
97                             <p style='margin-top:-1.0em; margin-left: 2em;'>Calculated sentiment: {3:0.00}</p> <br/>
98                         <table class='table' style='width:100%; border: 0; margin-left: 2em;'>
99                             <tr>
100                                 <td style='width:2em;border: 0; box-shadow: none;padding-right:0.5em'>
101                                 <figure class='image is-16x16'>
102                                 <img src='{4}'/>
103                                 </figure></td>
104                                 <td valign='center' style='width:20em;border: 0; box-shadow: none;'><progress class='progress {2}' value='{1}' max='100'>{1}%</progress></td> 
105                                 <td style='border: 0; box-shadow: none;padding-left:0.5em'><figure class='image is-16x16'>
106                                 <img src='{5}'/>
107                                 </figure></td>
108                             </tr>
109                             </table>                    
110                 
111                 </div>", term.Name, System.Math.Ceiling(term.Sentiment*100), color, term.Sentiment,
112                 Path.Combine(Directory.GetCurrentDirectory(), "assets", "thumbs_down.png"),
113                 Path.Combine(Directory.GetCurrentDirectory(), "assets", "thumbs_up.png"));
114             }
115  
116             sb.Append(@" </div>
117                             </body>
118                         </html>");
119             return sb.ToString();
120         }
121     }
122 }