1 #Region "Microsoft.VisualBasic::b24df0482912f4098bb69445df22e30e, Microsoft.VisualBasic.Core\Text\GreekAlphabets.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 GreekAlphabets
35     
36     '         Properties: Alphabets, lower, upper
37     
38     '         Constructor: (+1 OverloadsSub New
39     '         Function: (+2 Overloads) AlphabetUnescape, (+2 Overloads) StripGreek, unescapeInternal
40     
41     
42     ' /********************************************************************************/
43
44 #End Region
45
46 Imports System.Runtime.CompilerServices
47 Imports System.Text
48 Imports Microsoft.VisualBasic.Language
49
50 Namespace Text
51
52     ''' <summary>
53     ''' Processing for the chemical compound name/genome species scientific name.
54     ''' </summary>
55     Public Module GreekAlphabets
56
57         Const Α$ = "alpha"
58         Const Β$ = "beta"
59         Const Γ$ = "gamma"
60         Const Δ$ = "delta"
61         Const Ε$ = "epsilon"
62         Const Ζ$ = "zeta"
63         Const Η$ = "eta"
64         Const Θ$ = "theta"
65         Const Ι$ = "iota"
66         Const Κ$ = "kappa"
67         Const Λ$ = "lambda"
68         Const Μ$ = "mu"
69         Const Ν$ = "nu"
70         Const Ξ$ = "xi"
71         Const Ο$ = "omicron"
72         Const Π$ = "pi"
73         Const Ρ$ = "rho"
74         Const Σ$ = "sigma"
75         Const Τ$ = "tau"
76         Const Υ$ = "upsilon"
77         Const Φ$ = "phi"
78         Const Χ$ = "chi"
79         Const Ψ$ = "psi"
80         Const Ω$ = "omega"
81
82         Public ReadOnly Property Alphabets As Dictionary(Of StringString)
83         Public ReadOnly Property upper As Dictionary(Of StringString)
84         Public ReadOnly Property lower As Dictionary(Of StringString)
85
86         Sub New()
87             _Alphabets = New Dictionary(Of StringString) From {
88                {"α", GreekAlphabets.Α}, {"Α", GreekAlphabets.Α},
89                {"β", GreekAlphabets.Β}, {"Β", GreekAlphabets.Β},
90                {"γ", GreekAlphabets.Γ}, {"Γ", GreekAlphabets.Γ},
91                {"δ", GreekAlphabets.Δ}, {"Δ", GreekAlphabets.Δ},
92                {"ε", GreekAlphabets.Ε}, {"Ε", GreekAlphabets.Ε},
93                {"ζ", GreekAlphabets.Ζ}, {"Ζ", GreekAlphabets.Ζ},
94                {"η", GreekAlphabets.Η}, {"Η", GreekAlphabets.Η},
95                {"θ", GreekAlphabets.Θ}, {"Θ", GreekAlphabets.Θ},
96                {"ι", GreekAlphabets.Ι}, {"Ι", GreekAlphabets.Ι},
97                {"κ", GreekAlphabets.Κ}, {"Κ", GreekAlphabets.Κ},
98                {"λ", GreekAlphabets.Λ}, {"Λ", GreekAlphabets.Λ},
99                {"μ", GreekAlphabets.Μ}, {"Μ", GreekAlphabets.Μ},
100                {"ν", GreekAlphabets.Ν}, {"Ν", GreekAlphabets.Ν},
101                {"ξ", GreekAlphabets.Ξ}, {"Ξ", GreekAlphabets.Ξ},
102                {"ο", GreekAlphabets.Ο}, {"Ο", GreekAlphabets.Ο},
103                {"π", GreekAlphabets.Π}, {"Π", GreekAlphabets.Π},
104                {"ρ", GreekAlphabets.Ρ}, {"Ρ", GreekAlphabets.Ρ},
105                {"σ", GreekAlphabets.Σ}, {"Σ", GreekAlphabets.Σ},
106                {"τ", GreekAlphabets.Τ}, {"Τ", GreekAlphabets.Τ},
107                {"υ", GreekAlphabets.Υ}, {"Υ", GreekAlphabets.Υ},
108                {"φ", GreekAlphabets.Φ}, {"Φ", GreekAlphabets.Φ},
109                {"χ", GreekAlphabets.Χ}, {"Χ", GreekAlphabets.Χ},
110                {"ψ", GreekAlphabets.Ψ}, {"Ψ", GreekAlphabets.Ψ},
111                {"ω", GreekAlphabets.Ω}, {"Ω", GreekAlphabets.Ω}
112             }
113
114             _upper = Alphabets.Subset({
115                 "Α""Β""Γ""Δ""Ε""Ζ",
116                 "Η""Θ""Ι""Κ""Λ""Μ",
117                 "Ν""Ξ""Ο""Π""Ρ""Σ",
118                 "Τ""Υ""Φ""Χ""Ψ""Ω"
119             })
120             _lower = Alphabets.Subset(Alphabets.Keys.AsSet - upper.Keys)
121         End Sub
122
123         ''' <summary>
124         ''' 将字符串文本之中的希腊字母替换为英文单词
125         ''' </summary>
126         ''' <param name="s"></param>
127         ''' <returns></returns>
128         <Extension> Public Function StripGreek(ByRef s As StringBuilder) As StringBuilder
129             For Each key In Alphabets.Keys
130                 Call s.Replace(key, Alphabets(key))
131             Next
132             Return s
133         End Function
134
135         ''' <summary>
136         ''' 将字符串文本之中的希腊字母替换为英文单词
137         ''' </summary>
138         ''' <param name="s$"></param>
139         ''' <returns></returns>
140         <Extension> Public Function StripGreek(s$) As String
141             Return New StringBuilder(s).StripGreek.ToString
142         End Function
143
144         Const contactSymbols = "[\-\(\)&;\s ,\.:\|\[\]\+\*]"
145         Const escapePattern$ = contactSymbols & "[a-z]{2,10}" & contactSymbols
146
147         ''' <summary>
148         ''' 
149         ''' </summary>
150         ''' <param name="s$"></param>
151         ''' <param name="removesContacts"></param>
152         ''' <param name="upperCase"></param>
153         ''' <returns></returns>
154         ''' 
155         <MethodImpl(MethodImplOptions.AggressiveInlining)>
156         <Extension>
157         Public Function AlphabetUnescape(s$, Optional removesContacts As Boolean = FalseOptional upperCase As Boolean = FalseAs String
158             Return s.unescapeInternal(escapePattern, upperCase, removesContacts)
159         End Function
160
161         <Extension>
162         Private Function unescapeInternal(s$, escapePattern$, upperCase As Boolean, removesContacts As BooleanAs String
163             ' 如果直接匹配替换的话,可能会将单词之中的一部分给错误的替换掉,
164             ' 所以在这里假设希腊字母是在连接符周围的
165             Dim matches = s.Matches(escapePattern, RegexICSng)
166             Dim sb As New StringBuilder(s)
167             Dim alphabets = (upper Or lower.When(Not upperCase)).ReverseMaps
168
169             For Each match As String In matches
170                 Dim term$ = Mid(match, 2, Length:=match.Length - 2)
171                 Dim greek$ = alphabets.TryGetValue(term.ToLower)
172
173                 If Not greek Is Nothing Then
174                     If Not removesContacts Then
175                         match = term
176                     End If
177
178                     Call sb.Replace(match, greek)
179                 End If
180             Next
181
182             Return sb.ToString
183         End Function
184
185         <MethodImpl(MethodImplOptions.AggressiveInlining)>
186         <Extension>
187         Public Function AlphabetUnescape(s$, contacts As (left As Char, right As Char), Optional upperCase As Boolean = FalseAs String
188             Return s.unescapeInternal($"[{contacts.left}][a-z]{{2,10}}[{contacts.right}]", upperCase, True)
189         End Function
190     End Module
191 End Namespace