Skip to content

Instantly share code, notes, and snippets.

# -*- coding: utf-8 -*-
# Google 翻訳
require 'rubygems'
require 'json'
require 'open-uri'
URL = "http://ajax.googleapis.com/ajax/services/language/translate"
query = ARGV[0]
# Translation
function gtranslation() {
RESULT=`ruby "gtranslation.rb のパス" "${RBUFFER}"`
zle push-line
echo "${RESULT}"
zle send-break
}
zle -N gtranslation
bindkey "^t" gtranslation
@jewel12
jewel12 / gist:631569
Created October 18, 2010 01:47
fopner.rb
#!/usr/bin/ruby
# 拡張子が .gz であれば、zlib でオープンする。
require 'zlib'
module FOpener
def self.open( f_name )
f = Filename.extname(f_name) == ".gz" ? Zlib::GzipReader.open( f_name ) : File.open( f_name )
yield( f )
f.close
end
require 'tokyocabinet'
include TokyoCabinet
N = 5
class NgramCounter
def initialize(f_name)
@tcdb = TokyoCabinet::ADB::new
@tcdb.open("*") # オンメモリ
end
#! /bin/sh
# 親プロセスが死んだら、孤児プロセスを kill する
echo $PPID >&2
while [ ! "`ps --no-headers -p $PPID`" = "" ]
do
sleep 5
done
@jewel12
jewel12 / cd_last_opt
Created February 10, 2011 19:22
最後の引数のディレクトリにcd
function cd_last_opt() {
eval $BUFFER
cd `echo -c "${BUFFER}" | awk '{print $NF}'`
zle send-break
}
zle -N cd_last_opt
bindkey "^g" cd_last_opt
@jewel12
jewel12 / arr_comb.rb
Created February 24, 2011 01:20
順序を保持した組み合わせ
# -*- coding: utf-8 -*-
class Array
def comb
n = self.length - 1
0.upto(n) do |head_n|
head_n.upto(n) {|tail_n| yield self[head_n..tail_n] }
end
end
end
@jewel12
jewel12 / du_sort.rb
Created March 21, 2011 13:09
du -h の結果をソートする
# coding:utf-8
# du -h の結果をソート
# usage: du -h | ruby du_sort.rb
results = []
while l=STDIN.gets
size_h = l.split("\t")[0]
m = 1
case size_h[-1].chr
when "K"; m=1000
@jewel12
jewel12 / indexer.rb
Created May 18, 2011 03:47
Arrayにインデックスを張る(define_methodについて)
class Array
def set_index( index_name, i )
eval("
class << self
define_method( '#{index_name}' ) { return self[#{i}] }
end
")
end
end
@jewel12
jewel12 / wer.rb
Created May 18, 2011 13:21
Word Error Rate
# coding:utf-8
# Word Error Rate (空白が区切り文字前提)
module Eval
# cost[挿入, 削除, 置換]
def self.wer( str1, str2, c=[1,1,1] )
s1 = str1.split(' ')
s2 = str2.split(' ')
d = Array.new(s1.size+1) { Array.new(s2.size+1){ 0 } }