Skip to content

Instantly share code, notes, and snippets.

@NoFishLikeIan
Created August 19, 2021 13:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NoFishLikeIan/0ef2e64e3dc693c207cb3d430263e036 to your computer and use it in GitHub Desktop.
Save NoFishLikeIan/0ef2e64e3dc693c207cb3d430263e036 to your computer and use it in GitHub Desktop.
Find "longest" Italian word!
using DelimitedFiles, Base.Iterators
alphabet = 'A':'Z'
n = length(alphabet)
alphabetdict = Dict(alphabet .=> 1:n)
function parsepair(str::String)
clean = strip(str, [' ', '|'])
l, r = only.(split(clean, ""))
return (l, r)
end
function parsedistance(str::String)
clean = strip(str, [' ', 'm'])
return parse(Float64, clean)
end
function rawtograph(rawinput::Matrix{String})
edges = map(parsepair, rawinput[:, 1])
distances = map(parsedistance, rawinput[:, 2])
D = zeros(n, n)
for (edge, Δ) in zip(edges, distances)
src, dst = edge
l, r = alphabetdict[src], alphabetdict[dst]
D[l, r] = D[r, l] = Δ
end
return D
end
function findworddistance(word, D)
chars = only.(uppercase.(split(word, "")))
locations = (alphabetdict[char] for char in chars)
src, path = Iterators.peel(locations)
distance = 0.
for dst in path
distance += D[src, dst]
src = dst
end
return distance
end
begin # Find maximum italian word
distanceurl = "http://pi-ratebay.com/files/all_keyboard_distances.txt"
itwordsurl = "https://raw.githubusercontent.com/napolux/paroleitaliane/master/paroleitaliane/280000_parole_italiane.txt"
rawinput = readdlm(download(distanceurl), '=', String)
D = rawtograph(rawinput)
distances = Dict()
itwords = readdlm(download(itwordsurl), String)
for word in itwords
distance = findworddistance(word, D)
distances[word] = distance
end
distances = sort(collect(distances), by=x -> x[2])
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment