Implement seq2dot
authorSiraaj Khandkar <siraaj@khandkar.net>
Mon, 21 Sep 2020 14:14:41 +0000 (10:14 -0400)
committerSiraaj Khandkar <siraaj@khandkar.net>
Mon, 21 Sep 2020 14:14:41 +0000 (10:14 -0400)
graphing a sequence as pairs of prev->next relationships

home/bin/seq2dot [new file with mode: 0755]

diff --git a/home/bin/seq2dot b/home/bin/seq2dot
new file mode 100755 (executable)
index 0000000..b79c8e4
--- /dev/null
@@ -0,0 +1,44 @@
+#! /usr/bin/awk -f
+#
+# Graph a sequence of lines as prev->next relationships,
+# highlighting frequencies of pairings.
+#
+
+BEGIN {print "digraph {"}
+
+{
+    prev = prev ? prev : "--"
+    curr = $1
+    ++nlinks[prev]
+    ++nlinks_to[prev, curr]
+    prev = curr
+}
+
+END {
+    for (src_dst in nlinks_to) {
+        split(src_dst, sd, SUBSEP);
+        src = sd[1]
+        dst = sd[2]
+        m = nlinks[src]
+        n = nlinks_to[src, dst]
+        penwidth = num_scale(n, m, 1, 9)
+        color = sprintf("/orrd9/%d", num_scale(n, m, 2, 9))
+        label = sprintf("%s %.2f%%", src, (n / m) * 100)
+        printf \
+            "\"%s\" -> \"%s\" \
+                [ label=\"%s\"\
+                , fontname=monospace \
+                , fontsize=8 \
+                , penwidth=%d \
+                , color=\"%s\" \
+                , dir=both \
+                , arrowtail=odot \
+                ];\n", \
+            src, dst, label, penwidth, color;
+    }
+    print "}"
+}
+
+function num_scale(src_cur, src_max, dst_min, dst_max) {
+    return dst_min + ((src_cur * (dst_max - dst_min)) / src_max)
+}
This page took 0.03242 seconds and 4 git commands to generate.