-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWordAnalysisADT.java
More file actions
187 lines (152 loc) · 3.29 KB
/
WordAnalysisADT.java
File metadata and controls
187 lines (152 loc) · 3.29 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
import java.util.Scanner;
import java.io.*;
public class WordAnalysisADT{
int numOfNodes;
int numOfDuplicate;
Node occurArray[];
private Node head;
private Node current;
public WordAnalysisADT(){
numOfNodes=0;
numOfDuplicate=0;
head=current=null;
}
//---------------------------------------//
//(1)
public int totalNum(){
return numOfNodes;
}
//---------------------------------------//
//(2)
public int numUnqWords(){
return occurArray.length;
}
//---------------------------------------//
//(3)
public int numOfOccur(String word){
int count=0;
for(int i=0;i<occurArray.length; i++){
if(occurArray[i].data.equals(word)){
count=occurArray[i].appear;
break;}//if
}
return count;
}
//---------------------------------------//
//(4)
public int wordLen(int l){
int count=0;
for(int i=0;i<occurArray.length; i++){
if(occurArray[i].data.length()==l)
count+=occurArray[i].appear;
}
return count;
}
//---------------------------------------//
//(5)
public String printOccur(){
String str="";
for(int i=0;i<occurArray.length; i++)
str+="("+occurArray[i].data+","+occurArray[i].appear+")"+",";
return str.substring(0,str.length()-1);
}
//---------------------------------------//
//(6)
public String printLoc(String word){
String str="";
current=head;
while(current!=null){
if(current.data.equals(word))
str+="("+current.y+","+current.x+")"+",";
current=current.next;
}//while
return str.substring(0,str.length()-1);
}
//---------------------------------------//
//(7)
public boolean adj(String w1,String w2){
current=head;
while(current.next!=null){
if( ((current.data.equals(w1)) && (current.next.data.equals(w2))) || ((current.data.equals(w2)) && (current.next.data.equals(w1))))
return true;
current=current.next;
}//while
return false;
}
//insert
public void insertString(String fileName) throws IOException{
Scanner s=new Scanner(new File(fileName));
Node p;
int col=1;
int row=1;
String newl="\\n";
while(s.hasNext()){
String word=s.next();
if(word.equals(newl)){
row++;
col=1;
}
else{
if(Character.isLetter(word.charAt(0))){
if(!Character.isLetter(word.charAt(word.length()-1)))
word=word.substring(0,word.length()-1);
insert(word);
numOfNodes++;
Node t=current;
p=head;
while(p!=null){
if(p.data.equals(t.data) && t!=p){
t.appear++;
p.appear++;
t.firstOccur=false;
}//if
p=p.next;
}//inner while
t.x=col;
col++;
t.y=row;
}// if
}//big if
}// reading while
p=head;
while(p!=null){
if(p.firstOccur && p.appear>1){
numOfDuplicate+=p.appear;
numOfDuplicate--;
}//if
p=p.next;
}//while
int index=0;
occurArray=new Node[numOfNodes-numOfDuplicate];
p=head;
while(p!=null){
if(p.firstOccur==true){
occurArray[index]=p;
index++;
}//if
p=p.next;
}//while
for(int i=0; i<occurArray.length-1 ;i++){
for(int j=0; j<occurArray.length-1-i; j++){
if(occurArray[j].appear<occurArray[j+1].appear){
Node temp=occurArray[j];
occurArray[j]=occurArray[j+1];
occurArray[j+1]=temp;
}// if
}//small for
}//big for,
}//insert
public void insert(String d){
Node temp=new Node(d);
if(empty())
head=current=temp;
else{
temp.next=current.next;
current.next=temp;
current=temp;
}
}
public boolean empty(){
return head==null;
}
}