反正就是一整個好玩!!!(  ̄ c ̄)y▂ξ

目前日期文章:201001 (4)

瀏覽方式: 標題列表 簡短摘要

記錄執行時間

#!/bin/sh

before="$(date +%s)"
#./cmd
sleep 3
after="$(date +%s)"
elapsed_seconds="$(expr $after - $before)"
echo Elapsed time for code block: $elapsed_seconds

Ruby 一行文

ruby -e "s=Time.now ; sleep 2.5; p (s-Time.now).abs"

 

bash的printf

a=99 ; printf "%03d\n" $a


igogo 發表在 痞客邦 留言(0) 人氣()


#!/usr/bin/ruby -Ku
# encoding: utf-8
require 'amalgalite'
require 'fileutils'

class Csv_to_db
  def initialize(db_name, csv_name)
    db_file = "#{db_name}.db"
    FileUtils.rm(db_file) if File.exist?(db_file)  # start with a clean slate
    my_db=Amalgalite::Database.new(db_file)
    csv_rows = File.read("#{csv_name}", :encoding => "utf-8").split("\n")
    col_names = csv_rows[0].split(',')
    col_names_types= col_names.map do |col| col + ' char(1)' end
    sql_create = "create table ctr_map(%s)" % col_names_types.join(', ')
    my_db.execute(sql_create)
    stmt = csv_rows[1..-1].map do |x|
      "insert into ctr_map (%s) values (%s)" % [col_names.join(', '), x]
    end
    stmt.each do |row_insert|
      my_db.execute(row_insert)
    end
    puts "ok"
  end
end

db_name = "ctr_map"
csv_name = "chinese_map.csv"
Csv_to_db.new(db_name,csv_name)

lala

inser  into sqlite3 db

  def save_to_db
    str = self.gsub(/,/,"\",\"").gsub(/^/,"\"").gsub(/$/,"\"")
    my_db=Amalgalite::Database.new(@@gsp_db)
    stmt = my_db.execute("select * from gsp_corpus where word = ?", "#{self.split(',')[0]}")
  if !(stmt.any?)
   insert_stmt = "insert into gsp_corpus (%s) values (%s)" % [@@field_name.split.join(','), str]
  end
    my_db.execute(insert_stmt)
    my_db.close
  end


#str = "教育部,gs"

igogo 發表在 痞客邦 留言(0) 人氣()

  • 這是一篇加密文章,請輸入密碼
  • 密碼提示:西江小館
  • 請輸入密碼:


#!/usr/bin/ruby -Ku
# encoding: UTF-8
#將停用字移除
class String
  def to_rm_stop_words(stop_words)
    msg = self
      stop_words.each do |e|
        msg = msg.gsub("#{e}",',')
      end
    return msg
  end
end


stop_words_db = "stop_words.csv" #stop words list
dst_dir = "dst_dir" #處理檔案放置位置
file_array = Dir.glob("*.txt")

#stop_words
stop_words = Array.new
  File.open("#{stop_words_db}") do |content|
    while line = content.gets
      stop_words << line.strip
    end
  end

file_array.each do |each_file|
  msg = String.new #msg的型別為string
  File.open("#{each_file}") do |content|
    while line = content.gets
      msg << line.to_s.strip.chomp
    end
  end
   revised_msg = msg.to_rm_stop_words(stop_words)
.gsub(/,+/,",")


  File.open("./#{dst_dir}/#{each_file}","a") do |content|
    content.puts revised_msg
  end
  msg.clear
end




igogo 發表在 痞客邦 留言(0) 人氣()