<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Julia | Yuya Kaneta</title><link>https://www.yuyakaneta.page/tags/julia/</link><atom:link href="https://www.yuyakaneta.page/tags/julia/index.xml" rel="self" type="application/rss+xml"/><description>Julia</description><generator>Hugo Blox Builder (https://hugoblox.com)</generator><language>en</language><copyright>© 2021-2024 Yuya Kaneta</copyright><lastBuildDate>Thu, 27 May 2021 23:27:13 +0900</lastBuildDate><image><url>https://www.yuyakaneta.page/media/icon_hu_c4fd2c5d31e64759.png</url><title>Julia</title><link>https://www.yuyakaneta.page/tags/julia/</link></image><item><title>箇条書きをツリー形式に再構成するスクリプトをJuliaでかいてみた</title><link>https://www.yuyakaneta.page/post/tree_convert/</link><pubDate>Thu, 27 May 2021 23:27:13 +0900</pubDate><guid>https://www.yuyakaneta.page/post/tree_convert/</guid><description>&lt;h1 id="概要"&gt;概要&lt;/h1&gt;
&lt;p&gt;treeコマンドなどで表示されるツリー形式の表示方法は階層構造を視覚化でき、わかりやすいので案件等でもディレクトリ構成を説明する際に良く使っています。&lt;/p&gt;
&lt;p&gt;一方、この treeコマンドは当然ながら既にある階層的なディレクトリしか表示することができません。例えばコンフルエンス等の階層構造を説明する際ツリー形式に視覚的に綺麗に表示できれば良いとは思っても、treeコマンドが使えるはずもなくもどかしく思います。&lt;/p&gt;
&lt;p&gt;そこで「箇条書き」→「ツリー形式」に変換するスクリプトをJuliaで作りました。&lt;/p&gt;
&lt;h2 id="なぜjulia"&gt;なぜJulia？&lt;/h2&gt;
&lt;p&gt;全くもって特に理由はないです。&lt;/p&gt;
&lt;h1 id="問題設定"&gt;問題設定&lt;/h1&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;- test
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - aaa
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - 1
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - 2
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - x
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - y
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - 3
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - bbb
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - 1
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - 2
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - 3
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - x
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - y
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - z
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; - x
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;↑上記箇条書きを、↓下記ツリー形式に変換するスクリプト&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-fallback" data-lang="fallback"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;test
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;├── aaa
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;│   ├── 1
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;│   ├── 2
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;│   │   ├── x
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;│   │   └── y
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;│   └── 3
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;└── bbb
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ├── 1
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ├── 2
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; └── 3
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; ├── x
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; │   └── y
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; │   └── z
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; └── x
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h1 id="スクリプトについて"&gt;スクリプトについて&lt;/h1&gt;
&lt;p&gt;上記問題をJuliaで解決するスクリプトを作成しました。&lt;/p&gt;
&lt;h2 id="ターゲットとなる文章"&gt;ターゲットとなる文章&lt;/h2&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-julia" data-lang="julia"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cm"&gt;#=
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cm"&gt;今回のターゲットとなる箇条書き。
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cm"&gt;もちろん外部ファイルを読み込む形式にしても良い。
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cm"&gt;=#&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;tree_text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s"&gt;&amp;#34;- test
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s"&gt; - aaa
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s"&gt; - 1
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s"&gt; - 2
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s"&gt; - x
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s"&gt; - y
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s"&gt; - 3
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s"&gt; - bbb
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s"&gt; - 1
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s"&gt; - 2
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s"&gt; - 3
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s"&gt; - x
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s"&gt; - y
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s"&gt; - z
&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s"&gt; - x&amp;#34;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;一旦問題となる箇条書きをベタ書きしました。もちろん外部ファイルに列挙し、Juliaから read関数を用いて読み込む形式にしても問題ありません。 今回のコードでは- xxxxxで構成される箇条書きと、正しく配置されたインデントと改行コード \nがあれば対応できます。&lt;/p&gt;
&lt;h2 id="juliaスクリプトについて"&gt;Juliaスクリプトについて&lt;/h2&gt;
&lt;p&gt;いくつか箇条書きを解析し、ツリー構造を構築するための関数を作成しました。&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&amp;quot;&amp;quot;&amp;quot;
convert_text_to_vec(text)
textを改行で分割し、箇条書きの記号を消す処理
# Examples
```julia-repl
julia&amp;gt; tree_text = &amp;quot;- test
- aaa
- 1
- 2
- x
- y
- 3
- bbb
- 1
- 2
- 3
- x
- y
- z
- x&amp;quot;;
julia&amp;gt; convert_text_to_vec(tree_text)
15-element Vector{SubString{String}}:
&amp;quot;test&amp;quot;
&amp;quot; aaa&amp;quot;
&amp;quot; 1&amp;quot;
&amp;quot; 2&amp;quot;
&amp;quot; x&amp;quot;
&amp;quot; y&amp;quot;
&amp;quot; 3&amp;quot;
&amp;quot; bbb&amp;quot;
&amp;quot; 1&amp;quot;
&amp;quot; 2&amp;quot;
&amp;quot; 3&amp;quot;
&amp;quot; x&amp;quot;
&amp;quot; y&amp;quot;
&amp;quot; z&amp;quot;
&amp;quot; x&amp;quot;
```
&amp;quot;&amp;quot;&amp;quot;
convert_text_to_vec(text) = split(replace(text, &amp;quot;- &amp;quot; =&amp;gt; &amp;quot;&amp;quot;), &amp;quot;\n&amp;quot;)
&amp;quot;&amp;quot;&amp;quot;
add_information_indent(data)
`convert_text_to_vec` を用いてVector化したテキストのインデント情報を集計する
# Examples
```julia-repl
julis&amp;gt; add_information_indent(convert_text_to_vec(tree_text))
15-element Vector{Dict}:
Dict{Symbol, Any}(:indent =&amp;gt; 0, :text =&amp;gt; &amp;quot;test&amp;quot;)
Dict{Symbol, Any}(:indent =&amp;gt; 1, :text =&amp;gt; &amp;quot;aaa&amp;quot;)
Dict{Symbol, Any}(:indent =&amp;gt; 2, :text =&amp;gt; &amp;quot;1&amp;quot;)
Dict{Symbol, Any}(:indent =&amp;gt; 2, :text =&amp;gt; &amp;quot;2&amp;quot;)
Dict{Symbol, Any}(:indent =&amp;gt; 3, :text =&amp;gt; &amp;quot;x&amp;quot;)
Dict{Symbol, Any}(:indent =&amp;gt; 3, :text =&amp;gt; &amp;quot;y&amp;quot;)
Dict{Symbol, Any}(:indent =&amp;gt; 2, :text =&amp;gt; &amp;quot;3&amp;quot;)
Dict{Symbol, Any}(:indent =&amp;gt; 1, :text =&amp;gt; &amp;quot;bbb&amp;quot;)
Dict{Symbol, Any}(:indent =&amp;gt; 2, :text =&amp;gt; &amp;quot;1&amp;quot;)
Dict{Symbol, Any}(:indent =&amp;gt; 2, :text =&amp;gt; &amp;quot;2&amp;quot;)
Dict{Symbol, Any}(:indent =&amp;gt; 2, :text =&amp;gt; &amp;quot;3&amp;quot;)
Dict{Symbol, Any}(:indent =&amp;gt; 3, :text =&amp;gt; &amp;quot;x&amp;quot;)
Dict{Symbol, Any}(:indent =&amp;gt; 4, :text =&amp;gt; &amp;quot;y&amp;quot;)
Dict{Symbol, Any}(:indent =&amp;gt; 5, :text =&amp;gt; &amp;quot;z&amp;quot;)
Dict{Symbol, Any}(:indent =&amp;gt; 3, :text =&amp;gt; &amp;quot;x&amp;quot;)
```
&amp;quot;&amp;quot;&amp;quot;
function add_information_indent(data)
result = Vector{Dict}()
length_space = []
for v in data
temp = lstrip(v)
space = replace(v, temp =&amp;gt; &amp;quot;&amp;quot;)
push!(length_space, length(space))
end
min_space = minimum([i for i in length_space if i &amp;gt; 0])
for (i, j) in zip(lstrip.(data), length_space)
push!(result, Dict(:indent =&amp;gt; Int(j/min_space),
:text =&amp;gt; String(i)))
end
return result
end
&amp;quot;&amp;quot;&amp;quot;
add_index(data)
`convert_text_to_vec` と `add_information_indent` を用いてVector化したテキストのインデックス情報を集計する
# Examples
```julia-repl
julia&amp;gt; add_index(add_information_indent(convert_text_to_vec(tree_text)))
15-element Vector{Dict}:
Dict{Symbol, Any}(:index =&amp;gt; 1, :indent =&amp;gt; 0, :text =&amp;gt; &amp;quot;test&amp;quot;)
Dict{Symbol, Any}(:index =&amp;gt; 2, :indent =&amp;gt; 1, :text =&amp;gt; &amp;quot;aaa&amp;quot;)
Dict{Symbol, Any}(:index =&amp;gt; 3, :indent =&amp;gt; 2, :text =&amp;gt; &amp;quot;1&amp;quot;)
Dict{Symbol, Any}(:index =&amp;gt; 4, :indent =&amp;gt; 2, :text =&amp;gt; &amp;quot;2&amp;quot;)
Dict{Symbol, Any}(:index =&amp;gt; 5, :indent =&amp;gt; 3, :text =&amp;gt; &amp;quot;x&amp;quot;)
Dict{Symbol, Any}(:index =&amp;gt; 6, :indent =&amp;gt; 3, :text =&amp;gt; &amp;quot;y&amp;quot;)
Dict{Symbol, Any}(:index =&amp;gt; 7, :indent =&amp;gt; 2, :text =&amp;gt; &amp;quot;3&amp;quot;)
Dict{Symbol, Any}(:index =&amp;gt; 8, :indent =&amp;gt; 1, :text =&amp;gt; &amp;quot;bbb&amp;quot;)
Dict{Symbol, Any}(:index =&amp;gt; 9, :indent =&amp;gt; 2, :text =&amp;gt; &amp;quot;1&amp;quot;)
Dict{Symbol, Any}(:index =&amp;gt; 10, :indent =&amp;gt; 2, :text =&amp;gt; &amp;quot;2&amp;quot;)
Dict{Symbol, Any}(:index =&amp;gt; 11, :indent =&amp;gt; 2, :text =&amp;gt; &amp;quot;3&amp;quot;)
Dict{Symbol, Any}(:index =&amp;gt; 12, :indent =&amp;gt; 3, :text =&amp;gt; &amp;quot;x&amp;quot;)
Dict{Symbol, Any}(:index =&amp;gt; 13, :indent =&amp;gt; 4, :text =&amp;gt; &amp;quot;y&amp;quot;)
Dict{Symbol, Any}(:index =&amp;gt; 14, :indent =&amp;gt; 5, :text =&amp;gt; &amp;quot;z&amp;quot;)
Dict{Symbol, Any}(:index =&amp;gt; 15, :indent =&amp;gt; 3, :text =&amp;gt; &amp;quot;x&amp;quot;)
```
&amp;quot;&amp;quot;&amp;quot;
function add_index(data)
for (i, d) in enumerate(data)
d[:index] = i
end
result = data
return result
end
&amp;quot;&amp;quot;&amp;quot;
add_lastflag(data)
`convert_text_to_vec` と `add_information_indent` を用いてVector化したテキストのブロック最後のフラグ情報を集計する
# Examples
```julia-repl
julia&amp;gt; add_lastflag(add_information_indent(convert_text_to_vec(tree_text)))
15-element Vector{Dict}:
Dict{Symbol, Any}(:lastflag =&amp;gt; 1, :indent =&amp;gt; 0, :text =&amp;gt; &amp;quot;test&amp;quot;)
Dict{Symbol, Any}(:lastflag =&amp;gt; 0, :indent =&amp;gt; 1, :text =&amp;gt; &amp;quot;aaa&amp;quot;)
Dict{Symbol, Any}(:lastflag =&amp;gt; 0, :indent =&amp;gt; 2, :text =&amp;gt; &amp;quot;1&amp;quot;)
Dict{Symbol, Any}(:lastflag =&amp;gt; 0, :indent =&amp;gt; 2, :text =&amp;gt; &amp;quot;2&amp;quot;)
Dict{Symbol, Any}(:lastflag =&amp;gt; 0, :indent =&amp;gt; 3, :text =&amp;gt; &amp;quot;x&amp;quot;)
Dict{Symbol, Any}(:lastflag =&amp;gt; 1, :indent =&amp;gt; 3, :text =&amp;gt; &amp;quot;y&amp;quot;)
Dict{Symbol, Any}(:lastflag =&amp;gt; 1, :indent =&amp;gt; 2, :text =&amp;gt; &amp;quot;3&amp;quot;)
Dict{Symbol, Any}(:lastflag =&amp;gt; 1, :indent =&amp;gt; 1, :text =&amp;gt; &amp;quot;bbb&amp;quot;)
Dict{Symbol, Any}(:lastflag =&amp;gt; 0, :indent =&amp;gt; 2, :text =&amp;gt; &amp;quot;1&amp;quot;)
Dict{Symbol, Any}(:lastflag =&amp;gt; 0, :indent =&amp;gt; 2, :text =&amp;gt; &amp;quot;2&amp;quot;)
Dict{Symbol, Any}(:lastflag =&amp;gt; 1, :indent =&amp;gt; 2, :text =&amp;gt; &amp;quot;3&amp;quot;)
Dict{Symbol, Any}(:lastflag =&amp;gt; 0, :indent =&amp;gt; 3, :text =&amp;gt; &amp;quot;x&amp;quot;)
Dict{Symbol, Any}(:lastflag =&amp;gt; 1, :indent =&amp;gt; 4, :text =&amp;gt; &amp;quot;y&amp;quot;)
Dict{Symbol, Any}(:lastflag =&amp;gt; 1, :indent =&amp;gt; 5, :text =&amp;gt; &amp;quot;z&amp;quot;)
Dict{Symbol, Any}(:lastflag =&amp;gt; 1, :indent =&amp;gt; 3, :text =&amp;gt; &amp;quot;x&amp;quot;)
```
&amp;quot;&amp;quot;&amp;quot;
function add_lastflag(data)
map(x -&amp;gt; x[:lastflag] = 0, data)
indent_list = sort([i for i in Set([d[:indent] for d in data])])
diff_list = Vector()
for (st, nd) in zip(indent_list[begin:end-1], indent_list[begin+1:end])
push!(diff_list, [findall([i[:indent] for i in data] .== st), findall([j[:indent] for j in data] .== nd)])
end
last_li_index = Vector{Int}([1])
# push!(last_li_index, diff_list[1][2][end])
for (i, j) in diff_list
di = diff(i)
if length(di) == 0
push!(last_li_index, j[end])
else
for (i_di, d) in enumerate(di)
if d &amp;gt; 1
s = i[i_di]
e = i[i_di+1]
target = j[s .&amp;lt; j .&amp;lt; e]
if length(target) &amp;gt; 0
push!(last_li_index, maximum(target))
end
end
end
push!(last_li_index, maximum(j))
end
end
last_li_index = sort([i for i in Set(last_li_index)])
result = data
for l in last_li_index
result[l][:lastflag] = 1
end
return result
end
&amp;quot;&amp;quot;&amp;quot;
path_matrix(data)
`convert_text_to_vec` と `add_information_indent` を用いてtree構造にする際に必要な枝の位置をまとめたMatrixを返す
# Examples
```julia-repl
julia&amp;gt; path_matrix(add_information_indent(convert_text_to_vec(tree_text)))
15×5 Matrix{Int64}:
0 0 0 0 0
1 0 0 0 0
1 1 0 0 0
1 1 0 0 0
1 1 1 0 0
1 1 1 0 0
1 1 0 0 0
1 0 0 0 0
0 1 0 0 0
0 1 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 1 1 0
0 0 1 0 1
0 0 1 0 0
```
&amp;quot;&amp;quot;&amp;quot;
function path_matrix(data)
len_data = length(data)
max_data = maximum([i[:indent] for i in data])
ver_temp = zeros(Int, len_data, max_data)
for m in 1:max_data
temp_list = zeros(len_data)
for (i, d) in enumerate(reverse(data))
if d[:indent] == m
temp_list[i:end] .= 1
break
end
end
ver_temp[:, m] = temp_list
end
hor_temp = zeros(Int, len_data, max_data)
for (i, d) in enumerate(data)
temp_list = [(if i &amp;lt;= d[:indent] 1 else 0 end) for i in 1:max_data]
hor_temp[i,:] = temp_list
end
mul_matrix = hor_temp .* reverse(ver_temp, dims=1)
result = mul_matrix
return result
end
&amp;quot;&amp;quot;&amp;quot;
convert_text_to_vec(text)
`convert_text_to_vec`, `add_lastflag` と `add_information_indent` を用いてVector化したテキストのブロック最後のフラグ情報を集計する
# Examples
```julia-repl
julia&amp;gt; add_path(add_lastflag(add_information_indent(convert_text_to_vec(tree_text))))
15-element Vector{Dict}:
Dict{Symbol, Any}(:lastflag =&amp;gt; 1, :path =&amp;gt; &amp;quot;&amp;quot;, :indent =&amp;gt; 0, :text =&amp;gt; &amp;quot;test&amp;quot;)
Dict{Symbol, Any}(:lastflag =&amp;gt; 0, :path =&amp;gt; &amp;quot;├── &amp;quot;, :indent =&amp;gt; 1, :text =&amp;gt; &amp;quot;aaa&amp;quot;)
Dict{Symbol, Any}(:lastflag =&amp;gt; 0, :path =&amp;gt; &amp;quot;│   ├── &amp;quot;, :indent =&amp;gt; 2, :text =&amp;gt; &amp;quot;1&amp;quot;)
Dict{Symbol, Any}(:lastflag =&amp;gt; 0, :path =&amp;gt; &amp;quot;│   ├── &amp;quot;, :indent =&amp;gt; 2, :text =&amp;gt; &amp;quot;2&amp;quot;)
Dict{Symbol, Any}(:lastflag =&amp;gt; 0, :path =&amp;gt; &amp;quot;│   │   ├── &amp;quot;, :indent =&amp;gt; 3, :text =&amp;gt; &amp;quot;x&amp;quot;)
Dict{Symbol, Any}(:lastflag =&amp;gt; 1, :path =&amp;gt; &amp;quot;│   │   └── &amp;quot;, :indent =&amp;gt; 3, :text =&amp;gt; &amp;quot;y&amp;quot;)
Dict{Symbol, Any}(:lastflag =&amp;gt; 1, :path =&amp;gt; &amp;quot;│   └── &amp;quot;, :indent =&amp;gt; 2, :text =&amp;gt; &amp;quot;3&amp;quot;)
Dict{Symbol, Any}(:lastflag =&amp;gt; 1, :path =&amp;gt; &amp;quot;└── &amp;quot;, :indent =&amp;gt; 1, :text =&amp;gt; &amp;quot;bbb&amp;quot;)
Dict{Symbol, Any}(:lastflag =&amp;gt; 0, :path =&amp;gt; &amp;quot; ├── &amp;quot;, :indent =&amp;gt; 2, :text =&amp;gt; &amp;quot;1&amp;quot;)
Dict{Symbol, Any}(:lastflag =&amp;gt; 0, :path =&amp;gt; &amp;quot; ├── &amp;quot;, :indent =&amp;gt; 2, :text =&amp;gt; &amp;quot;2&amp;quot;)
Dict{Symbol, Any}(:lastflag =&amp;gt; 1, :path =&amp;gt; &amp;quot; └── &amp;quot;, :indent =&amp;gt; 2, :text =&amp;gt; &amp;quot;3&amp;quot;)
Dict{Symbol, Any}(:lastflag =&amp;gt; 0, :path =&amp;gt; &amp;quot; ├── &amp;quot;, :indent =&amp;gt; 3, :text =&amp;gt; &amp;quot;x&amp;quot;)
Dict{Symbol, Any}(:lastflag =&amp;gt; 1, :path =&amp;gt; &amp;quot; │   └── &amp;quot;, :indent =&amp;gt; 4, :text =&amp;gt; &amp;quot;y&amp;quot;)
Dict{Symbol, Any}(:lastflag =&amp;gt; 1, :path =&amp;gt; &amp;quot; │   └── &amp;quot;, :indent =&amp;gt; 5, :text =&amp;gt; &amp;quot;z&amp;quot;)
Dict{Symbol, Any}(:lastflag =&amp;gt; 1, :path =&amp;gt; &amp;quot; └── &amp;quot;, :indent =&amp;gt; 3, :text =&amp;gt; &amp;quot;x&amp;quot;)
```
&amp;quot;&amp;quot;&amp;quot;
function add_path(data)
path_m = path_matrix(data)
v, h = size(path_m)
for y in 1:v
target_vec = path_m[y,:]
path_index = findall(path_m[y,:] .== 1)
if length(path_index) == 0
path_string = &amp;quot;&amp;quot;
else
max_index = maximum(path_index)
path_string = join([(if path_m[y,i] == 1 &amp;quot;│   &amp;quot; else &amp;quot; &amp;quot; end) for i in 1:max_index-1])
if data[y][:lastflag] == 0
path_string *= &amp;quot;├── &amp;quot;
else
path_string *= &amp;quot;└── &amp;quot;
end
end
data[y][:path] = path_string
end
result = data
return result
end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;全ての関数にdocstringをつけてあります。特に特殊なパッケージ等を使っているわけでもないので、Juliaさえインストールされていれば動きます。&lt;/p&gt;
&lt;h1 id="結果"&gt;結果&lt;/h1&gt;
&lt;p&gt;実行方法は下記のようになります。&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-julia" data-lang="julia"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;add_path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;add_lastflag&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;add_index&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;add_information_indent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;convert_text_to_vec&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tree_text&lt;/span&gt;&lt;span class="p"&gt;)))));&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:path&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:text&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;深夜テンションで書いたので非常に冗長な構造になっていますが、動くからヨシ。&lt;/p&gt;
&lt;p&gt;出力
&lt;figure &gt;
&lt;div class="flex justify-center "&gt;
&lt;div class="w-full" &gt;&lt;img src="./screen.png" alt="png" loading="lazy" data-zoomable /&gt;&lt;/div&gt;
&lt;/div&gt;&lt;/figure&gt;
ちゃんと思った形式に表示されました。&lt;/p&gt;
&lt;h1 id="終わりに"&gt;終わりに&lt;/h1&gt;
&lt;p&gt;アルゴリズムとして複雑な処理が必要で辛かったです。「箇条書き→ツリー形式」の変換がしたいのでJuliaをインストールする?正しい判断です!!&lt;/p&gt;</description></item><item><title>Juliaの自動微分パッケージ Zygote の紹介</title><link>https://www.yuyakaneta.page/post/zygote/</link><pubDate>Mon, 12 Apr 2021 22:25:48 +0900</pubDate><guid>https://www.yuyakaneta.page/post/zygote/</guid><description/></item></channel></rss>