Skip to content

Instantly share code, notes, and snippets.

View xixilive's full-sized avatar
🌴
On vacation

xixilive xixilive

🌴
On vacation
View GitHub Profile
@xixilive
xixilive / shorten.rb
Created April 28, 2012 10:40
Base32 string encoder, generate a unique shorten string from a long string, such as a url
require 'digest/md5'
module Shorten
#base32
TABLE = %w(a b c d e f g h i j k l m n o p q r s t u v w x y z 2 3 4 6 7 8)
# short('balabala') => Array
# short('balabala',1) => String, Array[1]
def self.short long_str, index = nil
index = (0..3).to_a.include?(index) ? index : nil
hash_string = Digest::MD5.hexdigest(long_str)
@xixilive
xixilive / string_util.php
Created May 4, 2012 14:59
php function, Base32 string encoder
<?php
/**
* StringUtil
*
* @author mickey
* @version 1.0
* @package Utility
*/
final class StringUtil
{
@xixilive
xixilive / StringEncoder.cs
Created May 4, 2012 15:00
Base32 string encoder developing with C#
using System;
using System.Collections.Generic;
using System.Text;
namespace Mickey.Utils
{
/// <summary>
/// 字符串编码/解码
/// </summary>
public static class StringEncoder
{
@xixilive
xixilive / pinyin.php
Created May 4, 2012 15:06
get Chinese Pinyin
<?php
/**
* StringUtil
*
* @author mickey
* @version 1.0
* @package Utility
*/
final class StringUtil
{
@xixilive
xixilive / random_selector.rb
Created July 3, 2012 03:27
RandomSelector for ActiveModel via Mongoid
module RandomSelector
class << self
def included(base)
base.module_eval do
def self.random n, conditions=nil
criteria = conditions.is_a?(Mongoid::Criteria) ? (self.where({}).merge(conditions)) : self.where(conditions||{})
cc = criteria.count
mo = [cc-n,2].max
criteria.skip((rand*cc%mo).to_i).limit(n)
end
@xixilive
xixilive / gist:6934626
Created October 11, 2013 13:25
Install sublime text2 in ubuntu via apt-get
sudo add-apt-repository ppa:webupd8team/sublime-text-2
sudo apt-get update
sudo apt-get install sublime-text-2
Summarizer(originalText, maxSummarySize):
    // 计算原始文本的词频,生成一个数组,比如[(10,'the'), (3,'language'), (8,'code')...]
    wordFrequences = getWordCounts(originalText)
    // 过滤掉停用词,数组变成[(3, 'language'), (8, 'code')...]
    contentWordFrequences = filtStopWords(wordFrequences)
    // 按照词频进行排序,数组变成['code', 'language'...]
    contentWordsSortbyFreq = sortByFreqThenDropFreq(contentWordFrequences)
    // 将文章分成句子
    sentences = getSentences(originalText)
    // 选择关键词首先出现的句子
@xixilive
xixilive / gist:9478656
Created March 11, 2014 02:53
Mongoid Utils
module MongoidUtils
module RandomDocuments
extend ActiveSupport::Concern
module ClassMethods
def random n = 1
skip((rand * 99999999).to_i % ([count-n, 2].max)).limit(n)
end
end
end
@xixilive
xixilive / gist:9502333
Created March 12, 2014 07:24
Sublime2 Package Controll install script
import urllib2,os;pf='Package Control.sublime-package';ipp=sublime.installed_packages_path();os.makedirs(ipp) if not os.path.exists(ipp) else None;open(os.path.join(ipp,pf),'wb').write(urllib2.urlopen('http://sublime.wbond.net/'+pf.replace(' ','%20')).read())
@xixilive
xixilive / devise.zh-CN.yml
Created April 14, 2014 10:40
devise.3.1.2 i18n zh-CN
zh-CN:
devise:
helpers:
labels:
username_placeholder: "用户名/手机号码/电子邮件"
email_placeholder: "电子邮件"
mobile_placeholder: "手机号码"
password_placeholder: "密码"
password_conformation_placeholder: "确认密码"