-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordBag.java
More file actions
189 lines (145 loc) · 5.24 KB
/
WordBag.java
File metadata and controls
189 lines (145 loc) · 5.24 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
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package wordbag;
/**
*
* @author Cale Bierman
* @hawkid cbierman
*/
import java.util.Random;
public class WordBag {
MyWord [] myArray;
static int defaultCapacity = 1000;
int numWords; //how many distinct words
public WordBag() {
this(defaultCapacity);
}
public WordBag(int x) {
if (x < defaultCapacity)
x = defaultCapacity + 1;
myArray = new MyWord [x];
numWords = 0;
}
//Increases the frequency of the provided word and keeps myArray in decreasing
//order
public void addOccurance(String word) {
int currFreq;
int index = getIndexOf(word);
//System.out.println(index + " <<<<<<<<");
//If the index == numWords, then we have a new word to go inside the list
if (index == numWords) {
myArray[index] = new MyWord(word);
numWords++;
}
else {
//Otherwise, we get the current freqency and add one to it
currFreq = myArray[index].getFrequency();
currFreq = currFreq + 1;
//safe will be used as a handle for the word we're adding an occurance to
MyWord safe = myArray[index];
safe.setFrequency(currFreq);
//if the index is 0, the word is already the most frequent word
//so we just increase it's frequency
if (index == 0) {
myArray[index].setFrequency(currFreq);
}
//otherwise, if increasing the frequency puts myArray out of order,
//we need to find where it needs to go and put it there
else if (currFreq > myArray[index -1].getFrequency()) {
//Use binary search to find the index of where the word needs to go
int place = binarySearch(myArray, 0, index, currFreq);
//However, if the new frequency is not in myArray, we have to
//linearly search for where it needs to go
if (place == -1){
for (int i = 0; i< numWords;i++) {
if (myArray[i].getFrequency() < currFreq) {
place = i;
break;
}
}
}
//We then shift the rest of the list items over by 1 space
//to make room.
for (int j = index; j>place; j--) {
myArray[j] = myArray[j-1];
}
//and insert the new frequency into it's correct position
myArray[place]=safe;
}
//This fires off when increasing the frequency doesn't affect the
//order.
else {
myArray[index].setFrequency(currFreq);
}
}
}
public void removeOccurance(String word) {
int index = getIndexOf(word);
if (index == numWords)
return;
int currFreq = myArray[index].getFrequency();
if (currFreq > 1) {
myArray[index].setFrequency(currFreq - 1);
}
else {
for (int j = index + 1; j < numWords; j++) {
myArray[j-1] = myArray[j];
}
numWords--;
}
}
public String mostFrequent(){
return myArray[0].getWord();
}
public int numberOfWords() { //return number of distinct words
return numWords;
}
public int getFrequency(String word) {
int index = getIndexOf(word);
if (index < numWords) {
return myArray[index].getFrequency();
}
return -1;
}
private int getIndexOf(String word) {
int index = numWords;
for (int i = 0; i < numWords; i++) {
if (myArray[i].getWord().equals(word)) {
index = i;
break;
}
}
return index;
}
//Uses recursion to find the index of x within the Array A.
public static int binarySearch(MyWord [] A, int start, int end, int x) {
int mid = (start + end)/2;
if (start > end) {
return -1;
}
if (A[mid].getFrequency() == x) {
return mid;
}
else if (A[mid].getFrequency() < x) {
return binarySearch(A, start, mid-1, x);
}
else {
return binarySearch(A, mid+1, end, x);
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
WordBag bg = new WordBag();
for(int y = 0; y < bg.numWords; y++){
System.out.print(bg.myArray[y].getWord() + " ");
System.out.println(bg.myArray[y].getFrequency());
//System.out.println(bg.myArray[y]);
}
//System.out.println(binarySearch(bg.myArray, 0, (bg.numWords)-1 , 2));
}
}