-
Notifications
You must be signed in to change notification settings - Fork 0
/
APNode.java
executable file
·163 lines (157 loc) · 4.74 KB
/
APNode.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
package APCluster;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.LinkedList;
public class APNode {
class Incomplete{
int index; // 元组标号
ArrayList<Double> numData;
ArrayList<String> claData;
int miss;
public Incomplete(int index, ArrayList<Double> numData, ArrayList<String> claData, int miss) {
super();
this.index = index;
this.numData = numData;
this.claData = claData;
this.miss = miss;
}
@Override
public String toString() {
return "Incomplete [index=" + index + ", numData=" + numData + ", claData=" + claData + ", miss=" + miss
+ "]";
}
}
class Complete{
int index; // 元组标号
ArrayList<Double> numData;
ArrayList<String> claData;
public Complete(int index, ArrayList<Double> numData, ArrayList<String> claData) {
super();
this.index = index;
this.numData = numData;
this.claData = claData;
}
@Override
public String toString() {
return "Complete [index=" + index + ", numData=" + numData + ", claData=" + claData + "]";
}
}
int NN,CN;
LinkedList<Incomplete> incompleteData;
LinkedList<Complete> completeData;
ArrayList<ArrayList<Double>> similarity;
public APNode() {
super();
NN=CN=0;
similarity = new ArrayList<ArrayList<Double>>();
incompleteData = new LinkedList<Incomplete>();
completeData = new LinkedList<Complete>();
}
public void dataInputFromFile(String filePath,String split){
File file=new File(filePath);
if(!file.isFile() || !file.exists()){ //判断文件是否存在
System.out.println("找不到指定的文件");
return;
}
String encoding="UTF-8";
try(
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(//考虑到编码格式
new FileInputStream(file),encoding));
){
String linedata = bufferedReader.readLine();
String[] Mark = linedata.split(split);
for(String m:Mark){
if(m.equals("Num")){
NN++;
}else if(m.equals("Cla")){
CN++;
}
}
int count=1;
while((linedata = bufferedReader.readLine()) != null){
ArrayList<Double> numData = NN>0?new ArrayList<Double>():null;
ArrayList<String> claData = CN>0?new ArrayList<String>():null;
int miss=0;
String[] data = linedata.split(split);
for(int i=0; i<Mark.length; i++){
if(Mark[i].equals("Num")){
double value;
if(data[i].equals("?")){
miss++;
value = Double.NaN;
}else{
value = Double.parseDouble(data[i]);
}
numData.add(value);
}else if(Mark[i].equals("Cla")){
if(data[i].equals("?")) miss++;
claData.add(data[i]);
}
}
if(miss>0){
incompleteData.add(new Incomplete(count++,numData,claData,miss));
}else{
completeData.add(new Complete(count++,numData,claData));
}
}
incompleteData.sort((o1,o2)->(o1.miss-o2.miss));
if(NN>0) Tools.getMaxMin(this);
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
public void calculateSimlarity(){
int num = completeData.size();
for(int i=0; i<num; i++){
ArrayList<Double> s = new ArrayList<>();
for(int j=0; j<num; j++){
s.add(Tools.compareSimilarity(completeData.get(i), completeData.get(j)));
}
similarity.add(s);
}
}
public void updateSimilarity(Incomplete node){
int num = completeData.size();
ArrayList<Double> s = new ArrayList<>();
for(int i=0; i<num; i++){
double stmp = Tools.compareSimilarity(completeData.get(i), node);
similarity.get(i).add(stmp);
s.add(stmp);
}
s.add(new Double(0));
similarity.add(s);
}
public void transfer(){
while(!incompleteData.isEmpty()&&incompleteData.getFirst().miss==0){
Incomplete tmp = incompleteData.removeFirst();
updateSimilarity(tmp);
completeData.add(new Complete(tmp.index,tmp.numData, tmp.claData));
}
}
public void showData(){
for(Complete j:completeData){
System.out.println(j);
}
for(Incomplete i:incompleteData){
System.out.println(i);
}
}
public static void main(String[] args){
APNode test = new APNode();
test.dataInputFromFile("./data/winedata",",");
for(Complete j:test.completeData){
System.out.println(j);
}
for(Incomplete i:test.incompleteData){
System.out.println(i);
}
test.calculateSimlarity();
for(ArrayList<Double> m:test.similarity){
System.out.println(m);
}
}
}