Skip to content

Instantly share code, notes, and snippets.

Avatar

Phillip Johnson phillipjohnson

View GitHub Profile
@phillipjohnson
phillipjohnson / markov.sc
Created Jun 1, 2016
Markov-Chain Text Generation in Scala
View markov.sc
import scala.collection.mutable
val MARKOV_MAP:mutable.Map[Seq[String], mutable.Map[String, Int]] = new mutable.HashMap()
val CHAIN_SIZE = 2
def adjustProbabilities(sentence:String):Unit = {
val segments = sentence.split(" ").+:("").:+("").sliding(CHAIN_SIZE + 1).toList
for(segment <- segments) {
val key = segment.take(CHAIN_SIZE)
val probs = MARKOV_MAP.getOrElse(key, scala.collection.mutable.Map())
@phillipjohnson
phillipjohnson / b64.scala
Created Jul 6, 2014
Scala base64 encryption with alternative encodings
View b64.scala
val b64_ZH = "的一是不了人我在有他这中大来上国个到说们为子和你地出道也时年得" +
"就那要下以生会自着去之过家学对可她里后小么心多天而能好都然没日。"
val b64_EN = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
val input = "Man is distinguished, not only by his reason, but by this singular " +
"passion from other animals, which is a lust of the mind, that by a " +
"perseverance of delight in the continued and indefatigable generation " +
"of knowledge, exceeds the short vehemence of any carnal pleasure."
def strToAscii(s: String) : List[Int] = {
val ascii = for {
@phillipjohnson
phillipjohnson / LICENSE.txt
Last active Dec 21, 2016
Simple steganography in Python
View LICENSE.txt
The MIT License (MIT)
Copyright (c) 2014 Phillip Johnson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
@phillipjohnson
phillipjohnson / NaiveHashTable.java
Created Mar 16, 2014
Naive implementation of a hash table.
View NaiveHashTable.java
import java.util.Arrays;
/**
* Author: Phillip Johnson
* Date: 3/15/14.
*/
public class NaiveHashTable {
private Object[][] hashTable;
private static final int MAX_EQUAL_HASHES = 8;
@phillipjohnson
phillipjohnson / yelp_api_intro.py
Created Feb 8, 2014
A basic introduction to using the rauth library and the Yelp API.
View yelp_api_intro.py
import rauth
import time
def main():
locations = [(39.98,-82.98),(42.24,-83.61),(41.33,-89.13)]
api_calls = []
for lat,long in locations:
params = get_search_parameters(lat,long)
api_calls.append(get_results(params))
#Be a good internet citizen and rate-limit yourself
@phillipjohnson
phillipjohnson / rgb_img_cluster.R
Last active Dec 29, 2015
Cluster RGB data in R.
View rgb_img_cluster.R
rgbdata<-read.table("data_all.txt",header=F,sep="\t")
names(rgbdata)<-c("r","b","g","count")
km<-kmeans(rgbdata[c("r","b","g")],32,iter.max=500)
rgbdata$cluster_32<-km$cluster
rgbsamp<-rgbdata[sample(nrow(rgbdata), 5000), ]
library(fpc)
plotcluster(rgbsamp[c("r","b","g")], rgbsamp$cluster_32,col=rgb(rgbsamp$r/255,rgbsamp$g/255,rgbsamp$b/255),pch=19)
library(plyr)
rgb_means<-ddply(rgbdata,~cluster_32,summarise,r=mean(r),g=mean(g),b=mean(b),count=sum(count))
rgb_means$r<-round(rgb_means$r)
@phillipjohnson
phillipjohnson / rbg_img_process.py
Last active Dec 29, 2015
Quick Python script to process image RGB data.
View rbg_img_process.py
import Image
from os import listdir
rgb_counts = {}
def main():
process()
write_results()
def process():