Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2015
260
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. void adiciona_palavra(trie* t, char* palavra)
  2. {
  3. if(t == NULL || palavra == NULL)
  4. print_error("Null argument.\n");
  5. else if(existe(t,palavra) == 1)
  6. return;
  7.  
  8. int i;
  9. trie *aux=t, *aux1;
  10.  
  11. for(i=0;i < strlen(palavra);i++)
  12. {
  13. if(aux->filhos[(int)palavra[i]-'a'] == NULL)
  14. {
  15. aux1=nova_trie();
  16. if(aux1 == NULL)
  17. print_error("Error.\n");
  18. aux->filhos[(int)(palavra[i]-'a')] = aux1;
  19. }
  20. aux=aux->filhos[(int)(palavra[i]-'a')];
  21. }
  22. if(i == 0)
  23. return;
  24.  
  25. aux->se_palavra = 1;
  26. }
  27.  
  28. void adiciona_ocorrencia(trie* t, char* palavra, int documento)
  29. {
  30. if(t == NULL || palavra == NULL || documento < 0)
  31. print_error("Null argument.\n");
  32.  
  33. elemento *elem;
  34. int i, n;
  35. trie *aux=t;
  36.  
  37. if(existe(t,palavra) == 0)
  38. adiciona_palavra(t,palavra);
  39.  
  40. for(i=0;i < strlen(palavra);i++)
  41. aux=aux->filhos[(int)(palavra[i]-'a')];
  42.  
  43. if(obtem_documentos(t,palavra) == NULL)
  44. lista_insere(aux->docs,documento,FIM,1);
  45.  
  46. n = lista_pesquisa(aux->docs,documento,INICIO);
  47.  
  48. if(n == -1)
  49. {
  50. aux->docs = lista_nova();
  51. lista_insere(aux->docs,documento,FIM,1);
  52. }
  53.  
  54. elem = lista_elemento(aux->docs,n);
  55.  
  56. elem->ocorrencias++;
  57. }
  58.  
  59.  
  60. trie* index_documentos(char** nomes, int n)
  61. {
  62. if(nomes == NULL || n < 0)
  63. return NULL;
  64. trie *t=nova_trie();
  65. if(t==NULL)
  66. return NULL;
  67. FILE *f;
  68. int j;
  69. char *str;
  70. for(j=0;j<n;j++)
  71. {
  72. f=fopen(nomes[j],"r");
  73. if(f==NULL)
  74. print_error("File Error.\n");
  75. while(!feof(f))
  76. {
  77. str = le_palavra(f);
  78. adiciona_ocorrencia(t,str,j);
  79. }
  80. }
  81. fclose(f);
  82. free(str);
  83. return t;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement