-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordAnalysis_ADT.java
339 lines (293 loc) · 13.4 KB
/
WordAnalysis_ADT.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import java.io.*;
import java.util.Scanner;
public class WordAnalysis_ADT {
int n,m,k;
LinkedList<WordInformation> [] arrayOfDifferentLengths ;
WordInformation [] sortedArray;
public WordAnalysis_ADT(String f )
{
k = Calcuate_K(f);
arrayOfDifferentLengths = new LinkedList [k+1];
sortedArray = new WordInformation [200];
readFileAndAnalyse(f);
}
private int Calcuate_K(String f)
{
int max = 0;
String word;
try{
Scanner input = new Scanner(new File(f));
while (input.hasNext()){
word = input.next();
word=word.replaceAll("[, . ; ? ! / \" ]","");
if (max < word.trim().length())
max = word.trim().length();
}
input.close();
}catch (IOException ex){
System.out.println("error in calcuating the longest word in the file");
}
return max;
}
private void readFileAndAnalyse (String f)
{
try {
for ( int i = 0 ; i< arrayOfDifferentLengths.length ; i ++)
arrayOfDifferentLengths[i] = new LinkedList<WordInformation> ();
Scanner input = new Scanner(new File(f));
int lineNo = 0;
n =0;
m =0;
String line = input.nextLine();
String AllWordInLine[] = null;
while (!(line==null))
{
lineNo ++;
int position =1;
AllWordInLine = line.split("\\s");
for (int i = 0; i < AllWordInLine.length; i++)
{
String word=AllWordInLine[i].replaceAll("[, . ; ? ! / \"]","");
word = word.trim();
if (word.equalsIgnoreCase(""))
continue;
n++;
boolean FoundWord = false;
//Start reading
if (arrayOfDifferentLengths[word.length()].empty())
arrayOfDifferentLengths[word.length()].insert(new WordInformation (word , lineNo, position));
else
{
arrayOfDifferentLengths[word.length()].findFirst();
while (! arrayOfDifferentLengths[word.length()].last() && ! FoundWord)
{
if (arrayOfDifferentLengths[word.length()].retrieve().word.equalsIgnoreCase(word))
{
arrayOfDifferentLengths[word.length()].retrieve().Add(lineNo, position);
FoundWord = true;
}
else
arrayOfDifferentLengths[word.length()].findNext();
}
if ( ! FoundWord)//check the last node
{
if (arrayOfDifferentLengths[word.length()].retrieve().word.equalsIgnoreCase(word))
{
arrayOfDifferentLengths[word.length()].retrieve().Add(lineNo, position);
FoundWord = true;
}
}
if (!FoundWord )
arrayOfDifferentLengths[word.length()].insert(new WordInformation (word , lineNo, position));
}
//sorted Array
if (!FoundWord )
{
sortedArray[m] = new WordInformation (word , lineNo, position);
m++;
}
else
{
for ( int j = 0 ; j < m ; j++)
if (sortedArray[j] != null && sortedArray[j].word.equalsIgnoreCase(word))
sortedArray[j].size = arrayOfDifferentLengths[word.length()].retrieve().size ;
}
position ++ ;
}
if(input.hasNextLine())
line = input.nextLine();
else
break;
}
input.close();
mergesort(0, m-1 );
} catch (IOException ex) {
System.out.println("error in read method");
}
}
//*************************************************************************************************
// operation 1
public int documentLength ()
{
return n;
}
//*************************************************************************************************
// operation 2
public int uniqueWords ()
{
return m;
}
//*************************************************************************************************
// operation 3
public int totalWord (String w)
{
int count = 0 ;
if ( arrayOfDifferentLengths[w.length()].getsize() > 0)
{
arrayOfDifferentLengths[w.length()].findFirst();
while (! arrayOfDifferentLengths[w.length()].last())
{
if ( arrayOfDifferentLengths[w.length()].retrieve().word.equalsIgnoreCase(w)== true)
count = arrayOfDifferentLengths[w.length()].retrieve().size;
arrayOfDifferentLengths[w.length()].findNext();
}
if ( arrayOfDifferentLengths[w.length()].retrieve().word.equalsIgnoreCase(w)== true)
count = arrayOfDifferentLengths[w.length()].retrieve().size;
}
return count;
}
//*************************************************************************************************
// operation 4
public int totalWordsForLength (int l)
{
return arrayOfDifferentLengths[l].getsize();
}
//*************************************************************************************************
// operation 5
public void displayUniqueWords ()
{
for ( int i = 0 ; i <m ; i++)
System.out.println(" ( " + sortedArray[i].word + " , " + sortedArray[i].size + " ), ");
}
//*************************************************************************************************
// operation 6
public LinkedList<WordOccurrence> occurrences (String w){
LinkedList<WordOccurrence> tmp = null;
if ( arrayOfDifferentLengths[w.length()].getsize() > 0){
arrayOfDifferentLengths[w.length()].findFirst();
while (! arrayOfDifferentLengths[w.length()].last()){
if ( arrayOfDifferentLengths[w.length()].retrieve().word.equalsIgnoreCase(w)== true)
tmp = arrayOfDifferentLengths[w.length()].retrieve().occList;
arrayOfDifferentLengths[w.length()].findNext();
}
if ( arrayOfDifferentLengths[w.length()].retrieve().word.equalsIgnoreCase(w)== true)
tmp = arrayOfDifferentLengths[w.length()].retrieve().occList;
}
return tmp;
}
//*************************************************************************************************
// operation 7
public boolean checkAdjacent (String word1, String word2){
//word 1 and word 2 are the same
if (word1.equalsIgnoreCase(word2)){
LinkedList<WordOccurrence> wordOccList = occurrences(word1);
if (wordOccList != null ){
if (wordOccList.getsize() > 1){
wordOccList.findFirst();
WordOccurrence pos1 = wordOccList.retrieve();
for ( int i = 1; i < wordOccList.getsize() ; i++){
wordOccList.findNext();
WordOccurrence pos2 = wordOccList.retrieve();
if ( pos1.lineNo == pos2.lineNo && ((pos2.position - pos1.position) ==1 || (pos2.position - pos1.position) ==-1) )
return true;
pos1=pos2;
}
}
}
System.out.print("sorry these two word isn't in the file ");
return false;
}
// the words aren't the same
if (( arrayOfDifferentLengths[word1.length()].getsize() ==0) || (arrayOfDifferentLengths[word2.length()].getsize() == 0))
return false;
LinkedList<WordOccurrence> W1OccList = occurrences (word1);
LinkedList<WordOccurrence> W2OccList = occurrences (word2);
if ( W1OccList != null && W2OccList != null )
{
W1OccList.findFirst();
W2OccList.findFirst();
while ( ! W1OccList.last() && !W2OccList.last())
{
int line1 = W1OccList.retrieve().lineNo;
int line2 = W2OccList.retrieve().lineNo;
if ( line1 == line2 )
{
int w_no1 = W1OccList.retrieve().position;
int w_no2 =W2OccList.retrieve().position;
if ((w_no2 - w_no1) == 1 || (w_no2 - w_no1)==-1)
return true;
else if ( (w_no2 - w_no1) > 1)
W1OccList.findNext();
else
W2OccList.findNext();
}
else if ( line1 > line2)
{
W2OccList.findNext();
}
else
{
W1OccList.findNext();
}
}
while ( W1OccList.last() && !W2OccList.last())
{
int line1 = W1OccList.retrieve().lineNo;
int line2 =W2OccList.retrieve().lineNo;
if ( line1 == line2 )
{
int w_no1 = W1OccList.retrieve().position;
int w_no2 =W2OccList.retrieve().position;
if ((w_no2 - w_no1) == 1 || (w_no2 - w_no1) == -1)
return true;
}
W2OccList.findNext();
}
while ( !W1OccList.last() && W2OccList.last())
{
int line1 = W1OccList.retrieve().lineNo;
int line2 =W2OccList.retrieve().lineNo;
if ( line1 == line2 )
{
int w_no1 = W1OccList.retrieve().position;
int w_no2 =W2OccList.retrieve().position;
if ((w_no2 - w_no1) == 1 || (w_no2 - w_no1) == -1)
return true;
}
W1OccList.findNext();
}
if ( W1OccList.last() && W2OccList.last())
{
int line1 = W1OccList.retrieve().lineNo;
int line2 =W2OccList.retrieve().lineNo;
if ( line1 == line2 )
{
int w_no1 = W1OccList.retrieve().position;
int w_no2 =W2OccList.retrieve().position;
if ((w_no2 - w_no1) == 1 || (w_no2 - w_no1) == -1)
return true;
}
}
}
return false;
}
public void mergesort ( int First , int Last )
{
if ( First >= Last )
return;
int middle = ( First + Last ) / 2;
mergesort (First , middle ) ;
mergesort (middle + 1 , Last ) ;
merge (First , middle , Last ) ;
}
private void merge ( int First , int middle , int Last )
{
WordInformation [] Array = new WordInformation [ Last - First + 1];
int i = First , j = middle + 1 , k = 0;
while ( i <= middle && j <= Last )
{
if ( sortedArray[ i ].size >= sortedArray [ j ].size)
Array [ k ++] = sortedArray[ i ++];
else
Array [ k ++] = sortedArray[ j ++];
}
if ( i > middle )
while ( j <= Last )
Array [ k ++] = sortedArray[ j ++];
else
while ( i <= middle )
Array [ k ++] = sortedArray[ i ++];
for ( k = 0; k < Array . length ; k ++)
sortedArray[ k + First ] = Array [ k ];
}
}