Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void adiciona_palavra(trie* t, char* palavra)
- {
- if(t == NULL || palavra == NULL)
- print_error("Null argument.\n");
- else if(existe(t,palavra) == 1)
- return;
- int i;
- trie *aux=t, *aux1;
- for(i=0;i < strlen(palavra);i++)
- {
- if(aux->filhos[(int)palavra[i]-'a'] == NULL)
- {
- aux1=nova_trie();
- if(aux1 == NULL)
- print_error("Error.\n");
- aux->filhos[(int)(palavra[i]-'a')] = aux1;
- }
- aux=aux->filhos[(int)(palavra[i]-'a')];
- }
- if(i == 0)
- return;
- aux->se_palavra = 1;
- }
- void adiciona_ocorrencia(trie* t, char* palavra, int documento)
- {
- if(t == NULL || palavra == NULL || documento < 0)
- print_error("Null argument.\n");
- elemento *elem;
- int i, n;
- trie *aux=t;
- if(existe(t,palavra) == 0)
- adiciona_palavra(t,palavra);
- for(i=0;i < strlen(palavra);i++)
- aux=aux->filhos[(int)(palavra[i]-'a')];
- if(obtem_documentos(t,palavra) == NULL)
- lista_insere(aux->docs,documento,FIM,1);
- n = lista_pesquisa(aux->docs,documento,INICIO);
- if(n == -1)
- {
- aux->docs = lista_nova();
- lista_insere(aux->docs,documento,FIM,1);
- }
- elem = lista_elemento(aux->docs,n);
- elem->ocorrencias++;
- }
- trie* index_documentos(char** nomes, int n)
- {
- if(nomes == NULL || n < 0)
- return NULL;
- trie *t=nova_trie();
- if(t==NULL)
- return NULL;
- FILE *f;
- int j;
- char *str;
- for(j=0;j<n;j++)
- {
- f=fopen(nomes[j],"r");
- if(f==NULL)
- print_error("File Error.\n");
- while(!feof(f))
- {
- str = le_palavra(f);
- adiciona_ocorrencia(t,str,j);
- }
- }
- fclose(f);
- free(str);
- return t;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement