gpt4 book ai didi

graph - 将文件层次结构作为边列表,然后将树结构绘制为图形

转载 作者:行者123 更新时间:2023-12-01 07:43:53 28 4
gpt4 key购买 nike

所以我想把目录结构画成树状图。我正在使用 NetworkLayout.jl .我卡在第 1 步,但我可以执行第 2 步和第 3 步。

  1. 构建文件和文件夹之间的链接列表,以生成类似这样(手工制作)的内容:

    links = Pair[
    "/" => "System",
    "/" => "Library",
    "/" => "Users",
    "System" => "sys1",
    "System" => "sys2",
    "System" => "sys3",
    "Library" => "lib1",
    "Library" => "lib2",
    "Library" => "lib3",
    "Users" => "u1",
    "Users" => "u2",
    "Users" => "u3",
    "Users" => "u4",
    "Users" => "u5",
    "u5" => "MyFolder"]
  2. 创建邻接表。如果所有链接都是唯一的(也是手工制作的),这就很容易了:

    adjlist = [
    [2, 3, 4],
    [5, 6, 7],
    [8, 9, 10],
    [],
    [11],
    [],
    [],
    [11, 12, 13, 14],
    [],
    [],
    [15],
    [],
    [],
    [],
    []]
  3. 进行布局。这一点很简单,因为 NetworkLayout.jl 会为您完成所有工作:

    using NetworkLayout
    NetworkLayout.Buchheim.layout(adjlist)
    ...>
    GeometryTypes.Point{2,Float64}[[0.0, -0.0], [-2.0, -2.0], [1.0, -2.0],
    [2.0, -2.0], [-3.0, -4.0], [-2.0, -4.0], [-1.0, -4.0], [0.0, -4.0],
    [1.0, -4.0], [2.0, -4.0], [-1.5, -6.0], [-0.5, -6.0], [0.5, -6.0],
    [1.5, -6.0], [-1.5, -8.0]]

graph plot

所以我的问题是,在第 1 步中,如何构建文件之间的初始链接列表。某些文件可能具有相同的名称?

最佳答案

  # get links with full path
function read_sub_dirs(path::AbstractString)
try # avoid access issue
a = readdir(path)
return a[isdir.((path*"/").*a)]
catch
return String[]
end
end
function deepreaddir_raw(working_dir_path::AbstractString,search_depth::Int=2)
links = Array{Pair,1}()
previous_dirs = [working_dir_path]
for i in 1:search_depth
if length(previous_dirs) > 0
next_dirs = String[]
for each_dir in previous_dirs
if each_dir[end] == '/'
sub_dirs = each_dir.*read_sub_dirs(each_dir)
else
sub_dirs = (each_dir*"/").*read_sub_dirs(each_dir)
end
append!(links,each_dir.=>sub_dirs)
append!(next_dirs,sub_dirs)
end
deleteat!(previous_dirs,1:length(previous_dirs))
append!(previous_dirs,next_dirs)
else
break
end
end
return links
end

# generate list of links
function deepreaddir(working_dir_path::AbstractString,search_depth::Int=2)
links_raw = deepreaddir_raw(working_dir_path,2)
links = Pair[]
for each_p in links_raw
a = split(each_p[1],"/")[end]; b = split(each_p[2],"/")[end];
a == "" ? a = working_dir_path : nothing
push!(links,a=>b)
end
links
end

links = deepreaddir(".")
# construct relationships
tree_dic = Dict{String,Array}()
[tree_dic[x] = String[] for x in map(x->x[1],links)]
for p in links
push!(tree_dic[p[1]],p[2])
end

# "System" ==> 1
str2id_dic = Dict{String,Int64}()
[str2id_dic[links[i][2]] = i for i in 1:length(links)]

# loop through col2 of `links`, guess it's the output you want?
str_res = map(xx->haskey(tree_dic,xx) ? tree_dic[xx] : String[], map(x->x[2],links))

# to ids
res = [map(k->str2id_dic[k],x) for x in str_res]
julia> res       = [map(k->str2id_dic[k],x) for x in str_res]
15-element Array{Array{T,1} where T,1}:
[4, 5, 6]
[7, 8, 9]
[10, 11, 12, 13, 14]
Any[]
Any[]
Any[]
Any[]
Any[]
Any[]
Any[]
Any[]
Any[]
Any[]
[15]
Any[]

关于graph - 将文件层次结构作为边列表,然后将树结构绘制为图形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58960955/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com