1 |
#Region "Microsoft.VisualBasic::b24df0482912f4098bb69445df22e30e, Microsoft.VisualBasic.Core\Text\GreekAlphabets.vb"
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
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 |
|
53 |
|
54 |
|
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 String, String)
|
83 |
Public ReadOnly Property upper As Dictionary(Of String, String)
|
84 |
Public ReadOnly Property lower As Dictionary(Of String, String)
|
85 |
|
86 |
Sub New()
|
87 |
_Alphabets = New Dictionary(Of String, String) 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 |
|
124 |
|
125 |
|
126 |
|
127 |
|
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 |
|
136 |
|
137 |
|
138 |
|
139 |
|
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 |
|
148 |
|
149 |
|
150 |
|
151 |
<param name="removesContacts"></param>
|
152 |
<param name="upperCase"></param>
|
153 |
|
154 |
|
155 |
<MethodImpl(MethodImplOptions.AggressiveInlining)>
|
156 |
<Extension>
|
157 |
Public Function AlphabetUnescape(s$, Optional removesContacts As Boolean = False, Optional upperCase As Boolean = False) As 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 Boolean) As 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 = False) As String
|
188 |
Return s.unescapeInternal($"[{contacts.left}][a-z]{{2,10}}[{contacts.right}]", upperCase, True)
|
189 |
End Function
|
190 |
End Module
|
191 |
End Namespace
|