psvm
Class SVM

java.lang.Object
  extended by psvm.SVM

public class SVM
extends Object

PSVM is a Processing library for machine learning using Support Vector Machines. It is based on LibSVM.

+Example
import psvm.*;

SVM model;

String trainingDocuments[] = {
    "FREE NATIONAL TREASURE",     // Spam
    "FREE TV for EVERY visitor",   // Spam
    "Peter and Stewie are hilarious", // OK
    "AS SEEN ON NATIONAL TV",      // SPAM
    "Buy Viagra Now!",          // SPAM
    "New episode rocks, Peter and Stewie are hilarious", // OK
    "Peter is my fav!",        // OK
    "Buy Viagra",
    //"LOVE Viagra Cialis TV!", // SPAM
    //"Free viagra for you."
};

int labels[] = {
  1, 1, 0, 1, 1, 0, 0//, 1, 1
};

String testDocuments[] = {
    "FREE lotterry for the NATIONAL TREASURE!!!", // Spam
    "Stewie is hilarious",     // OK
    "Poor Peter...nhilarious",    // OK
    "I love this show",
    "Free gold just click HERE.",
    "Best episode ever",
    "Buy Viagra?"
};

ArrayList globalDictionary;

void buildGlobalDictionary() {
  globalDictionary = new ArrayList();
  for (int i = 0; i < trainingDocuments.length; i++) {
    String doc = trainingDocuments[i];
    String words[] = split(doc, ' ');
    for (int w = 0; w < words.length; w++) {
      String word = words[w].toLowerCase();
      word = word.replaceAll("\\W", "");
      if (!globalDictionary.contains(word)) {
        globalDictionary.add(word);
      }
    }
  }

  for (int i = 0; i < testDocuments.length; i++) {
    String doc = testDocuments[i];
    String words[] = split(doc, ' ');
    for (int w = 0; w < words.length; w++) {
      String word = words[w].toLowerCase();
      word = word.replaceAll("\\W", "");
      if (!globalDictionary.contains(word)) {
        globalDictionary.add(word);
      }
    }
  }

  println(globalDictionary);
}

int[] buildVector(String input) {
  String[] words = split(input, ' ');
  ArrayList normalizedWords = new ArrayList();
  for (int w = 0; w < words.length; w++) {
    words[w] = words[w].replaceAll("\\W", "");
    normalizedWords.add(words[w].toLowerCase());
  }

  int[] result = new int[globalDictionary.size()];
  for (int i = 0; i < globalDictionary.size(); i++) {
    String word = globalDictionary.get(i);
    if (normalizedWords.contains(word)) {
      result[i] = 1;
    } 
    else {
      result[i] = 0;
    }
  }
  return result;
}

void setup() {
  size(500,500);
  buildGlobalDictionary();

  int[][] trainingVectors = new int[trainingDocuments.length][1];
  for(int i = 0; i < trainingDocuments.length; i++){
    trainingVectors[i] = buildVector(trainingDocuments[i]);
  }

  model = new SVM(this);
  SVMProblem problem = new SVMProblem();
  problem.setNumFeatures(1);
  problem.setSampleData(labels, trainingVectors);
  model.train(problem);
  
  int[][] testVectors = new int[testDocuments.length][1];
  for(int i = 0; i < testDocuments.length; i++){
    testVectors[i] = buildVector(testDocuments[i]);
  }
  
  for(int i = 0; i < testDocuments.length; i++){
    println("testing: " + testDocuments[i] );
    double score = model.test(testVectors[i]); 
    println("result: " + score);
  } 
}

void draw() {
background(0);
for (int i =0; i < testDocuments.length; i++) {
    double score = model.test(buildVector(testDocuments[i]));
    if(score == 0){
      fill(0,255,0);
    } else {
      fill(255,0,0);
    }
    text(testDocuments[i] + " [score: "+score+"]", 10, 20*i + 20);
  }
}

void saveGlobalDictionary(){
  String[] strings = new String[globalDictionary.size()];
  strings = globalDictionary.toArray(strings);
  saveStrings(dataPath("dictionary.txt"), strings);
}

void keyPressed(){
  saveGlobalDictionary();
  model.saveModel("model.txt");
}

Field Summary
static int C_SVC
           
static int LINEAR_KERNEL
           
 svm_model model
           
static int NU_SVC
           
 svm_parameter params
           
static int POLY_KERNEL
           
static int RBF_KERNEL
           
static int SIGMOID_KERNEL
           
 SVMProblem svmProblem
           
static String VERSION
           
 
Constructor Summary
SVM(PApplet parent)
           
 
Method Summary
 void loadModel(String filename, int numFeatures)
           
 void saveModel(String filename)
           
 void setKernelParameters(int svmType, int kernelType, int degree, float gamma, int c)
           
 double test(double[] testVector)
           
 double test(float[] testVector)
           
 double test(int[] testVector)
           
 void train(SVMProblem problem)
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VERSION

public static final String VERSION
See Also:
Constant Field Values

LINEAR_KERNEL

public static final int LINEAR_KERNEL

RBF_KERNEL

public static final int RBF_KERNEL

POLY_KERNEL

public static final int POLY_KERNEL

SIGMOID_KERNEL

public static final int SIGMOID_KERNEL

C_SVC

public static final int C_SVC

NU_SVC

public static final int NU_SVC

svmProblem

public SVMProblem svmProblem

params

public svm_parameter params

model

public svm_model model
Constructor Detail

SVM

public SVM(PApplet parent)
Method Detail

setKernelParameters

public void setKernelParameters(int svmType,
                                int kernelType,
                                int degree,
                                float gamma,
                                int c)

loadModel

public void loadModel(String filename,
                      int numFeatures)

saveModel

public void saveModel(String filename)

train

public void train(SVMProblem problem)

test

public double test(double[] testVector)

test

public double test(float[] testVector)

test

public double test(int[] testVector)


Processing library PSVM by Greg Borenstein. (C) 2012