Snipperize snippets

Snipperize is a code clips warehouse, support nearly all the computer languages.
Here are the latest snippets. Please choose your favorite one or add a new one.

Execute a Unix Command with Nodejs

http://nodejs.org/api.html#_child_processes

Javascript / nodejs, child_process, exec, unix, command / by ThePeppersStudio (15 days, 18.05 hours ago)

New Way to Detect IE with Javascript

If you're not in IE (or IE version is less than 5) then: ie === undefined If you're in IE (>5) then you can determine which version: ie === 7; // IE7 Thus, to detect IE: if (ie) {} And to detect the version: ie === 6 // IE6 ie > 7 // IE8, IE9 ... ie < 9 // Anything less than IE9

Javascript / ie, detect / by ThePeppersStudio (15 days, 18.09 hours ago)

HTML5 Shiv from Google Code

<!--[if lt IE 9]><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><![endif]-->

XML/HTML / html5, ie, google, googlecode / by ThePeppersStudio (15 days, 18.14 hours ago)

Get IP

copy to .bash_profile: alias getip='ifconfig | sed -n "/en0/,/netmask/ p" | grep "inet " | awk ''{print\$2}'''

Bash/Shell / getip, ip, ifconfig / by ThePeppersStudio (39 days, 14.85 hours ago)

Drawing a Standard Focus Ring Inside of a Cell Bounds

Drawing a Standard Focus Ring Inside of a Cell Bounds

Objective-C / draw, focus ring, cell, nsview, nsrect / by ThePeppersStudio (53 days, 11.87 hours ago)

Correct Way to Draw Background Using a Pattern Image

Correct Way to Draw Background Using a Pattern Image

Objective-C / background, pattern, image, nsimage, nscolor, nsgraphicscontext / by ThePeppersStudio (53 days, 11.93 hours ago)

Get iPhone/iPad/iPod Touch Hardware Generation

How to get iPhone/iPad/iPod Touch hardware generation

Objective-C / iphone, ipad, ipod, platform, generation, sysctlbyname, sysctl / by ThePeppersStudio (56 days, 10.01 hours ago)

Hamming Distance

在信息领域,两个长度相等的字符串的海明距离是在相同位置上不同的字符的个数,也就是将一个字符串替换成另一个字符串需要的替换的次数。 例如: "toned" and "roses" is 3. 1011101 and 1001001 is 2. 2173896 and 2233796 is 3. 对于二进制来说,海明距离的结果相当于 a XOR b 结果中1的个数。

Python / hamming distance, similarity, distance / by ThePeppersStudio (58 days, 13.15 hours ago)

利用simhash来进行文本去重复

传统的hash函数能够将一样的文本生成一样的hash函数,但是,通过simhash方法,能够差不多相同的文档得到的hash函数也比较相近。 Charikar's hash 通过Charikar‘s hash,能够将比较相似度的文档得到比较相近的fingerprint。 该算法的流程如下: * Document is split into tokens (words for example) or super-tokens (word tuples) * Each token is represented by its hash value; a traditional hash function is used * Weights are associated with tokens * A vector V of integers is initialized to 0, length of the vector corresponds to the desired hash size in bits * In a cycle for all token's hash values (h), vector V is updated: o ith element is decreased by token's weight if the ith bit of the hash h is 0, otherwise o ith element is increased by token's weight if the ith bit of the hash h is 1 * Finally, signs of elements of V corresponds to the bits of the final fingerprint 该hash不是将文档总体计算hash值,而是将文档中的每个token计算哈希值,对文档中每个token的hash值,按照位 对hash值进行求和,如果当前token的hash值在该位上是0,则减去1,如果在该位上是1,则加上1.将所有的token按照这种方式累加,求的最终的值作为fingerprint。

Python / simhash, hash, Charikar, similarity, duplicate / by ThePeppersStudio (58 days, 13.20 hours ago)

Disallow Linking to the Images from Outside of your Site based on Htaccess

Replace "mysite" with your own and this will block access to sites attempting to link to your images. Note, it's not necessarily always a bad thing for people to link to your images. If you run a blog, portfolio, or sales gallery, etc, you may actually want your images to show up in google image search results and the like. Careful that you're not blocking your goals along with the images. Make sure this script is for you.

Apache / HTTP_REFERER, rewrite, htaccess, image, block / by ThePeppersStudio (58 days, 14.78 hours ago)