package awesomefb;

import com.aliasi.classify.Classification;
import com.aliasi.classify.Classified;
import com.aliasi.classify.DynamicLMClassifier;
import com.aliasi.lm.NGramProcessLM;
import com.aliasi.util.Files;

import java.io.*;

/**
 * Created by earl on 5/30/2015.
 */
public class SentimentClassifier {
    File mPolarityDir;
    String[] mCategories;
    DynamicLMClassifier<NGramProcessLM> mClassifier;

    public SentimentClassifier() {
        mPolarityDir = new File("train_datasets");
        mCategories = mPolarityDir.list();
        int nGram = 8;
        mClassifier = DynamicLMClassifier.createNGramProcess(mCategories, nGram);
    }

    void train() {
        for (int i = 0; i < mCategories.length; ++i) {
            String category = mCategories[i];
            Classification classification = new Classification(category);
            File dir = new File(mPolarityDir, mCategories[i]);
            File[] trainFiles = dir.listFiles();
            for (int j = 0; j < trainFiles.length; ++j) {
                File trainFile = trainFiles[j];
                if (isTrainingFile(trainFile)) {
                    try {
                        String review = Files.readFromFile(trainFile, "ISO-8859-1");
                        Classified<CharSequence> classified = new Classified<CharSequence>(review, classification);
                        mClassifier.handle(classified);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public String classify(String txt) {
        Classification classification = mClassifier.classify(txt);
        String resultCategory = classification.bestCategory();
        return resultCategory;
    }

    boolean isTrainingFile(File file) {
        return file.getName().charAt(2) != '9';  // test on fold 9
    }
}

@@ -561,16 +561,55 @@
 am = 8;%0A
+        if (mCategories != null) %7B%0A    
         
@@ -686,34 +686,164 @@
 m);%0A
+    
     %7D
-%0A%0A    void train() %7B%0A
+ else %7B%0A            System.out.println(%22Train data not found.%22);%0A        %7D%0A    %7D%0A%0A    public void train() %7B%0A        if (mCategories != null) %7B%0A    
     
@@ -901,24 +901,28 @@
             
+   
+ 
 String categ
@@ -943,16 +943,20 @@
 ies%5Bi%5D;%0A
+    
         
@@ -1029,24 +1029,28 @@
             
+ 
+   
 File dir = n
@@ -1096,24 +1096,28 @@
             
+  
+  
 File%5B%5D train
@@ -1137,24 +1137,28 @@
 istFiles();%0A
+    
             
@@ -1215,24 +1215,28 @@
             
+    
 File trainFi
@@ -1267,24 +1267,28 @@
             
+ 
+   
 if (isTraini
@@ -1332,14 +1332,22 @@
     
+    
 try %7B%0A
+    
     
@@ -1443,32 +1443,36 @@
                 
+    
 Classified%3CCharS
@@ -1563,32 +1563,36 @@
                 
+    
 mClassifier.hand
@@ -1627,16 +1627,20 @@
         
+  
+  
 %7D catch 
@@ -1681,16 +1681,20 @@
         
+    
 e.printS
@@ -1702,24 +1702,50 @@
 ackTrace();%0A
+                        %7D%0A
             
@@ -1840,24 +1840,102 @@
 ring txt) %7B%0A
+        String resultCategory = %22pos%22;%0A        if (mCategories != null) %7B%0A    
         Clas
@@ -1989,38 +1989,35 @@
 y(txt);%0A        
-String
+   
  resultCategory 
@@ -2045,24 +2045,34 @@
 Category();%0A
+        %7D%0A
         retu

Handle data not found