1 #Region "Microsoft.VisualBasic::ec72f97d4ffa09c3293fb5c350e424cd, Microsoft.VisualBasic.Core\ComponentModel\Algorithm\Levenshtein\ResultVisualize.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Module ResultVisualize
35     
36     '         Function: __innerMatrix, __visualizeHTML, HTMLVisualize
37     
38     
39     ' /********************************************************************************/
40
41 #End Region
42
43 Imports System.Drawing
44 Imports System.Runtime.CompilerServices
45 Imports Microsoft.VisualBasic.Linq.Extensions
46 Imports Microsoft.VisualBasic.Net.Http
47 Imports Microsoft.VisualBasic.Text.Xml
48 Imports Microsoft.VisualBasic.Text.Xml.Models
49 Imports sys = System.Math
50
51 Namespace Text.Levenshtein
52
53     Public Module ResultVisualize
54
55         ''' <summary>
56         ''' Output HTML result for visualization
57         ''' </summary>
58         ''' <returns></returns>
59         <Extension>
60         Public Function HTMLVisualize(result As DistResult) As String
61             Try
62                 Return result.__visualizeHTML().FormatHTML
63             Catch ex As Exception
64                 Call App.LogException(ex)
65                 Return _
66                     <html>
67                         <head>
68                             <title><%= NameOf(HTTP_RFC.RFC_INTERNAL_SERVER_ERROR) %></title>
69                         </head>
70                         <body>
71                             <pre><%= ex.ToString %></pre>
72                         </body>
73                     </html>
74             End Try
75         End Function
76
77         <Extension>
78         Private Function __visualizeHTML(dist As DistResult) As String
79             Dim html As New XmlBuilder()
80             Dim edits$ = dist.DistEdits
81
82             html += <h3>Summary</h3>
83             html += "<table>"
84             html += <tr>
85                         <td>Reference: </td>
86                         <td><%= dist.__getReference.Length %>(chars)</td>
87                         <td><%= dist.Reference %></td>
88                     </tr>
89             html += <tr>
90                         <td>Hypotheses: </td>
91                         <td><%= dist.__getSubject.Length %>(chars)</td>
92                         <td><%= dist.Hypotheses %></td>
93                     </tr>
94             html += dist.__innerInsert
95             html += <tr>
96                         <td>Distance: </td>
97                         <td></td>
98                         <td><%= dist.Distance %></td>
99                     </tr>
100             html += <tr>
101                         <td>Similarity: </td>
102                         <td></td>
103                         <td><%= dist.MatchSimilarity * 100 %>%</td>
104                     </tr>
105             html += "</table>"
106             html += <hr/>
107             html += <h4>Levenshtein Edit Distance Table</h4>
108             html += <p>
109                         Levenshtein Edit: <br/>
110                         <strong><%= edits %></strong> have (<%= edits.Count("m"c) %>/<%= Len(edits) %>) matches.<br/>
111
112                         <p>
113                             <table>
114                                 <tr><td>d</td><td> -> Delete</td></tr>
115                                 <tr><td>i</td><td> -> Insert</td></tr>
116                                 <tr><td>m</td><td> -> Match</td></tr>
117                                 <tr><td>s</td><td> -> Substitute</td></tr>
118                             </table>
119                         </p>
120                     </p>
121
122             html += dist.__innerMatrix
123
124             Return (<html>
125                         <head>
126                             <title><%= dist.ToString %></title>
127                         </head>
128                         <body style="font-family:Ubuntu;">
129                             $content
130                         </body>
131                     </html>).ToString _
132                             .Replace("$content", html.ToString)
133         End Function
134
135         <Extension> Private Function __innerMatrix(matrix As DistResult) As String
136             Dim dict As Dictionary(Of IntegerInteger())
137
138             If matrix.DistTable Is Nothing Then
139                 Return ""
140             End If
141
142             If matrix.Path.IsNullOrEmpty Then
143                 dict = New Dictionary(Of IntegerInteger())
144             Else
145                 Dim g = From cell As Point
146                         In matrix.Path
147                         Select cell
148                         Group cell By cell.X Into Group
149
150                 dict = g _
151                     .ToDictionary(Function(row) row.X,
152                                   Function(row)
153                                       Return row _
154                                         .Group _
155                                         .Select(Function(cell) cell.Y) _
156                                         .ToArray
157                                   End Function)
158             End If
159
160             Dim Reference As String = matrix.__getReference()
161             Dim Hypotheses As String = matrix.__getSubject()
162             Dim MAT As New XmlBuilder
163             Dim hyps$() = Hypotheses _
164                 .Select(Function(c)
165                             Return <td>
166                                        <strong><%= c %></strong>
167                                    </td>
168                         End Function) _
169                 .Select(Function(s) s.ToString) _
170                 .ToArray
171
172             MAT += (<tr>
173                         <td></td>
174                         $content
175                     </tr>).ToString _
176                           .Replace("$content", hyps.JoinBy(""))
177
178             For i As Integer = 0 To Len(Reference) - 1
179                 Dim r As New XmlBuilder
180
181                 For j As Integer = 0 To Len(Hypotheses) - 1
182                     Dim c = sys.Round(matrix.DistTable(i)(j), 2)
183
184                     If dict.ContainsKey(i) AndAlso Array.IndexOf(dict(i), j) > -1 Then
185                         r += <td style="background-color:green;color:white">
186                                  <strong><%= c %></strong>
187                              </td>
188                     Else
189                         r += <td><%= c %></td>
190                     End If
191                 Next
192
193                 MAT += (<tr>
194                             <td>
195                                 <strong><%= Reference(i) %></strong>
196                             </td>
197                                 $content
198                            <td style="background-color:blue;color:white">
199                                 <strong><%= matrix.DistEdits(i) %></strong>
200                             </td>
201                         </tr>).ToString _
202                               .Replace("$content", r.ToString)
203             Next
204
205             Return (<table>
206                         $content
207                     </table>).ToString _
208                              .Replace("$content", MAT.ToString)
209         End Function
210     End Module
211 End Namespace