Skip to content

Instantly share code, notes, and snippets.

@rkroll
rkroll / LetterPairSimilarity.java
Created November 28, 2011 21:23
Letter pair similarity algorithm
import java.util.ArrayList;
import java.util.List;
// from http://www.catalysoft.com/articles/StrikeAMatch.html
public class LetterPairSimilarity {
public static double compareStrings(String str1, String str2) {
List<String> pairs1 = wordLetterPairs(str1.toUpperCase());
List<String> pairs2 = wordLetterPairs(str2.toUpperCase());
int intersection = 0;
@rkroll
rkroll / controller.java
Created December 6, 2011 16:26
Plupload multi-file upload
package controllers;
import java.io.File;
public class Application extends Controller {
public static void index() {
render();
}
@rkroll
rkroll / gist:1594437
Created January 11, 2012 12:27
Guest homepage controller code
public class Guest extends BaseGuestController {
@Inject
private static TohAPI tohApi;
public static void home() {
renderArgs.put("section", "home");
render();
}
}
@rkroll
rkroll / gist:1606916
Created January 13, 2012 15:17
Play framework cookie problem
curl --cookie "mbox=PC#1304629322572-662537.17#1385160974|check#true#1322089034|session#1322088694720-932568#1322090834; s_vi=[CS]v1|26E1881C051D0253-6000012C8005844F[CE]; __qca=P0-981654462-1304636577313; CommunityServer-UserCookie2101=lv=Fri, 01 Jan 1999 00:00:00 GMT&mra=Thu, 05 May 2011 19:40:08 GMT; CommunityServer-UserCookie1458468=lv=Fri, 01 Jan 1999 00:00:00 GMT&mra=Tue, 10 May 2011 12:43:06 GMT; _wt.control-442660-ta_GlobalFactors2=aae2b5156bc7a098ea333beadebdfd626ec1e1d93661dcb4db0e0b48fd0745a1fe5bec55d80cf0f6d1290649d04f59aecac259e5cdbb3d58e0baf7b0e5cba0a1a20730b947181177c63cfd01bb5c87d6e473c293d0a10f4d036d6f95; _wt.user-442660=aae2a6017ed09184ff0031bc8cebb5353fd9f19d753f98a4986b6e10a65d13f98b18a303c31bf0b09579104bc54f5ebbb3bc3d95a0da0263c5fbb0fbf480fea1d66a50d3326b6e0fbb2be917f717cf81b422d699c2b0011201277c45c8cdf02abcf2cca3a2e312c3fd754c0ce285b87505db664a4d33a4514dd4f14af37137b8fab29adce664fe0359d70d; AdSsnGroup=ssngroup=0; Axxd=1; _wt.mode-442660=bfafb7196dce00f5b39a; s_cc=true; s_sq=rditasteofhom
@rkroll
rkroll / gist:1607909
Created January 13, 2012 18:26
Test and change necessary in netty CookieDecoder to support Talligent Cookies
@Test
public void testCookieParsing() {
MyCookieDecoder cookieDecoder = new MyCookieDecoder();
String badCookie = "CommunityServer-UserCookie2101=lv=Fri, 01 Jan 1999 00:00:00 GMT&mra=Thu, 05 May 2011 19:40:08 GMT;";
Set<Cookie> cookies = cookieDecoder.decode(badCookie);
Assert.assertEquals(1, cookies.size());
}
@rkroll
rkroll / HttpResponseWrapper
Created February 21, 2012 22:10
HttpResponse wrapper
public class RHttpResponse extends HttpResponse {
HttpResponse res;
public RHttpResponse(HttpResponse res) {
this.res = res;
}
@Override
public Integer getStatus() {
return res.getStatus();
}
@rkroll
rkroll / gist:1901013
Created February 24, 2012 13:40
Subscriber churn rate
//query was: sql subscriber churn rate
see: http://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&ved=0CDAQFjAA&url=http%3A%2F%2Fwww.data-miners.com%2Fresources%2FCustomer-Insight-Article.pdf&ei=RyJFT6jrNM3sggfgs4iOBA&usg=AFQjCNEcFyH44lLFOdAL_oFl4aWudHP2Ig&sig2=ruBwBS6plnRHl2A3wcPKkg
SELECT ((case when stop_date is NULL then <today>
else stop_date end) – start_date) as tenure,
count(*) as pop_at_t,
@rkroll
rkroll / nodeTemplate.java
Created March 6, 2012 18:45
node template
/**
* A template based implementation for use with an Elasticsearch {@link Node}
* and {@link Client}. Provides an abstraction around common operations needed
* from the Elasticsearch infrastructure to the consuming classes.
*
* Adapted from https://github.com/erezmazor/projectx/tree/master/org.projectx.elasticsearch
*
* @author Rich Kroll
*/
@rkroll
rkroll / SearchResult.java
Created March 6, 2012 18:47
SearchResult
public class SearchResult<T> {
private List<SearchResultRow<T>> data = new ArrayList<SearchResultRow<T>>();
private long numberFound;
private double duration;
private int limit;
private int offset;
private Map<String,Map<String,Integer>> facets = new HashMap<String,Map<String,Integer>>();
@rkroll
rkroll / SearchResultRow.java
Created March 6, 2012 18:48
SerachResultRow
/**
*
* When returning a list of items in SearchResult, there may be meta data required per item. This class wraps each item
* so meta data can be attached. For example, when searching on vendors near a geoPoint we need to return each vendor's
* distance from the geoPoint. We would put the vendor and the distance in this object.
*
* @see SearchResult
* @param <T>
*/