arr = []
arr << dec_num
str = arr.pack("U") #U=UTF-8 => encode unicode 8364 into a UTF-8
character.
puts str
#!/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
#!/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
#!/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
#!/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
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(','))
#!/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)
這篇文章受密碼保護,請輸入密碼後查看內容。
#!/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
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)