-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDictionary.java
293 lines (191 loc) · 6.86 KB
/
Dictionary.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package lab2;
import java.util.ArrayList;
import java.io.*;
import java.io.IOException;
public class Dictionary {
private ArrayList<Word> theDictionary;
// contains all the instances of Word, each instance (objekt)contains one word
private FileWriter outputWriter; // write out content of theDictionary using method toString()
// outputWriter ska skriva ut
// står i labbinstruktionen vi ska ha BufferedWriter men det fungerar ej så har FileWriter istället
public Dictionary() {
theDictionary = new ArrayList<Word>();
}
public Dictionary(String arg1) {
//don't return anything, so don't need any type before Dictionarym just public
theDictionary = new ArrayList<Word>();
this.addWords(arg1);
}
public void addWords(String arg2) {
String[] listOfIndividualWords = arg2.split(" +");
String regex = "\\d+"; // \\d = any digit
// listOfIndividualWords.length hur många ord i strängen
for(int i=0; i < listOfIndividualWords.length; i++)
{
if(!(listOfIndividualWords[i].isEmpty()))
{
if(!(listOfIndividualWords[i].matches(regex) && !listOfIndividualWords[i].equals(null))) // spara endast ord, inga siffror
{
theDictionary.add(new Word(listOfIndividualWords[i]));
// sparar word objektet i arrayen direkt
}
}
}
}
public int numberOfWords() {
return theDictionary.size(); //size() returns number of elements in list theDictionary
}
public void removeDuplicates() {
for(int j = 0; j <theDictionary.size(); j++)
{
Word compareWords = theDictionary.get(j);
//testa med debuggern om dessa looparna är okej
for(int i =j+1; i< theDictionary.size() ; i++)
{
if(compareWords.getWord().toLowerCase().equals(theDictionary.get(i).getWord().toLowerCase()))
{
// compareWords.getWord() tar ordet plockar fram som sträng
/**
for( int k = 0; k < theDictionary.get(i).getCounts(); k++ )
{
compareWords.increaseCounts();
// if compareWords is 5, and theDictionary.get(i) is 2
// compareWords will be 7 due to calling at function increaseCounts
}*/
compareWords.increaseCounts();
theDictionary.remove(i); //removes object when done counting
i-=1;
//eftersom att arrayens längd ändras när man tar bort ett värde
// man hoppar över ett värde annars eftersom de rör sig åt två motsatta håll, loopen och objekten
}
}
}
}
/**
public void removeDuplicates() {
for (int i = 0; i < theDictionary.size()-1; i++) {
for (int j =i+1; j < theDictionary.size(); j++) {
if( theDictionary.get(i).getWord().toLowerCase().contentEquals(theDictionary.get(j).getWord().toLowerCase())) {
theDictionary.get(i).increaseCounts();
theDictionary.remove(j);
j = j-1;
}
}
}
}
*/
/**
/**
*
* public String countOccurences() {
Integer M = 0;//how many words that occur N times
int N=0; //how many times a word occurs
// int howmanyOccurences[] = new int[theDictionary.size()]; //skapar ny array
// int compareOccurences[] = new int[theDictionary.size()];
for(int i = 0; i <theDictionary.size(); i++ )
{
M += theDictionary.get(i).getCounts();
// howmanyOccurences[i] = theDictionary.get(i).getCounts();
/** for(int j = i+1; j < theDictionary.size(); j++)
{
compareOccurences[j] = theDictionary.get(j).getCounts();
if(compareOccurences[j]==howmanyOccurences[i])
{
//turn how many times a word occur to an int
N = howmanyOccurences[i];
M++;
}
}
}
// ska den vara här?
//String returningOccurences = String.format("There are " + M + " words that occured " + N + " times.");
return M.toString();
}
**/
public String countOccurences() {
Integer M = 0;//how many words that occur N times
int counter=1; //how many times a word occur
int N = 0;
String result = "";
for (int i = 0; i < theDictionary.size(); i++) {
if(theDictionary.get(i).getCounts() >= counter) {
counter = theDictionary.get(i).getCounts();
}
}
Integer[] array = new Integer[counter+1];
// fyller array med nollor
for(int i = 0; i < array.length; i++) {
array[i]=0;
}
for(int j = 0; j < array.length; j++) {
for (int i = 0; i < theDictionary.size(); i++ ) {
if(theDictionary.get(i).getCounts()== j+1) {
array[j]++;
}
}
if (array[j]!=0) {
M = array[j];
N = j+1;
result+= "There are " + M + " words that occured " + N + " times. \n";
}
}
return result;
}
public void sortDictionaryByCounts() {
// sortDictionaryByCounts() should sort the entries of theDictionary by
// their number of counts. After sorting, the first entry should be that with
// the largest number of counts.
// sorting array
for(int pass = 1; pass <= theDictionary.size()-1; pass++ )
{
for(int k=0; k < theDictionary.size()-1; k++)
{
if(theDictionary.get(k).getCounts() < theDictionary.get(k+1).getCounts())
{
Word aux = theDictionary.get(k);
theDictionary.set(k, theDictionary.get(k+1));
theDictionary.set(k+1,aux);
}
}
}
}
public void setFileName(String filename)throws IOException {
//när man har skrivit save "aName", sparas det namnet man har valt till filen
outputWriter = new FileWriter(new File(filename));
}
public void saveFile() throws IOException {
//metoden saveFile sparar en sträng som har skickats från metoden toString
try
{
for(int i=0; i < theDictionary.size(); i++)
{
outputWriter.write(theDictionary.get(i).getWord() + " ");
// kallar på metoden outputWriter som hämtar ett ord och skriver ut det
// lägger till ett " " så att det blir mellanslag mellan varje ord
// lägger in det i ny fil som man sparar texten i
}
}
catch(IOException err)
{
System.out.println("ERROR when saving the file!");
}
outputWriter.close();
}
public String toString() {
int number1 = numberOfWords();
int number2 = 0;
for (int i = 0; i < theDictionary.size(); i++)
{
number2 = number2 + theDictionary.get(i).getCounts();
}
String result = String.format("Total words: " + number1 + " and total occurences " + number2 + " \n");
for(int i=0; i < theDictionary.size(); i++)
{
result = result + theDictionary.get(i).toString() + " \n";
//lägger på alla ord innan return sträng
// hela texten efter statistiken
// toString tar ordet ut textfilen med funktionen get() för att bygga upp en sträng
}
return result;
}
} //måsvinge till class dictionary