forked from linyiqun/DataMiningAlgorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnowledgeSystem.java
More file actions
195 lines (164 loc) · 3.79 KB
/
Copy pathKnowledgeSystem.java
File metadata and controls
195 lines (164 loc) · 3.79 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package DataMining_RoughSets;
import java.util.ArrayList;
import java.util.HashMap;
/**
* 知识系统
*
* @author lyq
*
*/
public class KnowledgeSystem {
// 知识系统内的集合
ArrayList<RecordCollection> ksCollections;
public KnowledgeSystem(ArrayList<RecordCollection> ksCollections) {
this.ksCollections = ksCollections;
}
/**
* 获取集合的上近似集合
*
* @param rc
* 原始集合
* @return
*/
public RecordCollection getUpSimilarRC(RecordCollection rc) {
RecordCollection resultRc = null;
ArrayList<String> nameArray;
ArrayList<String> targetArray;
ArrayList<RecordCollection> copyRcs = new ArrayList<>();
ArrayList<RecordCollection> deleteRcs = new ArrayList<>();
targetArray = rc.getRecordNames();
// 做一个集合拷贝
for (RecordCollection recordCollection : ksCollections) {
copyRcs.add(recordCollection);
}
for (RecordCollection recordCollection : copyRcs) {
nameArray = recordCollection.getRecordNames();
if (strIsContained(targetArray, nameArray)) {
removeOverLaped(targetArray, nameArray);
deleteRcs.add(recordCollection);
if (resultRc == null) {
resultRc = recordCollection;
} else {
// 进行并运算
resultRc = resultRc.unionCal(recordCollection);
}
if (targetArray.size() == 0) {
break;
}
}
}
//去除已经添加过的集合
copyRcs.removeAll(deleteRcs);
if (targetArray.size() > 0) {
// 说明已经完全还未找全上近似的集合
for (RecordCollection recordCollection : copyRcs) {
nameArray = recordCollection.getRecordNames();
if (strHasOverlap(targetArray, nameArray)) {
removeOverLaped(targetArray, nameArray);
if (resultRc == null) {
resultRc = recordCollection;
} else {
// 进行并运算
resultRc = resultRc.unionCal(recordCollection);
}
if (targetArray.size() == 0) {
break;
}
}
}
}
return resultRc;
}
/**
* 获取集合的下近似集合
*
* @param rc
* 原始集合
* @return
*/
public RecordCollection getDownSimilarRC(RecordCollection rc) {
RecordCollection resultRc = null;
ArrayList<String> nameArray;
ArrayList<String> targetArray;
targetArray = rc.getRecordNames();
for (RecordCollection recordCollection : ksCollections) {
nameArray = recordCollection.getRecordNames();
if (strIsContained(targetArray, nameArray)) {
removeOverLaped(targetArray, nameArray);
if (resultRc == null) {
resultRc = recordCollection;
} else {
// 进行并运算
resultRc = resultRc.unionCal(recordCollection);
}
if (targetArray.size() == 0) {
break;
}
}
}
return resultRc;
}
/**
* 判断2个字符数组之间是否有交集
*
* @param str1
* 字符列表1
* @param str2
* 字符列表2
* @return
*/
public boolean strHasOverlap(ArrayList<String> str1, ArrayList<String> str2) {
boolean hasOverlap = false;
for (String s1 : str1) {
for (String s2 : str2) {
if (s1.equals(s2)) {
hasOverlap = true;
break;
}
}
if (hasOverlap) {
break;
}
}
return hasOverlap;
}
/**
* 判断字符集str2是否完全包含于str1中
*
* @param str1
* @param str2
* @return
*/
public boolean strIsContained(ArrayList<String> str1, ArrayList<String> str2) {
boolean isContained = false;
int count = 0;
for (String s : str2) {
if (str1.contains(s)) {
count++;
}
}
if (count == str2.size()) {
isContained = true;
}
return isContained;
}
/**
* 字符列表移除公共元素
*
* @param str1
* @param str2
*/
public void removeOverLaped(ArrayList<String> str1, ArrayList<String> str2) {
ArrayList<String> deleteStrs = new ArrayList<>();
for (String s1 : str1) {
for (String s2 : str2) {
if (s1.equals(s2)) {
deleteStrs.add(s1);
break;
}
}
}
// 进行公共元素的移除
str1.removeAll(deleteStrs);
}
}