-
Notifications
You must be signed in to change notification settings - Fork 0
/
KNNCL1.java
173 lines (159 loc) · 5.18 KB
/
KNNCL1.java
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Queue;
import java.util.Set;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentSkipListMap;
public class KNNCL1 {
private ArrayList<String> reference;
private Map<ArrayList<Integer>,String> classifierMap;
private int k;
public KNNCL1(int k){
this.reference = new ArrayList<String>();
this.classifierMap = new Hashtable<ArrayList<Integer>,String>();
this.k = k;
}
public void train(Set<String> words, Collection<DocumentObject> docs){
Iterator<String> it = words.iterator();
String word;
Iterator<DocumentObject> docIt;
DocumentObject DOC;
ArrayList<Integer> point;
Queue<String> documentWords;
while(it.hasNext()){
word = it.next();
this.reference.add(word);
}
docIt = docs.iterator();
while(docIt.hasNext()){
DOC = docIt.next();
documentWords = DOC.getWords();
point = getPoint(documentWords);
this.classifierMap.put(point, DOC.getClassifier());
}
}
private ArrayList<Integer> getPoint(Queue<String> documentWords){
ArrayList<Integer> point = new ArrayList<Integer>();
for(int i = 0; i < this.reference.size(); i++){
if(documentWords.contains(this.reference.get(i))){
point.add(i, 1);//getNumberOfWords(documentWords,this.reference.get(i))
}else{point.add(i, 0);}
}
return point;
}
private static Integer getNumberOfWords(Queue<String> documentWords,String targetWord) {
Iterator<String> it = documentWords.iterator();
String word;
int count = 0;
while(it.hasNext()){
word = it.next();
if(word.equals(targetWord)){count++;}
}
return count;
}
public String classify(Queue<String> words){
String classifyGuess = "";
ArrayList<Integer> point = getPoint(words), point2;
Map<ArrayList<Integer>, Double> distance = new Hashtable<ArrayList<Integer>, Double>();
Iterator<ArrayList<Integer>> it= this.classifierMap.keySet().iterator();
while(it.hasNext()){
point2 = it.next();
distance.put(point2, calculateDistance(point, point2));
}
classifyGuess = getMostFreqClassifier(getClosestKNeighbourClassifiers(distance, this.k));
return classifyGuess;
}
public void changeK(int k){this.k=k;}
private static double calculateDistance(ArrayList<Integer> point, ArrayList<Integer> point2){
double distance = Double.MAX_VALUE;
int count = 0, difference;
for(int i = 0; i < point.size(); i++){
difference = point.get(i) - point2.get(i);
count += difference * difference;
}
distance = Math.sqrt((double)count);
return distance;
}
private Map<String, Integer> getClosestKNeighbourClassifiers(Map<ArrayList<Integer>, Double> distance, int k){
Map<String, Integer> neighbours = new TreeMap<String, Integer>();
ArrayList<Integer> holder;
String classifier;
int newVal;
for(int i = 0; i < k; i++){
holder = getClosestNeighbour(distance);
if(holder != null){
distance.remove(holder);
classifier = this.classifierMap.get(holder);
if(neighbours.containsKey(classifier)){
newVal = neighbours.remove(classifier)+1;
neighbours.put(classifier,newVal);
}else{
neighbours.put(classifier,1);
}
}//else{System.out.println("NULL: No neighbour");}
}
return neighbours;
}
private static ArrayList<Integer> getClosestNeighbour(Map<ArrayList<Integer>, Double> distance){
Iterator<ArrayList<Integer>> it = distance.keySet().iterator();
ArrayList<Integer> holder, currentClosest = null;
double currentClosestDist = Double.MAX_VALUE;
while(it.hasNext()){
holder = it.next();
if(distance.get(holder) < currentClosestDist){
currentClosestDist = distance.get(holder);
currentClosest = holder;
}
}
return currentClosest;
}
private static String getMostFreqClassifier(Map<String, Integer> classifierFrequency){
int highestFreq = 0;
String classifier, returnClassifier = null;
Iterator<String> it = classifierFrequency.keySet().iterator();
while(it.hasNext()){
classifier = it.next();
if(classifierFrequency.get(classifier) > highestFreq){
highestFreq = classifierFrequency.get(classifier);
returnClassifier = classifier;
}
}
return returnClassifier;
}
public void saveParamsToFile(){
PrintWriter writer;
try {
writer = new PrintWriter("./CSE5243HW2Cl1Params", "UTF-8");
for(int i = 0; i < this.reference.size(); i++){
writer.print(this.reference.get(i) + " ");
}
ArrayList<Integer> k;
Iterator<ArrayList<Integer>> it = this.classifierMap.keySet().iterator();
writer.println();
while(it.hasNext()){
k = it.next();
writer.print("[");
for(int i = 0; i < k.size(); i++){
writer.print(k.get(i) + " ");
}
writer.println("] >> " + this.classifierMap.get(k));
}
writer.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
System.out.println("ERROR: File not found");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
System.out.println("ERROR: Unsupported Encoding");
}
}
private void loadParamsFromFile(){}
}