http://www.ruby-forum.com/topic/182957
 
arr = []
arr << dec_num
str = arr.pack("U") #U=UTF-8 => encode unicode 8364 into a UTF-8
character.
puts str

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

八取四不重覆,並且排列順序列入考慮(permutation)





#!/usr/bin/env ruby


require 'permutation'


str = "abcdefgh"
nums = 4  #取的個數
perm = Permutation.for("#{str}")
arr = Array.new
arr = perm.map { |p| p.project[0..(nums-1)] }.sort.uniq


arr.each do |e|
 puts e
end


puts arr.uniq.size


 


 





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

與指令paste的功用相同





#!/usr/bin/ruby


files = Array.new
files = ["a.txt","b.txt","c.txt"]


tmp = Array.new
raw_data = Array.new
for i in 0..files.size-1
  File.open(files[i]) do |txt|


    while line=txt.gets
      tmp << line.chomp
    end
    raw_data << tmp.join(',').split(',')
    tmp.clear
  end
end


raw_data.transpose


for i in 0..raw_data.transpose.size-1
  puts raw_data.transpose[i].join("\t")
end











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

get mms file -> mp3






#!/usr/bin/ruby -Kw
require 'fileutils'
#mms_stream = ARGV[0]
class Mms 


    def self.FetchMms(mms_stream)
    wav_file=File.basename("#{mms_stream}").gsub(/.wma/,".wav")
            #mms://url


            #存成wav檔
  system("/usr/bin/mplayer -ao pcm:file=#{wav_file} #{mms_stream}")
    mp3_file=wav_file.gsub(/.wav/,".mp3")
            #puts mp3_file
    #將wav轉成mp3檔
    system("/usr/bin/lame -b 42 #{wav_file} #{mp3_file}")
    FileUtils.rm "#{wav_file}"
  end
end
if ARGV[0].nil?
  puts "usage: mms url or file"
else
  Mms.FetchMms(ARGV[0])
end





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

 
我真是手賤,
在這時刻還隨便按了個emerge --udate ruby,
結果真是靠北了....gem19就不能用,

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

ruby with sqlite3





#!/usr/bin/ruby -Ku
# encoding: utf-8
require 'amalgalite'
require 'fileutils'
class String
    @@tmp_db = "tmp_db.txt"
    @@gsp_db = "gsp_corpus.db"
    @@gsp_corpus = "gsp_corpus" #table name                                            @@field_name = "word source"
  def to_db #字串與資料庫查詢
    FileUtils.rm(@@tmp_db) if File.exist?(@@tmp_db)  # start with a clean slate    
    my_db=Amalgalite::Database.new(@@gsp_db)
    stmt = my_db.execute("select * from gsp_corpus where word = ?", "#{self}")
    my_db.close


    if stmt.any?
      File.open("./#{@@tmp_db}","a") do |txt|
               txt.puts stmt.join(',')
      end
      res = File.open("./#{@@tmp_db}").read
      return res
    end
  end #end to_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]
   my_db.execute(insert_stmt)
   puts "insert into sqlite3_db sucessfully"
  end
    my_db.close




  end
end #end class







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

raw tf csv file


a,1,2,3,4


b,1,2,3,4


c,1,2,3,4




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

求一超球体的圓心與半徑





#!/usr/bin/ruby -Ku
require 'ai4r'
require 'csv'
def EuclideanDistance(x,y)
  sum = 0
  for i in (0..x.size-1)
    #printf("x[%d]=%f,y[%d]=%f\n",i,x[i],i,y[i])
    ->(px,py){ sum += (px-py)**2.0}.(x[i],y[i])
  end
return Math.sqrt(sum)
end
csv_file = "tfidf-ckip-a.csv"
f=File.new("#{csv_file}")
data = Array.new
while (line = f.gets)
  data << line.split(',').collect  do |s| s.to_f end
end
#data = [[1,1,1], [3,3,3],[9,9,9]]
ai4r_data = Ai4r::Data::DataSet.new(:data_items=> data)
kmeans = Ai4r::Clusterers::KMeans.new
#abuild(dataset,clusters)
res=kmeans.build(ai4r_data,1)
#puts res.inspect
center = res.centroids[0] #此球体圓心
r = 0 #此球体半徑


 r = 0 #此球体半徑
tmp = 0
printf("計算中...\n")
for i  in (0..data.size-1)
  tmp = EuclideanDistance(center,data[i])
  r = tmp if(tmp > r)
end
printf("球体,%s\n",csv_file)
printf("球体半徑為,%f\n",r)
printf("球体圓心為,%s\n",center.join(','))







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)





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

這篇文章受密碼保護,請輸入密碼後查看內容。

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) 人氣()






class String


  def mbsubstr1(*range)


      split(//)[*range].join("")


  end


 


   def mbsubstr2(idx, len)


       slice(/\A.{#{idx}}(.{0,#{len}}/m, 1)


   end


end


p "一大串字一大串字".mbsubstr1(2,3)


p "一大串字一大串字".mbsubstr1(2..3)


p "一大串字一大串字".mbsubstr2(2,2)





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

1 2 3
Blog Stats
⚠️

成人內容提醒

本部落格內容僅限年滿十八歲者瀏覽。
若您未滿十八歲,請立即離開。

已滿十八歲者,亦請勿將內容提供給未成年人士。