-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dataset.java
122 lines (108 loc) · 3.59 KB
/
Dataset.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
package recommender.sol;
import recommender.src.IAttributeDataset;
import recommender.src.IAttributeDatum;
import java.util.LinkedList;
/**
* class that implements IAttributeDataset
*
* @param <T> the class of the type of datum stored in a dataset
*/
public class Dataset<T extends IAttributeDatum> implements IAttributeDataset<T> {
/**
* fields - list of attributes that the datum has
* - list of data - of type T
*/
public LinkedList<String> attributes;
public LinkedList<T> rows;
/**
* constructor
*
* @param attributes list of attributes that the datum has
* @param rows list of data - of type T
*/
public Dataset(LinkedList<String> attributes, LinkedList<T> rows) {
this.attributes = attributes;
this.rows = rows;
}
@Override
public LinkedList<String> getAttributes() {
return this.attributes;
}
@Override
public boolean allSameValue(String ofAttribute) {
Object val = this.rows.get(0).getValueOf(ofAttribute);
for (T datum : this.rows) {
if (datum.getValueOf(ofAttribute) != val) {
return false;
}
}
return true;
}
@Override
public int size() {
return this.rows.size();
}
public IAttributeDataset<T> filter(String onAttribute, Object value) {
LinkedList<T> list = new LinkedList<T>();
for (T datum : this.rows) {
if (datum.getValueOf(onAttribute).equals(value)) {
list.addLast(datum);
}
}
Dataset<T> data = new Dataset<T>(this.attributes, list);
return data;
}
/**
* @param onAttribute an attribute of the Data in the dataset
* @return a list of all the values that data in the dataset take for the given attribute
*/
public LinkedList<Object> extractValues(String onAttribute) {
LinkedList<Object> list = new LinkedList<Object>();
for (T datum : this.rows) {
Object value = datum.getValueOf(onAttribute);
if (!(list.contains(value))) {
list.addLast(value);
}
}
return list;
}
@Override
public LinkedList<IAttributeDataset<T>> partition(String onAttribute) {
LinkedList<Object> extracted = this.extractValues(onAttribute);
LinkedList<IAttributeDataset<T>> list1 = new LinkedList<IAttributeDataset<T>>();
for (Object value : extracted) {
list1.addLast(this.filter(onAttribute, value));
}
return list1;
}
@Override
public Object getSharedValue(String ofAttribute) {
assert this.allSameValue(ofAttribute);
return this.rows.get(0).getValueOf(ofAttribute);
}
@Override
public Object mostCommonValue(String ofAttribute) {
LinkedList<Object> extracted = this.extractValues(ofAttribute);
LinkedList<Integer> count = new LinkedList<Integer>();
for (Object value : extracted) {
int i = 0;
for (T datum : this.rows) {
if (datum.getValueOf(ofAttribute) == value) {
i++;
}
}
count.addLast(i);
}
int currentMax = 0;
int index = 0;
int finalIndex = 0;
for (int i : count) {
if (i > currentMax) {
currentMax = i;
finalIndex = index;
index++;
}
}
return extracted.get(finalIndex);
}
}