-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
234 lines (179 loc) · 4.78 KB
/
main.c
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 1000
int tamanho = 0;
int tamanho2 = 0;
int suma = 0;
typedef struct{
int quantidade;
char palavra[50];
} Sumario;
typedef struct{
char palavra[50];
} Texto;
typedef struct{
char plv[50];
} Stopwords;
typedef struct{
char palavra[50];
} Origem;
Sumario sumario[MAX];
Texto texto[MAX];
Stopwords stopwords[MAX];
Origem origem[MAX];
void preencher(Texto *texto, char *arq);
void stw(Stopwords *stopwords, const char *arqStp);
void removerStopWords(Texto *texto, Stopwords *stopwords);
void preencherSemRepetir(Texto *texto, Sumario *sumario);
void eliminar(Texto *texto);
void copiar(Texto *texto, Origem *origem);
void comparacao(Sumario *sumario, Origem *origem);
void ordenar(Sumario *sumario);
void salvar(Sumario *sumario, char *sKOaida, int quantidade);
int main(int argc, char **argv) {
int n = atoi(argv[1]);
argc++;
if(n != 2){
printf("Nao existe o caminho para arquivo HTML.");
return 0;
} else{
char *arq = argv[2];
char *stp = argv[4];
char *saida = argv[5];
int a = atoi(argv[3]);
preencher(texto, arq);
stw(stopwords, stp);
removerStopWords(texto, stopwords);
eliminar(texto);
copiar(texto, origem);
preencherSemRepetir(texto, sumario);
comparacao(sumario, origem);
ordenar(sumario);
salvar(sumario, saida, a);
}
return 0;
}
void preencher(Texto *texto, char *arq){
FILE *file;
file = fopen(arq,"r");
if(file){
int i;
for(i = 0; i < MAX; i++){
fscanf(file,"%s",texto[tamanho].palavra);
tamanho++;
}
fclose(file);
} else{
printf("Arquivo nao encontrado!\n");
}
}
void stw(Stopwords *stopwords, const char *arqStp){
FILE *file;
file = fopen(arqStp,"r");
if(file){
int i;
for(i = 0; i < MAX; i++){
fscanf(file,"%s",stopwords[tamanho2].plv);
tamanho2++;
}
fclose(file);
} else{
printf("Arquivo nao encontrado!\n");
}
}
void removerStopWords(Texto *texto, Stopwords *stopwords){
int i, j;
for(i = 0; i < MAX; i++){
for(j = 0; j < MAX; j++){
if(strcasecmp(texto[i].palavra, stopwords[j].plv) == 0){
strcpy(texto[i].palavra, "");
}
}
}
}
void preencherSemRepetir(Texto *texto, Sumario *sumario){
int i, j;
for(i = 0; i < tamanho; i++){
for(j = 0; j < i; j++){
if(strcasecmp(texto[j].palavra, texto[i].palavra) == 0){
strcpy(texto[j].palavra, "");
}
}
}
for(j = 0; j < tamanho; j++){
if(strcmp(texto[j].palavra, "") != 0){
strcpy(sumario[suma].palavra, texto[j].palavra);
sumario[suma].quantidade = 0;
suma++;
}
}
}
void eliminar(Texto *texto){
int i;
char aux[50];
for(i = 0; i < tamanho; i++){
strcpy(aux, texto[i].palavra);
int l = 0;
int ver = 0;
while(aux[l] != '\0'){
if(aux[l] == ',' || aux[l] == '.' || aux[l] == '?' || aux[l] == '!' || aux[l] == ';'){
ver = 1;
}
l++;
}
if(ver){
aux[l - 1] = '\0';
}
strcpy(texto[i].palavra, aux);
}
}
void comparacao(Sumario *sumario, Origem *origem){
int i, j;
for(i = 0; i < suma; i++){
for(j = 0; j < tamanho; j++){
if(strcasecmp(sumario[i].palavra, origem[j].palavra) == 0){
sumario[i].quantidade++;
}
}
}
}
void copiar(Texto *texto, Origem *origem){
int i;
for(i = 0; i < tamanho; i++){
strcpy(origem[i].palavra, texto[i].palavra);
}
}
void ordenar(Sumario *sumario){
int i, j;
int quant;
char auxPalavra[50];
for(i = 1; i < tamanho; i++){
quant = sumario[i].quantidade;
strcpy(auxPalavra, sumario[i].palavra);
j = i - 1;
while((j >= 0) && quant > sumario[j].quantidade){
sumario[j + 1].quantidade = sumario[j].quantidade;
strcpy(sumario[j + 1].palavra, sumario[j].palavra);
j--;
}
sumario[j + 1].quantidade = quant;
strcpy(sumario[j + 1].palavra, auxPalavra);
}
}
void salvar(Sumario *sumario, char *saida, int quantidade){
int i;
FILE *file = fopen (saida, "w");
i = 0;
if(file){
while ( quantidade-- ) {
if(sumario[i].quantidade == 0){
break;
} else{
fprintf(file, "%s %d\n", sumario[i].palavra, sumario[i].quantidade);
}
i++;
}
fclose(file);
}
}