// a wrapper for LIBLINEAR public class Classifier { private HashMap featureMap; private Model[] models; private HashMap cache; private BufferedWriter cacheWriter; public Classifier(String installDir, String[] emotions) { models = new Model[emotions.length]; cache = new HashMap(); try { ArrayList lines = new ArrayList(); // load feature index map InputStream instream; if (installDir.startsWith("http:")) { instream = new URL(installDir + "/data/features.txt").openStream(); } else { // instream = new FileInputStream(new File(installDir + "/data/features-short.txt")); instream = new FileInputStream(new File(installDir + "/data/features.txt")); } BufferedReader reader = new BufferedReader(new InputStreamReader(instream)); String inLine; while ((inLine = reader.readLine()) != null) { lines.add(inLine); } reader.close(); featureMap = new HashMap(); for (String line : lines) { String[] f = line.split("\\t"); featureMap.put(f[1].trim(), new Integer(f[0].trim())); } // load classifier models for (int i=0; i 0) && cache.containsKey(sentence)) { Integer iCached = cache.get(sentence); for (int i=0; i 0) { for (int i=0; i 0) { cache.put(sentence,iClass); } if (cacheWriter != null) { try { cacheWriter.write(iClass + "\t" + sentence + "\n"); cacheWriter.flush(); } catch (IOException e) { e.printStackTrace(); System.exit(0); } } } private FeatureNode[] getFeatures(String s) { s = s.toLowerCase().replaceAll("(\\w)([.,;:!?'\"])","$1 $2"); String[] f = s.split("\\s+"); // println("feature vector " + Arrays.toString(f)); HashMap featMap = new HashMap(); for (String feat : f) { Integer i = featureMap.get(feat); if (i != null) { if (!featMap.containsKey(i)) { featMap.put(i, new Double(0.0)); } featMap.put(i, new Double(featMap.get(i) + 1.0)); } } // normalize, sort double sum = 0.0; for (Integer i : featMap.keySet()) { sum += featMap.get(i); } ArrayList indices = new ArrayList(featMap.keySet()); Collections.sort(indices); FeatureNode[] fn = new FeatureNode[indices.size()]; for (int i = 0; i < fn.length; i++) { fn[i] = new FeatureNode(indices.get(i), featMap.get(indices.get(i)) / sum); // System.out.println("fn " + i + "=" + fn[i]); } return fn; } }