import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class testFileCal {
String filename;
List<Double> bayes;
testFileCal(String f){
filename = f;
bayes = new ArrayList<Double>();
}
public void testArticleRead(List<String> data, List<Integer> frequency, List<String> data2, List<Integer> frequency2){
try{
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
String line;
String str;
line = br.readLine();
while(line != null){
StringTokenizer st = new StringTokenizer(line, " ");
ArrayList<String> articleWords = new ArrayList<String>();
while(st.hasMoreTokens()){
str = st.nextToken();
if(str.length() > 1 && str.charAt(0) != '.' && str.charAt(0) != ','&& str.charAt(0) != '!'&& str.charAt(0) != '"' && str.charAt(0) != ':' && str.charAt(0) != '-' && str.charAt(0) != ';'){
boolean addFlag = true;
for(int j=0; j<articleWords.size(); j++){
if(str.equals(articleWords.get(j))){
addFlag = false;
break;
}
}
if(addFlag) articleWords.add(str);
}
}
for(int i=0; i<articleWords.size(); i++) System.out.println(articleWords.get(i));
double bayesNumber = 0;
for(int j=0; j<articleWords.size(); j++){
int eNum = 0;
int pNum = 0;
for(int k=0; k<data.size(); k++){
if(articleWords.get(j).equals(data.get(k))) eNum = frequency.get(k);
}
for(int k=0; k<data2.size(); k++){
if(articleWords.get(j).equals(data2.get(k))) pNum = frequency2.get(k);
}
if (eNum == 0 || pNum == 0){ eNum++; pNum++; }
bayesNumber += Math.log((double)eNum / (double)pNum);
System.out.println(bayesNumber);
}
bayes.add(bayesNumber);
line = br.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void printBayes(){
System.out.println("bayes size : "+bayes.size());
try {
String fname = "D:/bayesResult.txt";
FileWriter fw = new FileWriter(fname);
String str = null;
for(int i=0; i < bayes.size(); i++){
str = bayes.get(i) + "\n";
System.out.println(str);
fw.write(str);
}
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String [] args){
FileAnalasys ec = new FileAnalasys("D:/economy_analysis.txt");
FileAnalasys po = new FileAnalasys("D:/politics_analysis.txt");
ec.loadAnalysisFile();
po.loadAnalysisFile();
//testFileCal testE = new testFileCal("D:/economy.txt");
testFileCal testP = new testFileCal("D:/politics.txt");
//testE.testArticleRead(ec.data, ec.frequency, po.data, po.frequency);
testP.testArticleRead(po.data, po.frequency, ec.data, ec.frequency);
testP.printBayes();
}
}