-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.jl
248 lines (198 loc) · 5.66 KB
/
utils.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#=
Copyright (C) 2023 Yiding Song
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
=#
using JLD2, ProgressBars
# Utility functions
function normalise(title::String)
return replace(uppercasefirst(strip(title)), " " => "_")
end
checkfile(f) = ispath(f) && rm(f)
# Page management
mutable struct Pageman
source::AbstractString
id2title::Vector{String}
title2id::Dict{String,Int32}
redirs::Vector{Int32}
numpages::Int32
totalpages::Int32
function Pageman(fpath::AbstractString)
id2tt = readlines(fpath)[2:end] # First line is "page_title"
npg = length(id2tt)
tt2id = Dict(id2tt .=> [i for i = 1:npg])
rds = [i for i = 1:npg]
return new(fpath, id2tt, tt2id, rds, npg, npg)
end
end
function isRedir(
pm::Pageman,
id::Integer
)
return pm.redirs[id] != id
end
function notRedir(
pm::Pageman,
id::Integer
)
return pm.redirs[id] == id
end
function traceRedir!(
pm::Pageman,
id::Integer
)
intermediates = []
red = pm.redirs[id]
next = pm.redirs[red]
while red != next
push!(intermediates, red)
red = pm.redirs[next]
next = pm.redirs[red]
end
pm.redirs[id] = red
for i in intermediates
pm.redirs[i] = red
end
return red
end
function tie(
pm::Pageman,
srcId::Integer,
trgId::Integer
)
if pm.redirs[srcId] != trgId
if pm.redirs[srcId] == srcId
pm.numpages -= 1
end
pm.redirs[srcId] = trgId
end
end
function savepm(
fpath::AbstractString,
pm::Pageman
)
save(fpath, Dict("source" => pm.source, "numpages" => pm.numpages, "redirs" => pm.redirs))
end
function loadpm(fpath::AbstractString, titlesPath::AbstractString)
bundle = load(fpath)
pm = Pageman(titlesPath)
pm.numpages = bundle["numpages"]
pm.redirs = bundle["redirs"]
return pm
end
# Link serialiation
function saveLinks(
fpath::AbstractString,
links::Vector{Vector{Pair{Int32, Int32}}},
pm::Pageman
)
checkfile(fpath)
open(fpath, "a") do f
for i::Int32 in ProgressBar(eachindex(links))
write(f, i)
written = Int32[]
for (t, w) in links[i]
r = traceRedir!(pm, t)
if !(r in written)
write(f, r, w, Int32(0))
push!(written, r)
end
end
write(f, Int32(0))
end
end
end
function loadLinks(fpath::AbstractString, pm::Pageman; backwards::Bool=false)
links = Vector{Pair{Int32, Int32}}[[] for _ = 1:pm.totalpages]
open(fpath, "r") do f
counter::Int32 = 1
newline = true
target = false
weight = false
delim = false
cache::Int32 = 0
for c in ProgressBar(readeach(f, Int32))
if newline
@assert c == counter "File is corrupted (counter error): counter is $(counter), file is $(c)"
target = true
newline = false
elseif target
if c == 0
counter += 1
newline = true
else
@assert c > 0 "File is corrupted (target < 0)"
cache = c
weight = true
end
target = false
elseif weight
@assert c >= 0 "File is corrupted (weight <= 0)"
trg = traceRedir!(pm, cache)
if backwards
(counter => c) ∉ links[trg] && push!(links[trg], counter => c)
else
(trg => c) ∉ links[counter] && push!(links[counter], trg => c)
end
delim = true
weight = false
elseif delim
@assert c == 0 "File is corrupted (delim != 0)"
target = true
delim = false
end
end
end
return links
end
function saveLinksQuick(
fpath::AbstractString,
links::Vector{Vector{Pair{Int32, Int32}}}
)
checkfile(fpath)
open(fpath, "a") do f
for i::Int32 in ProgressBar(eachindex(links))
for (t, w) in links[i]
write(f, t)
end
write(f, Int32(0))
end
end
end
function saveLinksQuick(
fpath::AbstractString,
links::Vector{Vector{Int32}}
)
checkfile(fpath)
open(fpath, "a") do f
for i::Int32 in ProgressBar(eachindex(links))
for t in links[i]
write(f, t)
end
write(f, Int32(0))
end
end
end
function loadLinksQuick(fpath::AbstractString)
links = Vector{Int32}[[]]
open(fpath, "r") do f
counter::Int32 = 1
for c in ProgressBar(readeach(f, Int32))
if c == 0
counter += 1
push!(links, [])
else
push!(links[counter], c)
end
end
end
return links
end