Java tutorial
/* * ============================================================ * The SSE USTC Software License * * InputController.java * 2014-3-3 * * Copyright (c) 2006 China Payment and Remittance Service Co.,Ltd * All rights reserved. * ============================================================ */ package ustc.sse.controller; import java.io.UnsupportedEncodingException; import java.math.BigDecimal; import java.util.Collection; import java.util.HashMap; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import ustc.sse.datamining.algo.ChineseTokenizer; import ustc.sse.datamining.algo.DefaultStopWordsHandler; import ustc.sse.datamining.algo.MultinomialModelNaiveBayes; import ustc.sse.datamining.algo.TrainSampleDataManager; import ustc.sse.datamining.algo.kmeans.KMean; import ustc.sse.model.Classifer; import ustc.sse.rjava.RJavaInterface; /** * ? * <p> * date author email notes<br /> * -------- --------------------------- ---------------<br /> *2014-3-3 starqiu@mail.ustc.edu.cn <br /></p> * */ @Controller public class DataMiningController { private Log log = LogFactory.getLog(DataMiningController.class); public static HashMap<String, String> docClass = new HashMap<String, String>(); //public static final String DEFAULT_DIR=System.getProperty("user.dir")+"../RDMP1/WEB-INF/classess/java/ustc/sse/datamining/algo/ik/"; public static final String DEFAULT_DIR = TrainSampleDataManager.class.getResource("").getPath() + "/ik/"; static { } public DataMiningController() { super(); } /** * ? ??+ * @param request * @param response * @return */ @RequestMapping("dataMining") public String dataMining(HttpServletRequest request, HttpServletResponse response) { if (!RJavaInterface.getRengine().waitForR()) { log.error("Can not load R!"); } //? String cmd = "source('/home/starqiu/workspace/RDMP1/src/main/java/ustc/sse/r/tbmr.R',echo=TRUE)"; String rv = ""; try { rv = RJavaInterface.getRengine().eval(cmd).asString(); log.info("data mining succeed!"); log.info("the result of source tbmr.R is:" + rv); } catch (Exception e) { log.error("data mining failed!"); } // cmd = "source('/home/starqiu/workspace/RDMP1/src/main/java/ustc/sse/r/draw.R',echo=TRUE)"; try { rv = RJavaInterface.getRengine().eval(cmd).asString(); log.info("data visual succeed!"); log.info("the result of source draw.R is:" + rv); } catch (Exception e) { log.error("data visual failed!"); } RJavaInterface.getRengine().end(); return "taskDetail"; } /** * ? * @param request * @param response * @return */ @RequestMapping("autoDocumentInput") public String autoDocumentInput(HttpServletRequest request, HttpServletResponse response) { return "autoDocumentInput"; } /** * * @param request * @param response * @return * @throws UnsupportedEncodingException */ @RequestMapping("autoDocument") public String autoDocument(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException { TrainSampleDataManager.process(); String article = new String((request.getParameter("article")).getBytes("ISO-8859-1"), "UTF-8"); //"????????29???????????29??????????"; //String s="????????????Foursquare?????"; //String s="???????75%80????"; //String s=" ????????CEOCEO????"; //String s="???44621? ?????446?????3113119.1862%????20062007???????????????(?)"; //String s="?Uber2.58????Uber???"; //String s="? ??"; Set<String> words = ChineseTokenizer.segStr(article).keySet(); Map<String, BigDecimal> resultMap = MultinomialModelNaiveBayes .classifyResult(DefaultStopWordsHandler.dropStopWords(words)); Set<String> set = resultMap.keySet(); docClass.put("C000007", ""); docClass.put("C000008", "?"); docClass.put("C000010", "IT"); docClass.put("C000013", "?"); docClass.put("C000014", ""); docClass.put("C000016", ""); docClass.put("C000020", ""); docClass.put("C000022", "?"); docClass.put("C000023", ""); docClass.put("C000024", ""); List<Classifer> classifers = new LinkedList<Classifer>(); BigDecimal total = new BigDecimal(0); BigDecimal probability = new BigDecimal(0); for (String str : set) { probability = resultMap.get(str); total = total.add(probability); log.info("classifer:" + str + " probability:" + probability); Classifer classifer = new Classifer(docClass.get(str), probability); classifers.add(classifer); } // for (Classifer classifer : classifers) { classifer.setProbability((classifer.getProbability()).divide(total, 2, BigDecimal.ROUND_HALF_EVEN)); } request.setAttribute("classifers", classifers); String classifyName = docClass.get(MultinomialModelNaiveBayes.getClassifyResultName()); request.setAttribute("classifyName", classifyName); log.info("The final result:" + classifyName); return "autoDocument"; } /** * K-Means ??? * @param request * @param response * @return */ @RequestMapping("kMeansInput") public String kMeansInput(HttpServletRequest request, HttpServletResponse response) { return "kMeansInput"; } /** * K-Means ?? * @param request * @param response * @return * @throws UnsupportedEncodingException */ @RequestMapping("kMeans") public String kMeans(HttpServletRequest request, HttpServletResponse response) throws UnsupportedEncodingException { String[] args = new String[4]; args[0] = "-k"; args[1] = request.getParameter("k"); args[2] = "-n"; args[3] = request.getParameter("numOfNode"); KMean.kmeans(args); return "kMeans"; } }