public uint allocateIdForDocument(IxDocument document, IxTokenizer documentTokenizer) { uint documentId; lock (nextDocumentId) { /* Pobierz ID dla dokumentu */ documentId = nextDocumentId.value++; /* Dodaj dokument do indeksu dokumentów */ documentsIndex.addDocument(document, documentId); /* Dodaj spis tokenów do indeksu wprzód (jeśli tworzony) */ if (IxSettings.createFwdIndex) fwdIndex.addDocumentTokenList(documentTokenizer.getEncounteredTokens()); } return documentId; }
/// <summary> /// Indeksuje pojedynczy dokument /// /// Tokenizuje tekst oraz tytuł (w tym czasie nowym tokenom w słownikach są nadawane ID), pobiera ID dodkumentu z alokatora /// sharedData.docIdAllocator (dokument dodawany jest do spisu dokumentów IIxDocumentsIndex i ew. do indeksu wprzód), /// obliczane są wagi tokenów i informacje o wystąpieniach tokenów są przekazywane do indexWriter. /// </summary> /// <param name="document">Dokument do zaindeksowania</param> public void indexPage(IxDocument document) { /* Wykonaj listę tokenów w tekście dokumentu */ textTokensListing.reset(); textTokensListing.addFromText(document.text, true); Dictionary<uint, uint> textTokensWithCount = textTokensListing.getTokensWithCount(); /* Wykonaj listę tokenów w tytule dokumentu */ titleTokensListing.reset(); titleTokensListing.addFromText(document.title, false); Dictionary<uint, uint> titleTokensWithCount = titleTokensListing.getTokensWithCount(); /* Pobierz ID dla dokumentu; tutaj dokument zostanie dodany do spisu i ew. indeksu wprzód */ uint documentId = sharedData.docIdAllocator.allocateIdForDocument(document, textTokensListing); /* Liczba tokenów w tytule i tekście, dla przyspieszenia obliczeń w float */ float titleTokensTotal = 0f, textTokensTotal = 0f; /* Zlicz tokeny dla tytułu */ foreach (KeyValuePair<uint, uint> tokenWithCount in titleTokensWithCount) titleTokensTotal += (float)tokenWithCount.Value; /* Zlicz tokeny dla tekstu */ foreach (KeyValuePair<uint, uint> tokenWithCount in textTokensWithCount) textTokensTotal += (float)tokenWithCount.Value; /* Dodaj wpisy tokenów tytułu do indeksu */ foreach (KeyValuePair<uint, uint> tokenWithCount in titleTokensWithCount) { /* Waga tokenu dla dokumentu */ float myValue; /* Wartość tokenu w tytule pomnożona razy dwa */ myValue = (float)(2f * (float)tokenWithCount.Value / titleTokensTotal); /* Jeśli tekst dokumentu zawiera ten token, dodaj do jego wagi odpowiednią wagę z tekstu */ if (textTokensWithCount.ContainsKey(tokenWithCount.Key)) { myValue += (float)((float)textTokensWithCount[tokenWithCount.Key] / textTokensTotal); } /* Dodaj wpis dla tokenu do indeksu */ indexWriter.addAssociacion(documentId, tokenWithCount.Key, myValue); } foreach (KeyValuePair<uint, uint> tokenWithCount in textTokensWithCount) { /* Jeśli token wystąpił już w tytule, nie dodawaj - już został dodany do indeksu */ if (titleTokensWithCount.ContainsKey(tokenWithCount.Key)) continue; /* Waga tokenu dla dokumentu */ float myValue; /* Wartość tokenu w tytule */ myValue = (float)((float)tokenWithCount.Value / textTokensTotal); /* Dodaj wpis dla tokenu do indeksu */ indexWriter.addAssociacion(documentId, tokenWithCount.Key, myValue); } /* Jeśli trzeba, raportuj postęp */ if (documentId % 1000 == 0) { Console.WriteLine("{0} dokumentów, {1}, {2:0.00} dok./sek.", documentId, sharedData.stopwatch.Elapsed, 1000 / sharedData.stopwatch.Elapsed.TotalSeconds); sharedData.stopwatch.Reset(); sharedData.stopwatch.Start(); } }
public IxDocument getDocumentById(uint documentId) { if (lastIdDocumentRead >= documentId || documentId - lastIdDocumentRead > IxSettings.stdDiskDocumentsIndexOmitDifference) { /* Oblicz położenie wskaźnika w pliku indeksu na podstawie ID i interwału indeksu, przesuń wskaźnik na indeksie */ long indexIndexOffset = ((long)(documentId / indexInterval)) * sizeof(long); documentsIndexFileReader.BaseStream.Seek(indexIndexOffset, SeekOrigin.Begin); /* Wczytaj offset pliku danych z indeksu i przesuń wskaźnik na pliku danych */ long indexOffset = documentsIndexFileReader.ReadInt64(); documentsDataFileReader.BaseStream.Seek(indexOffset, SeekOrigin.Begin); } IxDocument result = new IxDocument(); try { /* Wczytaj kolejne dokumenty, aż trafisz na właściwe ID */ uint readDocumentId; do { readDocumentId = documentsDataFileReader.ReadUInt32(); result.title = documentsDataFileReader.ReadString(); } while (readDocumentId != documentId); } catch (EndOfStreamException) { lastIdDocumentRead = 0; return null; } lastIdDocumentRead = documentId; return result; }
/// <summary> /// Dodaje dokument do kolejki /// /// Blokuje, jeśli nie ma miejsca w buforze, aż się zwolni. /// </summary> /// <param name="document">Dokument do dodania do kolejki</param> public void addDocument(IxDocument document) { buffer.put(document); }
public void addDocument(IxDocument document, uint documentId) { lock (this) { /* Jeśli trzeba, dodaj nowy wpis w indeksie. Dzięki umiejscowieniu go tak, nie będziemy otrzymywać wpisów wskazujących * na koniec pliku */ if (indexIntervalCounter == indexInterval) { documentsIndexFileWriter.Write((long)documentsDataFileWriter.BaseStream.Position); indexIntervalCounter = 0; } documentsDataFileWriter.Write((uint)documentId); documentsDataFileWriter.Write(document.title); indexIntervalCounter++; } }