/* Dynamicznie alokuje pamiec wczytujac slowa do listy dwukierunkowej wyrazow. Lista jest uznawana za poprawna jesli kazde slowo ma szanse zmiescic sie w dowolnej kolumnie i zawiera jakikolwiek wyraz. Parametry: - Words: struktura pomocnicza listy slow, - S1: dlugosc pierwszej kolumny, - S2: dlugosc drugiej kolumny. Zwracana wartosc: 1 jesli udalo sie wczytac liste i jest poprawna, 0 jesli lista jest nieprawidlowa lub nie udalo sie jej wczytac. */ int readWords (WordsHandler *Words, const int S1, const int S2) { /* Lancuch do pobierania danych z wejscia. */ char OriginalString [3*LineLength]; /* Ktory znak strumienia wejsciowego jest obecnie przetwarzany. */ int CurrentSign = 0, /* Dlugosc biezacego wyrazu (binarna, po uwzglednieniu znakow spoza ASCII moze byc inna). */ CurrentLength = 0; /* Przygotwanie do pobierania slow */ Words->First = NULL; if (addNewWord (Words) == NULL) { printf ("Memory allocation error!\n"); return 0; } /* Wczytywanie wyrazow do dynamicznie alokowanej pamieci. */ while (scanf ("%s", OriginalString) != -1 && OriginalString [0] != EOF) { /* Obliczanie wymaganej pamieci wymaganej dla wyrazu i usuwanie ew. bialych znakow z tesktu. */ for (CurrentSign = 0; OriginalString [CurrentSign] != '\0'; CurrentSign++) if (OriginalString [CurrentSign] != '\n' && OriginalString [CurrentSign] != ' ' && OriginalString [CurrentSign] != '\r' && OriginalString [CurrentSign] != '\t') CurrentLength++; /* Jesli nie udalo sie zaalokowac pamieci, zwalnia dotychczas zaalokowana pamiec i przerywa program. */ if (CurrentLength != 0 && (fillWord (OriginalString, Words->CurrentForward, CurrentLength, CurrentSign) == NULL || addNewWord (Words) == NULL) ) { printf ("Memory allocation error!\n"); return 0; } CurrentSign = 0; CurrentLength = 0; } /* Zakonczenie programu jesli nie wczytano zadnych wyrazow. */ if (Words->First->Length == 0) return 0; /* Zabezpiecznie przed pustym ostatnim wyrazem. */ correctWords (Words); /* Sprawdza czy wszytkie slowa sa w stanie zmiescic sie, w ktorejkolwiek kolumn. */ for (Words->CurrentForward = Words->First; Words->CurrentForward != NULL; Words->CurrentForward = Words->CurrentForward->Next) if (Words->CurrentForward->Length > S1 && Words->CurrentForward->Length > S2) { printf ("At least one of words was to long!\n"); return 0; } return 1; }
// Add a word to the list. Here, we do not differentiate // the upper case and lower case. We will turn everything into // upper case void addWordToList(char * start, char * end) { char * cur = start; int i = 0; char word[1024]; int length; wordInfo_t * wordInfo; int index; index = (start - fileStart)/RECORD_LEN; // We are copying the word to a temporary buffer while(cur < end) { word[i] = toupper(*cur); // printf("%c", word[i]); cur++; i++; } // We do not need to handle the last ":" in a word char curr_ltr = toupper(*cur); if(curr_ltr >= 'A' && curr_ltr <= 'Z') { word[i] = toupper(*cur); // printf("%c", word[i]); i++; } word[i] = '\0'; // Now length is i length = i; // fprintf(stderr, "Check new word: %s\n", word); wordInfo = isWordExisting(word, length); if(wordInfo != NULL) { // Check whether this word is existing? // printf(" length %d, it is an existing word\n", i); // This word is already existing. addExistingWord(wordInfo, word, length, index); } else { // printf(" length %d, it is a new word\n", i); // Add this new word to the global list. addNewWord(word, length, index); } }
/** * @brief Gera e indexa o arquivo invertido */ void generateInvertedIndex() { ArvoreAVL<WordPtr> wordtree; // create empty file std::ofstream output(INVERTED_INDEX, std::ios::out | std::ios::binary); output.close(); std::ifstream input(MANPAGES, std::ios::in | std::ios::binary); if (!input) { throw std::runtime_error("Init::generateInvertedIndex: could not open file"); } ManPage manpage; std::streampos current_pos; int count = 0; while (input.good()) { if ((count % 100) == 0) std::cout << "Current manpage: " << count << std::endl; ++count; current_pos = input.tellg(); input.read((char*) &manpage, sizeof(ManPage)); auto wordset = listOfWords(manpage.conteudo); for (auto i = wordset.begin(); i != wordset.end(); ++i) { WordPtr wordptr; WordPtr* wordptrptr; bool found = true; strcpy(wordptr.word, (*i).c_str()); try { wordptrptr = wordtree.busca(wordptr); } catch (std::runtime_error& err) { found = false; } if (found) { addPageToWord(*wordptrptr, current_pos); } else { Word word; strcpy(word.word, (*i).c_str()); word.add(current_pos); wordptr.pos = addNewWord(word); wordtree.inserir(wordptr); } } } input.close(); wordtree.saveOnDisk(INDEX_TREE); }
void insert(Word *word){ if(checkBadLine())return; addNewWord(word); addLastWord(); }