/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package keypoh;

import java.util.*;
import static java.util.Arrays.asList;
import StringMatching.*;

/**
 * Kaypoh
 * @author ical
 */
public class Keypoh {
    private static List<String> topics = asList("topic1", "topic2", "topic3");
    private static List< List<String> > topicCategories = asList(
        asList("categ11", "categ12", "categ13"),
        asList("categ21", "categ22", "categ23"),
        asList("categ31", "categ32", "categ33")
    );
    private static List<String> categories;
    private static Map<String, List<String> > searchResult = new HashMap();
        
    /**
     * Driver
     * arguments : api_used matching_method topic tags keywords
     * @param args String[]
     * @throws java.lang.Exception
     */
    public static void main(String[] args) throws Exception {
        List<String> contents;
        StringProcessor st;
        
        /* Get contents from API request */
        if (args[0].equals("tw")) {
            twitter4j.conf.ConfigurationBuilder config;
            config = new twitter4j.conf.ConfigurationBuilder();
            contents = new TwitterCrawler(config).Call(args);
        } else {
            facebook4j.conf.ConfigurationBuilder config;
            config = new facebook4j.conf.ConfigurationBuilder();
            contents = new FacebookCrawler(config).Call(args);
        }
        
        /* Set matching method */
        if (args[1].equals("bm")) {
            st = new BoyerMoore();
        } else {
            st = new KnuthMorrisPratt();
        }
        
        /* Set categories from given topic */
        for (int i = 0; i < topics.size(); ++i) {
            if (topics.get(i).equals(args[2])) {
                categories = topicCategories.get(i);
                break;
            }
        }
        
        /* Categorize each content */
        for (String content : contents) {
            boolean categorized = false;
                
            for (int i = 4; i < args.length && !categorized; ++i) {
                List<String> keywords = parse(args[i], ",");
                
                for (String keyword : keywords) {
                    st.setPattern(keyword);
                    if (st.search(content)) {
                        List<String> list = searchResult.get(categories.get(i - 4));
                        if (list == null) {
                            list = new LinkedList<>();
                        }
                        list.add(content);
                        searchResult.put(keyword, list);
                        break;
                    }
                }
            }
            
            /* unknown category */
            if (!categorized) {
                List<String> list = searchResult.get("unknown");
                if (list == null) {
                    list = new LinkedList<>();
                }
                list.add(content);
                searchResult.put("unknown", list);
                break;
            }
        }
        
        /* Print result */
        for (String category : categories) {
            List<String> results = searchResult.get(category);
            
            System.out.println("Kategori " + category + ":");
            for (String result : results) {
                System.out.println(result);
            }
            System.out.println();
        }
    }
    
    /**
     * Function parse
     * Parsing string to string list
     * 
     * @param s
     * @param delim
     * @return List<String>
     */
    private static List<String> parse(String s, String delim) {
        List<String> l = new LinkedList<>();
        for (String ss : s.split(delim)) {
            if (ss != null) {
                l.add(ss);
            }
        }
        return l;
    }
    
    /**
     * String QueryGenerator
     * Adjust multi queries to API call argument
     * 
     * @param key
     * @param param
     * @return String
     */
    public static String QueryGenerator(String key, int param) {
        String q = "";
        for (String s : parse(key, ",")) {
            if (s != null) {
                if (!q.equals("")) {
                    switch (param) {
                        case 0 : q += " OR "; break;
                        case 1 : q += "+"; break;
                    }
                }
                q += s;
            }
        }
        return q;        
    }
}

@@ -2772,32 +2772,103 @@
 eyword, list);%0D%0A
+                        %0D%0A                        categorized = true;%0D%0A
                 
@@ -3554,14 +3554,83 @@
  + %22
+ 
 :%22);%0D%0A
+            System.out.println(%22Banyak post : %22 + results.size());%0D%0A
     
@@ -3761,24 +3761,24 @@
 println();%0D%0A
-
         %7D%0D%0A 
@@ -3772,24 +3772,379 @@
 %0A        %7D%0D%0A
+        %0D%0A        /* Khusus untuk kategori unknown */%0D%0A        System.out.println(%22Kategori unknown :%22);%0D%0A        List%3CString%3E results = searchResult.get(%22unknown%22);%0D%0A        System.out.println(%22Banyak post : %22 + results.size());%0D%0A        for (String result : results) %7B%0D%0A            System.out.println(result);%0D%0A        %7D%0D%0A        System.out.println();%0D%0A
     %7D%0D%0A    %0D

fix tinggal jsp