Describe 데블스캠프2011/둘째날/Machine-Learning/NaiveBayesClassifier/김동준 here {{{ import java.io.*; import java.util.*; public class Native { public static void main(String[] args) { int economyNum = 0; int politicNum = 0; Map economyWord = new HashMap(); Map politicWord = new HashMap(); BufferedReader economyLearn; BufferedReader politicLearn; try { economyLearn = new BufferedReader(new FileReader(new File("C:/Users/KDJ/Documents/Devils/svm_data.tar/package/train/economy/index.economy.db"))); politicLearn = new BufferedReader(new FileReader(new File("C:/Users/KDJ/Documents/Devils/svm_data.tar/package/train/politics/index.politics.db"))); String tmp = economyLearn.readLine(); while(tmp != null) { String[] a = tmp.split(" "); for(String wordTmp:a) { if(wordTmp.length() == 1) {continue; } if( economyWord.get(wordTmp) == null) { economyNum++; economyWord.put(wordTmp, 1); } else { economyWord.put(wordTmp, economyWord.get(wordTmp)+1); } } tmp = economyLearn.readLine(); } tmp = politicLearn.readLine(); while(tmp != null) { String[] a = tmp.split(" "); for(String wordTmp:a) { if(wordTmp.length() == 1) {continue; } if (politicWord.get(wordTmp) == null ) { politicNum++; politicWord.put(wordTmp, 1); } else { politicWord.put(wordTmp, politicWord.get(wordTmp)+1); } } tmp = politicLearn.readLine(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { double ecoResultNum = 0; double polResultNum = 0; double posiNum = 0; double negaNum = 0; double reslt = Math.log((double)economyNum/politicNum); BufferedReader targetDocument = new BufferedReader(new FileReader(new File("C:/Users/KDJ/Documents/Devils/svm_data.tar/package/test/economy/economy.txt"))); String tmp = targetDocument.readLine(); while(tmp != null) { String[] str = tmp.split(" "); for(String tmpStr:str) { if(economyWord.get(tmpStr) == null) { ecoResultNum = 0; } else { ecoResultNum = economyWord.get(tmpStr); } if(politicWord.get(tmpStr) == null) { polResultNum = 0; } else { polResultNum = politicWord.get(tmpStr); } polResultNum+=1; ecoResultNum+=1; reslt += Math.log(ecoResultNum / polResultNum); } if(reslt < 0) { negaNum+= 1; } else { posiNum += 1; } reslt = 0; tmp = targetDocument.readLine(); } double accu = (posiNum / (posiNum+negaNum)); targetDocument.close(); posiNum = 0; negaNum = 0; polResultNum = 0; ecoResultNum = 0; reslt = Math.log((double)economyNum/politicNum); targetDocument = new BufferedReader(new FileReader(new File("C:/Users/KDJ/Documents/Devils/svm_data.tar/package/test/politics/politics.txt"))); tmp = targetDocument.readLine(); while(tmp != null) { String[] str = tmp.split(" "); for(String tmpStr:str) { if(economyWord.get(tmpStr) == null) { ecoResultNum = 0; } else { ecoResultNum = economyWord.get(tmpStr); } if(politicWord.get(tmpStr) == null) { polResultNum = 0; } else { polResultNum = politicWord.get(tmpStr); } polResultNum+=1; ecoResultNum+=1; reslt += Math.log(polResultNum / ecoResultNum); } if(reslt < 0) { negaNum+= 1; } else { posiNum += 1; } reslt = 0; tmp = targetDocument.readLine(); } double accu2 = ((double)posiNum / (posiNum+negaNum)); System.out.println((accu + accu2) / 2); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }}}