]> git.somenet.org - pub/jan/aic18.git/blob - service-reporting/Utility/TemplateGenerator.cs
fix small pdf bugs for 1 term
[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() > 1) 
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.Append(@"<!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             if(terms.Length == 1) {
47                 sb.AppendFormat(@"For term: {0}" + terms[0].Name);
48             } else {
49                 sb.AppendFormat(@"For terms: {0}" + terms.Select(i => i.Name).Aggregate((i, j) => i + ',' + ' ' + j));   
50             }
51                 
52             sb.AppendFormat(@"
53                                     
54                                 </p>
55                             </div>
56                             </section>
57                             <div class='notification is-info' style='padding-left:5em; padding-right:5em'>
58                                 <table style='padding-left:2em'>
59                                     <td>
60                                         <tr>
61                                 <img src='{6}' style='height:2em; width: auto;padding-right:2em;'/>
62                                 <img src='{5}' style='height:2em; width: auto;padding-right:2em;'/>
63                                         </tr>
64                                     </td>
65                                 </table>
66                             </div>
67                             <div class='container' style='padding-left:5em; padding-right:5em'>
68                                 
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/>
74                                 <hr/>
75                                 <h1 class='subtitle is-3'>Detail results:</h1>
76
77                                 <div class='notification'>
78                                 <a>Legend:</a> <br/>
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/>
82                                 </div>
83
84                                 ", terms.Select(i => i.Name).Aggregate((i, j) => i + ',' + ' ' + j),
85                                 terms.Length,
86                                 terms.Select(i => i.Sentiment).Average(),
87                                 CalculateStdDev(terms.Select(i => (double)i.Sentiment)),
88                                 DateTime.Now,
89                                 Path.Combine(Directory.GetCurrentDirectory(), "assets", "indico.png"),
90                                 Path.Combine(Directory.GetCurrentDirectory(), "assets", "twitter.png"));
91  
92             foreach (Term term in terms)
93             {
94                 string color = "";
95                 if(term.Sentiment < 0.40)
96                     color = "is-danger";
97                 else if(term.Sentiment < 0.60)
98                     color = "is-warning";
99                 else 
100                     color = "is-success";
101
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;'>
106                             <tr>
107                                 <td style='width:2em;border: 0; box-shadow: none;padding-right:0.5em'>
108                                 <figure class='image is-16x16'>
109                                 <img src='{4}'/>
110                                 </figure></td>
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'>
113                                 <img src='{5}'/>
114                                 </figure></td>
115                             </tr>
116                             </table>                    
117                 
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"));
121             }
122  
123             sb.Append(@" </div>
124                             </body>
125                         </html>");
126             return sb.ToString();
127         }
128     }
129 }