-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassifier.java
More file actions
154 lines (132 loc) · 4.53 KB
/
Copy pathClassifier.java
File metadata and controls
154 lines (132 loc) · 4.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/**
* Created with IntelliJ IDEA.
* User: assaad.moawad
* Date: 03/07/13
* Time: 17:11
* To change this template use File | Settings | File Templates.
*/
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Classifier {
private List<String> database;
private List<String[]> categories;
public Classifier(List<String> database, List<String> categories )
{
this.database=database;
this.categories=convertToLists(categories);
}
private List<String[]> convertToLists(List<String> categoryFile)
{
List<String[]> temp = new ArrayList<String[]>();
Iterator iter = categoryFile.iterator();
String s;
while (iter.hasNext())
{
s=(String) iter.next();
s=s.toLowerCase().trim();
temp.add(s.split(","));
}
return temp;
}
private boolean exist(String line, String[] category)
{
String s;
for(int j=0;j<category.length;j++)
{
s= category[j].trim();
if(s.length()>1)
if(line.contains(s))
return true;
}
return false;
}
public void analyze()
{
File statfile=new File("stats.txt");
File matrixfile=new File("matrix.txt");
File noncategoryfile=new File("uncategorized.txt");
try {
if(!statfile.exists())
statfile.createNewFile();
if(!matrixfile.exists())
matrixfile.createNewFile();
if(!noncategoryfile.exists())
noncategoryfile.createNewFile();
BufferedWriter statout = new BufferedWriter(new FileWriter(statfile,false));
BufferedWriter matrixout = new BufferedWriter(new FileWriter(matrixfile,false));
BufferedWriter noncategoryout = new BufferedWriter(new FileWriter(noncategoryfile,false));
int[] results=new int[categories.size()+1];
String boolR;
boolean temp;
boolean tempCat;
int linecounter=0;
String line;
String[] cat;
int catIndex;
Iterator iterLine = database.iterator();
Iterator iterCat;
while(iterLine.hasNext())
{
catIndex=0;
line=(String) iterLine.next();
String line2=line.toLowerCase().trim();
//String[] words = line2.split("\\W");
boolR="" ;
temp=false;
iterCat=categories.iterator();
while(iterCat.hasNext())
{
cat=(String[])iterCat.next();
tempCat=exist(line2,cat);
temp=temp||tempCat;
if(tempCat)
{
results[catIndex]++;
boolR+= "1,";
}
else
{
boolR+= "0,";
}
catIndex++;
}
matrixout.write(boolR);
matrixout.newLine();
if(temp==false)
{
results[categories.size()]++;
noncategoryout.write(line);
noncategoryout.newLine();
}
linecounter++;
}
System.out.println("Categorized: "+(linecounter- results[categories.size()])+" / " +linecounter);
System.out.println("-------------------------------------------------------------");
System.out.println();
String print;
for(int i=0; i< categories.size();i++)
{
print = "Cat "+i+" - "+ categories.get(i)[0]+ "... : "+ results[i];
System.out.println(print);
statout.write(print);
statout.newLine();
}
print = "Uncategorized : "+ results[categories.size()];
System.out.println(print);
System.out.println("Press any enter to exit.");
statout.write(print);
statout.newLine();
matrixout.close();
statout.close();
noncategoryout.close();
} catch (Exception ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}