Skip to content

Instantly share code, notes, and snippets.

@no6v
Last active December 15, 2015 04:29
Show Gist options
  • Save no6v/5201578 to your computer and use it in GitHub Desktop.
Save no6v/5201578 to your computer and use it in GitHub Desktop.
# encoding: UTF-8
str = <<'EOT'
連続する、、句読点。
どう扱えば、いいのか。。
末尾に、、、句読点がなくても
大丈夫だょ
EOT
punc = "、。,."
h = "ー「」→↑←↓=…"
v = "|¬∟↓→↑←॥:"
hv = (h + v)
vh = (v + h)
lines = []
str.tr(hv, vh).each_line{|line|
lines << line.chomp.split(/([#{punc}]+)/o)
}
status = []
lines.each{|line| status << line.each_slice(2).with_object(["", ""]){|(main, puncta), (punctuation, mains)|
puncta ||= " "
punctuation << " " * main.size.pred + puncta
mains << main + " " * puncta.size.pred
}}
status.flatten!.each{|line| line.chomp!(" ")}
max = status.map(&:size).max
status = status.map{|line|
line.chars.fill(" ", line.size...max)
}.transpose.map{|line| line.reverse.join}.join("\n")
status.gsub!(/ +$/, "")
puts status
# >> 大 末 ど 連
# >> 丈 尾 う 続
# >> 夫 に、扱 す
# >> だ  、え る、
# >> ょ  、ば、 、
# >>   句 い 句
# >>   読 い 読
# >>   点 の 点。
# >>   が か。
# >>   な  。
# >>   く
# >>   て
# >>   も
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment