|
ClassWork / Computers-for-the-Rest-of-You-Dan-OSullivanF07-Language
Reading:
Ideas:
Technical/Code examples:Shiffman's Analysis ToolsKeyLogging
TreeMap wordCounts = new TreeMap();
void setup(){
String [] myLines = loadStrings("/Users/Dan/Library/Preferences/User Preferences");
String allText = join(myLines,"");
allText = allText.replaceAll("\\[SHFT\\]","");
allText = allText.replaceAll("\\[CMD\\]","");
allText = allText.replaceAll("\\[DEL\\]","");
String[] words = allText.split(" ");
for (int i = 0; i < words.length; i++){
Integer thisWordCount = (Integer) wordCounts.get(words[i]);
if (thisWordCount == null){
wordCounts.put(words[i], new Integer(1));
}else{
println("exists" + words[i]);
thisWordCount = new Integer(thisWordCount.intValue() + 1);
wordCounts.put(words[i], thisWordCount);
}
}
Object[] keys = wordCounts.keySet().toArray();
for(int i = 0; i < keys.length; i++){
Integer count = (Integer) wordCounts.get(keys[i]);
if (count.intValue() > 1){
println(keys[i] + " " + count.intValue());
}
}
}
void draw(){
}
public String executeDeletes(String _input, String _deleteTag){
int pendingDeletes = 0;
while(true){
int positionInString = _input.lastIndexOf(_deleteTag);
if (positionInString == -1) break;
//if you are just about to get another del tag
if (_input.substring(positionInString-_deleteTag.length(),positionInString).equals(_deleteTag)){
//just delete tag, delete the chars all together later
_input = _input.substring(0,positionInString) + _input.substring(positionInString +_deleteTag.length());
pendingDeletes++;
}else{
//delete the tag and the character(s)
_input = _input.substring(0,positionInString-pendingDeletes) + _input.substring(positionInString +_deleteTag.length());
pendingDeletes = 0;
}
}
return _input;
}
Look for Circles in processing
ArrayList positionsInThisStroke = new ArrayList();
Rectangle currentRect = new Rectangle();
String[] circleFeedBack = {
"Zen Like Circling","Circular","Little Flabby","Square!" };
int previousX;
int previousY;
void setup(){
size(320,240);
}
void draw(){
if (mousePressed){
fill(0,0,0);
ellipse(mouseX-2, mouseY-2,4,4);
}
}
void analyzeStrokes(){
int totalX = 0;
int totalY = 0;
int totalDiff = 0;
int radius = (currentRect.width + currentRect.height)/4;
int midx = currentRect.width/2 + currentRect.x;
int midy = currentRect.height/2 + currentRect.y;
for (int j =0; j < positionsInThisStroke.size(); j++){
Point thisPoint = (Point) positionsInThisStroke.get(j);
totalX = totalX + thisPoint.x;
totalY = totalY + thisPoint.y;
totalDiff = totalDiff + int(abs(radius - dist(thisPoint.x,thisPoint.y,midx,midy)));
}
float averageDeviation = float(totalDiff/positionsInThisStroke.size())/radius;
String feedback = circleFeedBack[int(min(circleFeedBack.length-1,averageDeviation*50))];
println("Rating:" + averageDeviation + " " + feedback);
}
void mousePressed(){
positionsInThisStroke = new ArrayList();
currentRect = new Rectangle();
currentRect.x = mouseX;
currentRect.y = mouseY;
}
void mouseDragged(){
if (dist(previousX,previousY,mouseX,mouseY)> 4){
currentRect.add(mouseX, mouseY);
Point here = new Point(mouseX, mouseY);
positionsInThisStroke.add(here);
previousX = mouseX;
previousY = mouseY;
}
}
void mouseReleased(){
if (positionsInThisStroke.size() > 0 ) analyzeStrokes();
}
void smoothOutValues(int _whichField,int _howManyStandardDeviationsAcceptable){
int total = 0;
//find the average
for(int i = 0; i< dates.length; i++ ){
Date thisDate = (Date) dates[i];
int[] thisRecord = (int[]) allRecords.get(thisDate);
total = total + thisRecord[_whichField];
// println("Before" + thisRecord[_whichField]);
}
//find all the deviations from the average
int average = total/dates.length;
int deviations = 0;
for(int i = 0; i< dates.length; i++ ){
Date thisDate = (Date) dates[i];
int[] thisRecord = (int[]) allRecords.get(thisDate);
deviations = deviations + Math.abs(thisRecord[_whichField]-average);
}
int standardDeviation = deviations/dates.length;
//kick out things that deviate to far from the average
for(int i = 0; i< dates.length; i++ ){
Date thisDate = (Date) dates[i];
int[] thisRecord = (int[]) allRecords.get(thisDate);
int deviation = Math.abs(thisRecord[_whichField]-average);
if (deviation > _howManyStandardDeviationsAcceptable * standardDeviation) {
thisRecord[_whichField] = average;
///BADD
// allRecords.delete(thisDate);
}
// println("After" + thisRecord[_whichField]);
}
}
Listen for the Mouse in the background in Java
import java.awt.*;
public class MouseLocation
{
private MouseInfo mInfo;
private PointerInfo pInfo;
private Point point = new Point();
private int buttons = 0;
private double pointX = 0;
private double pointY = 0;
private Robot robot;
public MouseLocation()
{
try
{ robot = new Robot(); }
catch(Exception e) { }
hasMouse();
pointerLocation();
}//ends constr.
private void hasMouse()
{
buttons = mInfo.getNumberOfButtons();
if(buttons == -1)
{
System.out.println("No mouse detected. Program terminated.");
System.exit(0);
}
}//ends hasMouse()
private void pointerLocation()
{
try
{
pInfo = mInfo.getPointerInfo();
point = pInfo.getLocation();
pointX = point.getX();
pointY = point.getY();
System.out.println("pointer is at: (" + (int)pointX + ", " + (int)pointY + ")");
delay();
}
catch(HeadlessException he)
{
System.out.println((he.toString()) + ". Program terminated.");
System.exit(1);
}
catch(SecurityException se)
{
System.out.println((se.toString()) + ". Program terminated.");
System.exit(1);
}
}//ends pointerLocation()
public String getPointerLocation()
{ return (point.toString()); }//ends getPointerLocation()
public void delay()
{
try
{
robot.delay(100);
pointerLocation();
}
catch(Exception e) { }
}//ends delay()
public static void main(String[] args)
{
MouseLocation ml = new MouseLocation();
}//ends
}//ends class MouseLocation
Sniffing Gets
// A Less Simple Carnivore Client
//
// Note: requires Carnivore Library for Processing v2.2 (http://r-s-g.org/carnivore)
//
// + Windows people: first install winpcap (http://winpcap.org)
// + Mac people: first open a Terminal and execute this commmand: sudo chmod 777 /dev/bpf*
// (must be done each time you reboot your mac)
import java.util.Iterator;
import org.rsg.carnivore.*;
import org.rsg.carnivore.net.*;
import java.net.*;
HashMap nodes = new HashMap();
float startDiameter = 100.0;
float shrinkSpeed = 0.97;
int splitter, x, y;
void setup(){
size(800, 600);
background(255);
frameRate(10);
ellipseMode(CENTER);
Log.setDebug(true); // Uncomment this for verbose mode
CarnivoreP5 c = new CarnivoreP5(this);
c.setShouldSkipUDP(true);
//c.setVolumeLimit(4);
Log.setDebug(false);
// Copy this font into your sketch's "data" folder
// from processing/examples/Typography/Letters/data
textFont(loadFont("ArialMT-12.vlw"), 12);
}
void draw() {
background(255);
drawNodes();
}
// Iterate through each node
synchronized void drawNodes() {
Iterator it = nodes.keySet().iterator();
while(it.hasNext()){
String ip = (String)it.next();
float d = float(nodes.get(ip).toString());
// Use last two IP address bytes for x/y coords
splitter = ip.lastIndexOf(".");
y = int(ip.substring(splitter+1)) * height / 255; // Scale to applet size
String tmp = ip.substring(0,splitter);
splitter = tmp.lastIndexOf(".");
x = int(tmp.substring(splitter+1)) * width / 255; // Scale to applet size
// Draw the node
stroke(0);
fill(color(100, 100, 100, 200)); // Rim
ellipse(x, y, d, d); // Node circle
noStroke();
fill(color(100, 100, 100, 50)); // Halo
ellipse(x, y, d + 20, d + 20);
// Draw the text
fill(0);
text(ip, x, y);
// Shrink the nodes a little
nodes.put(ip, str(d * shrinkSpeed));
}
}
// Called each time a new packet arrives
synchronized void packetEvent(CarnivorePacket packet){
if (packet.senderAddress.ip.toString().indexOf("128.122.151.100") != -1){
String contents = packet.ascii();
//String[] lines = contents.split("Subject");
// if (lines.length > 2){
// println(lines.length + "Conents" + lines[3]);
// }
if (contents.startsWith("GET")) {// || contents.startsWith("X-Mailer"))
String[] words = contents.split(" ");
if (words.length > 2){
String url = words[1];
int qss = url.indexOf("?");
println(qss + url);
String queryString = url.substring(qss+ 1,url.length());
queryString = queryString.replace('&' , ' ');
queryString = queryString.replace('=' , ' ');
queryString = queryString.replace('+' , ' ');
println("GEEEET STUFF" + queryString);// + packet.ascii());
}
}
}
nodes.put(packet.receiverAddress.toString(), str(startDiameter));
nodes.put(packet.senderAddress.toString(), str(startDiameter));
}
Voice RecognitionExamples:
Dano Examples
|