-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathword_frequency_count.cpp
More file actions
205 lines (176 loc) · 5.19 KB
/
Copy pathword_frequency_count.cpp
File metadata and controls
205 lines (176 loc) · 5.19 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
using namespace std;
char upperToLowerCase (char ch);
int readCharacters(char data[]);
bool isLetter (char c);
int getWord (char words[], int start, char word[]);
void sort(string array[], int len);
void countWords(char allWords[]);
int main(){
ifstream in;
char word;
int count=0, i = 0, chFind, wFind;
char data[500];
char words[50];
chFind=readCharacters(data);
cout<<endl;
countWords(data);
cout<<"Program Excuted and output placed into the result.txt file"<<endl;
return 0;
}
//Returns a character in lowercase form
//eg: if character is 'A' it will return 'a'
char upperToLowerCase (char ch){
if((ch>= 'A') && (ch<='Z')){
ch=ch+32;
}
return ch;
}
//Returns an integer, i, indicating the amount of characters present in the file passage.txt
//Reads each character from the text file and changes it to it's lowercase form and adds it to an array
int readCharacters(char data[]) {
ifstream fin;
char ch;
int i;
fin.open("passage.txt");
if (!fin.is_open()) {
cout << "Error -- passage.txt could not be opened." << endl;
return -1;
}
i = 0;
fin >> noskipws;
fin >> ch;
ch=upperToLowerCase(ch);//changes all characters to lower case. Makes data easier to manipulate.
while (!fin.eof()) {
data[i] = ch;
i = i + 1;
fin>>noskipws;
fin >> ch;
ch=upperToLowerCase(ch);
}
data[i] = '\0';
fin.close();
return i;
}
//Returns a boolean value, True or False.
//Accepts a single character and determines if it is a letter.
bool isLetter (char c) {
if ((c >= 'A' && c <= 'Z') ||
(c >= 'a' && c <= 'z'))
return true;
else
return false;
}
//Returns i, an integer value, that represents the starting point to look for another word
//This function takes each seperated character and remakes the word into a c-string
/*
Parameters:
words[] -> character array, contains an array of each character from the text file ending in '\0'
start -> integer
word[] -> character array, contains an array of c-strings
*/
int getWord (char words[], int start, char word[]) {
int i, j, length;
length = strlen (words); // find how many characters in words
i = start;
while (i < length && !isLetter(words[i]))
i = i + 1;
if (i == length) // there are no more words
return -1;
j = 0; // copy characters to word
while (isLetter(words[i])) {
word[j]= words[i];
i = i + 1;
j = j + 1;
}
word[j]= '\0';
return i; // starting position to look for next word
}
/*
This function sorts the words in ascending order
*/
void sort(string array[], int len) {
int i, j;
string check;
for(i=1; i<len; i++) {
check = array[i];
for(j=i; j>=1 && (check < array[j-1]); j--) {
array[j] = array[j-1];
array[j-1] = check;
}
}
}
//Outputs the frequency of words counted to results.txt
void countWords(char allWords[]){
ofstream output;
int length, wcount, j=0, i=0, x=0, y=0, te=0, tr, counter=0, ty=0, cap;
int count[100];
string tempW[100];//tempW: temporary Word aray
string tempW2[100];
string temp, temp2;
char tempC[100];//tempC: temporary Character array
output.open("result.txt");
if(!output.is_open()){
cout<<"Error opening file result.txt"<<endl;
return ;
}
length=strlen(allWords);
for(int l=0;l<length;l++){
if(allWords[l]== ' '){
wcount++;
}
}
//The while loop is used to acquire all the words from the allWords[] array and store it in
// the tempW[] string array as strings
while(i<wcount){
j=getWord(allWords, j, tempC);
tempW[i]=tempC;
i++;
}
for(int wd=0;wd<i;wd++){
count[wd]= 0;
}
sort(tempW, i);//sorts the words in tempW array even if they occur more than once
while(x<i){
temp=tempW[te];
for(int ja=0;ja<i;ja++){
if(temp==tempW[ja]){
tr=ja;//tr is the index of the last occurrence of the word in the tempW[] array
}
}
tempW2[y]=tempW[tr];//This array was used to remove the words that appear more than once
te=tr+1;//te is given the number tr+1 to go to the next index position in the tempW[] array to get the next word
//cout<<te<<endl;
x=te;// x is assigned the value of the variable te to avoided duplication of the last word in the array
y++;
}
//checks for the occurrence of each word
while(counter<y){
temp2=tempW2[counter];
for (int mn=0;mn<i;mn++){
if(temp2==tempW[mn]){
count[counter]=count[counter]+1;
}
}
counter++;
}
output<<"Words"<<"==============================="<<"Frequency"<<endl;
for(int jk=0;jk<y;jk++){
if(jk<50){
output<<tempW2[jk]<<"\t\t\t\t\t\t"<<count[jk]<<endl;
cap=jk;
}
}
cap=cap+1;
for(int qw=0;qw<5;qw++){
output<<endl;
}
output<<"These word(s) were unable to be stored."<<endl;
output<<"Words"<<"====================================="<<"Frequency"<<endl;
for(int lp=cap; lp<y;lp++){
output<<tempW2[lp]<<"\t\t\t\t\t\t"<<count[lp]<<endl;
}
}