-
Notifications
You must be signed in to change notification settings - Fork 96
/
tagging_schemes.jl
146 lines (124 loc) · 3.94 KB
/
tagging_schemes.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
# Ref:
# https://en.wikipedia.org/wiki/Inside%E2%80%93outside%E2%80%93beginning_(tagging)
# https://chameleonmetadata.com/Education/NLP-3/ref_nlp_encoding_schemes_list.php
abstract type tag_scheme end
struct BIO1 <: tag_scheme end # BIO
struct BIO2 <: tag_scheme end
struct BIOES <: tag_scheme end
const available_schemes = ["BIO1", "BIO2", "BIOES"]
"""
tag_scheme!(tags, current_scheme::String, new_scheme::String)
Convert `tags` from `current_scheme` to `new_scheme`.
List of tagging schemes currently supported-
* BIO1 (BIO)
* BIO2
* BIOES
# Example
```julia-repl
julia> tags = ["I-LOC", "O", "I-PER", "B-MISC", "I-MISC", "B-PER", "I-PER", "I-PER"]
julia> tag_scheme!(tags, "BIO1", "BIOES")
julia> tags
8-element Array{String,1}:
"S-LOC"
"O"
"S-PER"
"B-MISC"
"E-MISC"
"B-PER"
"I-PER"
"E-PER"
```
"""
function tag_scheme!(tags, current_scheme::String, new_scheme::String)
current_scheme = uppercase(current_scheme)
new_scheme = uppercase(new_scheme)
(length(tags) == 0 || current_scheme == new_scheme) && return
if new_scheme ∉ available_schemes || current_scheme ∉ available_schemes
error("Invalid tagging scheme")
end
current_scheme = eval(Symbol(current_scheme))()
new_scheme = eval(Symbol(new_scheme))()
tag_scheme!(tags, current_scheme, new_scheme)
end
function tag_scheme!(tags, current_scheme::BIO1, new_scheme::BIO2)
for i in eachindex(tags)
if tags[i] == 'O' || tags[i][1] == "O"
tags[i] = "O"
continue
end
(tags[i][1] == 'O' || tags[i][1] == 'B') && continue
if tags[i][1] == 'I'
if i == 1
tags[i] = 'B' * tags[i][2:end]
elseif tags[i - 1] == "O" || tags[i - 1][2:end] != tags[i][2:end]
tags[i] = 'B' * tags[i][2:end]
else
continue
end
else
error("Invalid tags")
end
end
end
function tag_scheme!(tags, current_scheme::BIO2, new_scheme::BIO1)
for i in eachindex(tags)
if tags[i] == 'O' || tags[i][1] == "O"
tags[i] = "O"
continue
end
(tags[i][1] == 'O' || tags[i][1] == 'I') && continue
if tags[i][1] == 'B'
if i == length(tags)
tags[i] = 'I' * tags[i][2:end]
elseif tags[i + 1] == "O" || tags[i + 1][2:end] != tags[i][2:end]
tags[i] = 'I' * tags[i][2:end]
else
continue
end
else
error("Invalid tags")
end
end
end
function tag_scheme!(tags, current_scheme::BIO2, new_scheme::BIOES)
for i in eachindex(tags)
if tags[i] == 'O' || tags[i][1] == 'O'
tags[i] = "O"
continue
end
if tags[i][1] == 'I' && (i == length(tags) ||
tags[i+1][2:end] != tags[i][2:end])
tags[i] = 'E' * tags[i][2:end]
elseif tags[i][1] == 'B' && (i == length(tags) ||
tags[i+1][2:end] != tags[i][2:end])
tags[i] = 'S' * tags[i][2:end]
else
(tags[i][1] == 'I' || tags[i][1] == 'B') && continue
error("Invalid tags")
end
end
end
function tag_scheme!(tags, current_scheme::BIOES, new_scheme::BIO2)
for i in eachindex(tags)
if tags[i] == 'O' || tags[i][1] == 'O'
tags[i] = "O"
continue
end
(tags[i][1] == 'B' || tags[i][1] == 'I') && continue
if tags[i][1] == 'E'
tags[i] = 'I' * tags[i][2:end]
elseif tags[i][1] == 'S'
tags[i] = 'B' * tags[i][2:end]
else
error("Invalid tags")
end
end
end
function tag_scheme!(tags, current_scheme::BIO1, new_scheme::BIOES)
tag_scheme!(tags, BIO1(), BIO2())
tag_scheme!(tags, BIO2(), BIOES())
end
function tag_scheme!(tags, current_scheme::BIOES, new_scheme::BIO1)
tag_scheme!(tags, BIOES(), BIO2())
tag_scheme!(tags, BIO2(), BIO1())
end