Last active
March 6, 2016 05:05
-
-
Save yamasushi/b088d332b6b211a8a01a to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env gosh | |
; https://gist.github.com/yamasushi/b088d332b6b211a8a01a | |
(use gauche.parseopt) | |
(use srfi-13) | |
(use srfi-14) | |
(use gauche.lazy) | |
(use gauche.collection) | |
(use gauche.sequence) | |
(use srfi-27) | |
(define cs-kanji #[々〇〻\x3400;-\x9FFF;\xF900;-\xFAFF;\x20000;-\x2FFFF;] ) | |
(define cs-hirakana #[ぁ-ん]) | |
(define cs-katakana #[ァ-ン]) | |
(define cs-kana (char-set-union cs-hirakana cs-katakana) ) | |
(define cs-kankana (char-set-union cs-kanji cs-kana) ) | |
(define cs-tweet | |
(char-set-union | |
cs-kankana | |
#[・~〜ー、…!!??,,..。] | |
) ) | |
(define rand (make-random-source)) | |
(random-source-randomize! rand) | |
(define (string-take* s n) (if (< (string-length s) n) s (string-take s n) ) ) | |
(define (main args) (let-args (cdr args) | |
( (nchr "n=i" 140) | |
(tag "t=s" ) | |
(echo-mode "e|echo" ) | |
. files | |
) | |
($ (^s | |
(format #t "~a" s) | |
(if echo-mode (format (standard-error-port) "~a~%" s) ) ) | |
$ (cut string-append | |
<> | |
(if tag (string-append " #" tag) "") ) | |
$ (cut string-take* <> nchr ) | |
$ (cut regexp-replace #/^[、ー]+/ <> "") | |
; | |
$ (cut regexp-replace-all #/([んン])[んン]+/ <> "\\1") | |
$ (cut regexp-replace-all #/([ッっ])[ッっ]+/ <> "\\1") | |
; | |
$ (cut regexp-replace-all #/[、…]([ーをヲんンっッぁぃぅぇぉゃゅょァィゥェォャュョ])/ <> "\\1") | |
$ (cut regexp-replace-all #/ー+([ぁぃぅぇぉゃゅょァィゥェォャュョ])/ <> "\\1") | |
$ (cut regexp-replace-all #/[、…]{2,}/ <> "…") | |
; | |
$ (cut regexp-replace-all #/([^ぁ-んァ-ンー])ー+/ <> "\\1") | |
$ (cut regexp-replace-all #/([んンっッ])ー+/ <> "\\1") | |
; | |
$ (cut shuffle <> rand) | |
$ string-concatenate | |
$ lfilter ($ string-any cs-kankana $) | |
$ lmap (^s (regexp-replace-all #/[.,、・]{2,}/ s "…") ) | |
$ lmap (^s (regexp-replace-all #/[~〜・!!??,,..。]/ s "、") ) | |
$ lmap (^s (regexp-replace-all #/[.,、・]{2,}/ s "…") ) | |
$ lmap (^s | |
(if (#/^.*[、!!??,,..。]$/ s) | |
s | |
(string-append s "、") ) ) | |
$ lmap ($ string-filter cs-tweet $) | |
$ lfilter ($ string-any cs-kankana $) | |
(if (null? files) | |
($ port->string-lseq $ standard-input-port) | |
($ lconcatenate $ lmap ($ port->string-lseq $ open-input-file $) files) | |
) ) | |
) ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment