Last active
March 29, 2025 04:27
-
-
Save pgtwitter/2e0e0037928680dd6b10d531c591595e to your computer and use it in GitHub Desktop.
モール信号をツリー表示(dotファイル)にするpythonスクリプト(自作 / Grok3版 https://gist.github.com/pgtwitter/1d66a655c3c53b021b4ccc46f3fc8cc3 ).morse.txtを読み込んでmorse_tree.dotを作成する.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def morse2Dot(fin, fout): | |
class m: | |
def __init__(self, l, c): | |
self.c, self.n, self.cms, self.eA, self.nL, self.l, self.p = c, c.count('.'), [], DOT_eA if c[-1] == '.' else '', f'"{NA+c if l == NA else l}" [shape={SC if c[-1] == "." else SR}{NA_nA if l == NA else ""}];', f'{NA}{c}' if l == NA else l, RC if len(c) == 1 else c[0:-1] | |
def eLines(n): | |
ret = [f'"{n.l}" -> "{c.l}"[penwidth=2{c.eA}];' for c in n.cms] | |
ret.extend([l for c in n.cms for l in eLines(c)]) | |
return ret | |
RL, RC, NA, SC, SR, NA_nA, DOT_eA, L1 = '(start)', 'START', 'DUMMY', 'circle', 'rectangle', ',label="(N/A)", margin=0', ', weight=1000, style=dashed', '{rank=same;' | |
L0, ms = f'rankdir=TB;msep=0.5;pad=0.2;"{RL}" [shape=diamond];', {RC: m(RL, RC)} | |
with open(fin, 'r', encoding='utf-8') as f: ms.update({(k, m(v, k)) for v, k in [l.strip().split('\t') for l in f]}) | |
while (flag := sum([len(n.cms) for n in ms.values()])) > -1: | |
for n in [v for v in ms.values() if v.c != RC]: | |
ms[n.p] = ms[n.p] if n.p in ms else m(NA, n.p) | |
if n not in ms[n.p].cms and ms[n.p] != n: ms[n.p].cms.append(n) | |
if flag == sum([len(n.cms) for n in ms.values()]): break | |
ls = ''.join(sorted([n.nL for n in [v for v in ms.values() if v.c != RC]])) + ''.join(sorted(eLines(ms[RC]))) + ''.join([L1 + ' '.join(sorted([f'"{n.l}"' for n in ms.values() if n.n == i])) + '}' for i in range(max([n.n for n in ms.values()]) + 1)]) | |
with open(fout, 'w', encoding='utf-8') as f: f.write('digraph MorseTree {' + L0 + ls + '}') | |
morse2Dot('morse.txt', 'morse_tree.dot') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A .- | |
B -... | |
C -.-. | |
D -.. | |
E . | |
F ..-. | |
G --. | |
H .... | |
I .. | |
J .--- | |
K -.- | |
L .-.. | |
M -- | |
N -. | |
O --- | |
P .--. | |
Q --.- | |
R .-. | |
S ... | |
T - | |
U ..- | |
V ...- | |
W .-- | |
X -..- | |
Y -.-- | |
Z --.. | |
5 ..... | |
4 ....- | |
3 ...-- | |
2 ..--- | |
1 .---- | |
6 -.... | |
7 --... | |
8 ---.. | |
9 ----. | |
0 ----- | |
ピリオド. .-.-.- | |
カンマ, --..-- | |
コロン: ---... | |
問符? ..--.. | |
略符' .----. | |
訂正 ........ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
digraph MorseTree {rankdir=TB;msep=0.5;pad=0.2;"(start)" [shape=diamond];"0" [shape=rectangle];"1" [shape=rectangle];"2" [shape=rectangle];"3" [shape=rectangle];"4" [shape=rectangle];"5" [shape=circle];"6" [shape=circle];"7" [shape=circle];"8" [shape=circle];"9" [shape=circle];"A" [shape=rectangle];"B" [shape=circle];"C" [shape=circle];"D" [shape=circle];"E" [shape=circle];"F" [shape=circle];"G" [shape=circle];"H" [shape=circle];"I" [shape=circle];"J" [shape=rectangle];"K" [shape=rectangle];"L" [shape=circle];"M" [shape=rectangle];"N" [shape=circle];"O" [shape=rectangle];"P" [shape=circle];"Q" [shape=rectangle];"R" [shape=circle];"S" [shape=circle];"T" [shape=rectangle];"U" [shape=rectangle];"V" [shape=rectangle];"W" [shape=rectangle];"X" [shape=rectangle];"Y" [shape=rectangle];"Z" [shape=circle];"dummy----" [shape=rectangle,label="(N/A)", margin=0];"dummy---." [shape=circle,label="(N/A)", margin=0];"dummy--..-" [shape=rectangle,label="(N/A)", margin=0];"dummy.-.-" [shape=rectangle,label="(N/A)", margin=0];"dummy.-.-." [shape=circle,label="(N/A)", margin=0];"dummy..--" [shape=rectangle,label="(N/A)", margin=0];"dummy..--." [shape=circle,label="(N/A)", margin=0];"dummy......" [shape=circle,label="(N/A)", margin=0];"dummy......." [shape=circle,label="(N/A)", margin=0];"カンマ," [shape=rectangle];"コロン:" [shape=circle];"ピリオド." [shape=rectangle];"問符?" [shape=circle];"略符'" [shape=circle];"訂正" [shape=circle];"(start)" -> "E"[penwidth=2, weight=1000, style=dashed];"(start)" -> "T"[penwidth=2];"1" -> "略符'"[penwidth=2, weight=1000, style=dashed];"5" -> "dummy......"[penwidth=2, weight=1000, style=dashed];"8" -> "コロン:"[penwidth=2, weight=1000, style=dashed];"A" -> "R"[penwidth=2, weight=1000, style=dashed];"A" -> "W"[penwidth=2];"B" -> "6"[penwidth=2, weight=1000, style=dashed];"D" -> "B"[penwidth=2, weight=1000, style=dashed];"D" -> "X"[penwidth=2];"E" -> "A"[penwidth=2];"E" -> "I"[penwidth=2, weight=1000, style=dashed];"G" -> "Q"[penwidth=2];"G" -> "Z"[penwidth=2, weight=1000, style=dashed];"H" -> "4"[penwidth=2];"H" -> "5"[penwidth=2, weight=1000, style=dashed];"I" -> "S"[penwidth=2, weight=1000, style=dashed];"I" -> "U"[penwidth=2];"J" -> "1"[penwidth=2];"K" -> "C"[penwidth=2, weight=1000, style=dashed];"K" -> "Y"[penwidth=2];"M" -> "G"[penwidth=2, weight=1000, style=dashed];"M" -> "O"[penwidth=2];"N" -> "D"[penwidth=2, weight=1000, style=dashed];"N" -> "K"[penwidth=2];"O" -> "dummy----"[penwidth=2];"O" -> "dummy---."[penwidth=2, weight=1000, style=dashed];"R" -> "L"[penwidth=2, weight=1000, style=dashed];"R" -> "dummy.-.-"[penwidth=2];"S" -> "H"[penwidth=2, weight=1000, style=dashed];"S" -> "V"[penwidth=2];"T" -> "M"[penwidth=2];"T" -> "N"[penwidth=2, weight=1000, style=dashed];"U" -> "F"[penwidth=2, weight=1000, style=dashed];"U" -> "dummy..--"[penwidth=2];"V" -> "3"[penwidth=2];"W" -> "J"[penwidth=2];"W" -> "P"[penwidth=2, weight=1000, style=dashed];"Z" -> "7"[penwidth=2, weight=1000, style=dashed];"Z" -> "dummy--..-"[penwidth=2];"dummy----" -> "0"[penwidth=2];"dummy----" -> "9"[penwidth=2, weight=1000, style=dashed];"dummy---." -> "8"[penwidth=2, weight=1000, style=dashed];"dummy--..-" -> "カンマ,"[penwidth=2];"dummy.-.-" -> "dummy.-.-."[penwidth=2, weight=1000, style=dashed];"dummy.-.-." -> "ピリオド."[penwidth=2];"dummy..--" -> "2"[penwidth=2];"dummy..--" -> "dummy..--."[penwidth=2, weight=1000, style=dashed];"dummy..--." -> "問符?"[penwidth=2, weight=1000, style=dashed];"dummy......" -> "dummy......."[penwidth=2, weight=1000, style=dashed];"dummy......." -> "訂正"[penwidth=2, weight=1000, style=dashed];{rank=same;"(start)" "0" "M" "O" "T" "dummy----"}{rank=same;"1" "9" "A" "E" "G" "J" "K" "N" "Q" "W" "Y" "dummy---."}{rank=same;"2" "8" "C" "D" "I" "P" "R" "U" "X" "Z" "dummy--..-" "dummy.-.-" "dummy..--" "カンマ," "略符'"}{rank=same;"3" "7" "B" "F" "L" "S" "V" "dummy.-.-." "dummy..--." "コロン:" "ピリオド."}{rank=same;"4" "6" "H" "問符?"}{rank=same;"5"}{rank=same;"dummy......"}{rank=same;"dummy......."}{rank=same;"訂正"}} |
Author
pgtwitter
commented
Mar 21, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment