/
lgortho.jl
199 lines (159 loc) · 4.71 KB
/
lgortho.jl
1
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
"An orthographic system for standard orthography of printed literary Greek."
struct LiteraryGreekOrthography <: GreekOrthography
codepoints
tokencategories
end
OrthographyTrait(::Type{LiteraryGreekOrthography}) = IsOrthographicSystem()
"""Implement Orthography's tokenize function for LiteraryGreekOrthography.
$(SIGNATURES)
"""
function tokenize(s::AbstractString, o::LiteraryGreekOrthography)
tokenizeLiteraryGreek(s)
end
"""Implement Orthography's codepoints functions for LiteraryGreekOrthography.
$(SIGNATURES)
"""
function codepoints(ortho::LiteraryGreekOrthography)
ortho.codepoints
end
"""Implement Orthography's tokentypes functions for LiteraryGreekOrthography.
$(SIGNATURES)
"""
function tokentypes(ortho::LiteraryGreekOrthography)
ortho.tokencategories
end
"""
Instantiate a LiteraryGreekOrthography with correct code points and token types.
$(SIGNATURES)
"""
function literaryGreek()
cps = alphabetic() * " \t\n" * punctuation()
ttypes = [
Orthography.LexicalToken,
Orthography.PunctuationToken
]
LiteraryGreekOrthography(cps, ttypes)
end
"""Implement vowels function of GreekOrthography interface.
$(SIGNATURES)
"""
function vowels(ortho::LiteraryGreekOrthography)
LG_VOWELS
end
"""Implement consonants function of GreekOrthography interface.
$(SIGNATURES)
"""
function consonants(ortho::LiteraryGreekOrthography)
LG_CONSONANTS
end
"""Compose a string with all alphabetic characters.
$(SIGNATURES)
"""
function alphabetic()
ranges = [
'\u0027'; # single quote for elision
'α':'ω'; 'Α':'Ρ' ; 'Σ':'Ω' ;
'ϊ':'ώ'; 'ά'; 'έ'; 'ή' ; 'ί';'ΰ';'ΐ';
'\u1f00':'\u1f15'; '\u1f18':'\u1f1d' ;
'\u1f20':'\u1f45' ; '\u1f48':'\u1f4d' ;
'\u1f50':'\u1f57'; '\u1f59' ; '\u1f5b' ;
'\u1f5d' ; '\u1f5f':'\u1f7d' ;
'\u1f80':'\u1faf'; '\u1fb2':'\u1fb4' ;
'\u1fb6':'\u1fbc';
'\u1fc2':'\u1fc4';
'\u1fc6':'\u1fcc';
'\u1fd2':'\u1fd3'; '\u1fd6':'\u1fdb';
'\u1fe2':'\u1fe7';
'\u1fea':'\u1fec';
'\u1ff2':'\u1ff4'; '\u1ff6':'\u1ffc'
]
join(ranges,"")
end
"""Compose a string with all punctuation characters that can precede a token.
$(SIGNATURES)
"""
function prefixpunctuation()
"(\""
end
"""Compose a string with all punctuation characters that can follow a token.
$(SIGNATURES)
"""
function postfixpunctuation()
".,;:)\""
end
"""Compose a string with all punctuation characters.
$(SIGNATURES)
"""
function punctuation()
prefixpunctuation() * postfixpunctuation() |> unique |> String
end
"""
True if all characters in s are alphabetic.
$(SIGNATURES)
"""
function isAlphabetic(s::AbstractString)
chlist = split(s,"")
alphas = alphabetic()
tfs = []
for i in collect(eachindex(s))
push!(tfs, occursin(s[i], alphas))
end
tfs = map(c -> occursin(c, alphas), chlist)
nogood = false in tfs
!nogood
end
"""
True if all characters in s are puncutation.
$(SIGNATURES)
"""
function isPunctuation(s::AbstractString)
chlist = split(s,"")
puncts = punctuation()
tfs = map(c -> occursin(c, puncts), chlist)
nogood = false in tfs
!nogood
end
"""
Alphabetically sort a list of words in Unicode Greek.
$(SIGNATURES)
"""
function sortWords(words, ortho::LiteraryGreekOrthography)
strippedpairs = map(wd -> ( lowercase(Unicode.normalize(wd, stripmark=true)), wd),words)
sorted = sort(strippedpairs)
map(pr -> pr[2], sorted)
end
"""
Create correct OrthographicToken for a given string.
$(SIGNATURES)
"""
function tokenforstring(s::AbstractString)
normed = Unicode.normalize(s, :NFKC)
if isAlphabetic(normed)
OrthographicToken(normed, LexicalToken())
elseif isPunctuation(normed)
OrthographicToken(normed, PunctuationToken())
else
OrthographicToken(normed, Orthography.UnanalyzedToken())
end
end
"""
Split off any trailing punctuation and return an Array of leading string + trailing punctuation.
$(SIGNATURES)
"""
function splitPunctuation(s::AbstractString)
trailingpunct = Orthography.collecttail(s, PolytonicGreek.postfixpunctuation())
leadingpunct = Orthography.collecthead(s, PolytonicGreek.prefixpunctuation())
posttrimmed = Orthography.trimtail(s, PolytonicGreek.postfixpunctuation())
trimmed = Orthography.trimhead(posttrimmed, PolytonicGreek.prefixpunctuation())
filter(s -> ! isempty(s), [leadingpunct, trimmed, trailingpunct])
end
"""
Tokenize a string in standard literary Greek orthography.
$(SIGNATURES)
"""
function tokenizeLiteraryGreek(s::AbstractString)
wsdelimited = split(s)
depunctuated = map(s -> splitPunctuation(s), wsdelimited)
tknstrings = collect(Iterators.flatten(depunctuated))
tkns = map(t -> tokenforstring(t), tknstrings)
end