2019-08-20 19:21:31 +02:00
|
|
|
"""
|
2019-12-12 19:21:36 +01:00
|
|
|
find_best(s1::AbstractString, iter, dist::PreMetric; min_score = 0.0)
|
2019-08-20 19:21:31 +02:00
|
|
|
|
2019-12-12 19:21:36 +01:00
|
|
|
`find_best` returns the element of the iterator `iter` that has the highest similarity score with `s1` according to the distance `dist`. Return nothing if all elements have a similarity score below `min_score`.
|
2019-08-20 19:21:31 +02:00
|
|
|
The function is optimized for `Levenshtein` and `DamerauLevenshtein` distances (potentially modified by `Partial`, `TokenSort`, `TokenSet`, or `TokenMax`)
|
|
|
|
"""
|
2019-08-20 22:26:24 +02:00
|
|
|
function find_best(s1::AbstractString, iter_s2, dist::PreMetric; min_score = 0.0)
|
2019-12-12 19:21:36 +01:00
|
|
|
min_score >= 0 || throw("min_score should be positive")
|
|
|
|
best_s2s = AbstractString["" for _ in 1:Threads.nthreads()]
|
|
|
|
best_scores = [-1.0 for _ in 1:Threads.nthreads()]
|
|
|
|
min_score_atomic = Threads.Atomic{typeof(min_score)}(min_score)
|
|
|
|
Threads.@threads for s2 in iter_s2
|
|
|
|
score = compare(s1, s2, dist; min_score = min_score_atomic[])
|
|
|
|
min_score_atomic_old = Threads.atomic_max!(min_score_atomic, score)
|
|
|
|
if score >= min_score_atomic_old
|
|
|
|
best_s2s[Threads.threadid()] = s2
|
|
|
|
best_scores[Threads.threadid()] = score
|
2019-08-20 20:00:57 +02:00
|
|
|
score == 1.0 && return s2
|
2019-08-20 19:21:31 +02:00
|
|
|
end
|
|
|
|
end
|
2019-12-12 19:21:36 +01:00
|
|
|
i = argmax(best_scores)
|
|
|
|
if best_scores[i] < 0
|
|
|
|
return nothing
|
|
|
|
else
|
|
|
|
return best_s2s[i]
|
|
|
|
end
|
2019-08-20 19:21:31 +02:00
|
|
|
end
|
2019-08-20 21:38:14 +02:00
|
|
|
|
2019-08-20 19:21:31 +02:00
|
|
|
|
|
|
|
"""
|
|
|
|
find_all(s1::AbstractString, iter, dist::PreMetric; min_score = 0.8)
|
2019-12-12 19:21:36 +01:00
|
|
|
`find_all` returns the vector with all the elements of `iter` that have a similarity score higher or equal than `min_score` according to the distance `dist`.
|
2019-08-20 19:21:31 +02:00
|
|
|
The function is optimized for `Levenshtein` and `DamerauLevenshtein` distances (potentially modified by `Partial`, `TokenSort`, `TokenSet`, or `TokenMax`)
|
|
|
|
"""
|
2019-12-12 19:21:36 +01:00
|
|
|
function find_all(s1::AbstractString, iter_s2, dist::PreMetric; min_score = 0.8)
|
|
|
|
best_s2s = [eltype(iter_s2)[] for _ in 1:Threads.nthreads()]
|
|
|
|
Threads.@threads for s2 in iter_s2
|
|
|
|
score = compare(s1, s2, dist; min_score = min_score)
|
|
|
|
if score >= min_score
|
|
|
|
push!(best_s2s[Threads.threadid()], s2)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
vcat(best_s2s...)
|
2019-08-20 19:21:31 +02:00
|
|
|
end
|