/
ortho.jl
130 lines (98 loc) · 3.39 KB
/
ortho.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
"An abstract type for orthographic systems."
abstract type OrthographicSystem end
"An abstract type for token categories."
abstract type TokenCategory end
"A classified string of text."
struct OrthographicToken
text::AbstractString
tokencategory::TokenCategory
end
"""Find text value of an `OrthographicToken`.
$(SIGNATURES)
"""
function tokentext(ot::OrthographicToken)
ot.text
end
"""Find analytical category of an `OrthographicToken`.
$(SIGNATURES)
"""
function tokencategory(ot::OrthographicToken)
ot.tokencategory
end
"""The orthography trait."""
abstract type OrthographyTrait end
"""Value for the OrthographyTrait for orthographic systems."""
struct IsOrthographicSystem <: OrthographyTrait end
"""Value for the OrthographyTrait for non-orthographic systems."""
struct NotOrthographicSystem <: OrthographyTrait end
"""Define default value of OrthographyTrait as OrthographyTrait(::Type) = NotOrthographicSystem()
."""
OrthographyTrait(::Type) = NotOrthographicSystem()
"""Define OrthographyTrait value for all subtypes of OrthographicSystem."""
OrthographyTrait(::Type{<:OrthographicSystem}) = IsOrthographicSystem()
### FUNCTIONS REQUIRED FOR OrthographyTrait ###
#=
1. codepoints
2. tokentypes
3. tokenize
=#
## 1. First delegate based on type:
"""Delegate to specific functions based on
type's orthography trait value.
$(SIGNATURES)
"""
function codepoints(x::T) where {T}
codepoints(OrthographyTrait(T), x)
end
"""Delegate to specific functions based on
type's orthography trait value.
$(SIGNATURES)
"""
function tokentypes(x::T) where {T}
tokentypes(OrthographyTrait(T), x)
end
"""Delegate to specific functions based on
type's orthography trait value.
$(SIGNATURES)
"""
function tokenize(s::AbstractString, x::T) where {T}
tokenize(OrthographyTrait(T), s, x)
end
## 2. Override application to nonorthographic types.
"""It is an error to invoke the `codepoints` function on anything but an orthographic system.
$(SIGNATURES)
"""
function codepoints(::NotOrthographicSystem, x)
throw(DomainError(x, string("Objects of type ", typeof(x), " are not orthographic systems.")))
end
"""It is an error to invoke the `tokentypes` function on anything but an orthographic system.
$(SIGNATURES)
"""
function tokentypes(::NotOrthographicSystem, x)
throw(DomainError(x, string("Objects of type ", typeof(x), " are not orthographic systems.")))
end
"""It is an error to invoke the `tokenize` function on anything but an orthographic system.
$(SIGNATURES)
"""
function tokenize(::NotOrthographicSystem, s::AbstractString, x)
throw(DomainError(x, string("Objects of type ", typeof(x), " are not orthographic systems.")))
end
## 3. Impose required function on all citable texts:
"""Orthographic systems must implement codepoints.
$(SIGNATURES)
"""
function codepoints(::OrthographicSystem, ortho)
throw(DomainError(ortho, string("Please implement the codepoints function for type ", typeof(ortho))))
end
"""Orthographic systems must implement tokentypes.
$(SIGNATURES)
"""
function tokentypes(::OrthographicSystem, ortho, s::AbstractString)
throw(DomainError(ortho, string("Please implement the tokentypes function for type ", typeof(ortho))))
end
"""Orthographic systems must implement tokenize.
$(SIGNATURES)
"""
function tokenize(::OrthographicSystem, s::AbstractString, ortho)
throw(DomainError(ortho, string("Please implement the tokenize function for type ", typeof(ortho))))
end