Changeset 333


Ignore:
Timestamp:
Aug 31, 2008, 2:01:55 PM (17 years ago)
Author:
RBRi
Message:

search bug fix; more logging

Location:
branches/2.19_branch/NewView
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/2.19_branch/NewView/HelpTopic.pas

    r290 r333  
    14601460                                ): longint;
    14611461var
    1462   WordSequence: TList;
    1463   SequenceStepIndex: longint;
     1462  tmpWordSequence: TList;
     1463  tmpSequenceStepIndex: longint;
    14641464  pSequenceStepWords: Uint32ArrayPointer;
    14651465
     
    14761476  for SequenceIndex := 0 to WordSequences.Count - 1 do
    14771477  begin
    1478     WordSequence := WordSequences[ SequenceIndex ];
    1479     pSequenceStepWords := WordSequence[ 0 ];
     1478    tmpWordSequence := WordSequences[ SequenceIndex ];
     1479    pSequenceStepWords := tmpWordSequence[ 0 ];
    14801480
    14811481    if pSequenceStepWords^[ GlobalDictIndex ] > 0 then
    14821482    begin
    14831483      // matched first step in this sequence. Look ahead...
    1484 
    1485       SequenceStepIndex := 0;
     1484      tmpSequenceStepIndex := 1;
    14861485
    14871486      pDataTemp := pData;
    14881487      SlotIndexTemp := SlotIndex;
    14891488      StateTemp := State;
     1489
    14901490      while true do
    14911491      begin
    1492         inc( SequenceStepIndex );
    1493         if SequenceStepIndex = WordSequence.Count then
    1494         begin
    1495           // have a match for the sequence, insert start highlight
    1496           Result := WordSequence.Count;
     1492        if tmpSequenceStepIndex = tmpWordSequence.Count then
     1493        begin
     1494          // have a match for the sequence; time to leave
     1495          Result := tmpWordSequence.Count;
    14971496          break;
    14981497        end;
    14991498
    15001499        // get words for next step in sequence
    1501         pSequenceStepWords := WordSequence[ SequenceStepIndex ];
    1502 
    1503         DictIndex := GetNextIPFTextItem( SlotIndexTemp,
    1504                                          pDataTemp,
    1505                                          StateTemp );
     1500        pSequenceStepWords := tmpWordSequence[ tmpSequenceStepIndex ];
     1501
     1502        DictIndex := GetNextIPFTextItem( SlotIndexTemp, pDataTemp, StateTemp );
    15061503        if DictIndex = -2 then
    15071504        begin
     
    15181515
    15191516        // s := pstring( _GlobalDictionary[ DictIndex ] )^; // for debug only
     1517
     1518
     1519        // ignore whitespace
    15201520        if not StrIsEmptyOrSpaces( pstring( _GlobalDictionary[ DictIndex ] )^ ) then
    15211521        begin
     
    15251525            break;
    15261526          end;
     1527
     1528          // match; next one
     1529          inc(tmpSequenceStepIndex);
    15271530        end;
    15281531
     
    15331536  end; // for sequenceindex ...
    15341537end;
     1538
    15351539
    15361540// Main translation function. Turns the IPF data into
     
    16371641
    16381642        if ShowWordSeparators then
     1643        begin
    16391644          Text.AddString( '{' + IntToStr( GlobalDictIndex )+ '}' );
     1645        end;
    16401646
    16411647        // normal lookup
     
    16561662          // store string into "word"
    16571663          if State.TextBlock.Length = 0 then
     1664          begin
    16581665            // store start of block
    16591666            State.StartOfTextBlock := Text.Length;
     1667          end;
    16601668
    16611669          State.TextBlock.AddString( StringToAdd );
     
    16721680              begin
    16731681                // now finished, insert end highlight
    1674                 StringToAdd := StringToAdd
    1675                                + State.BackgroundColorTag;
    1676 
     1682                StringToAdd := StringToAdd + State.BackgroundColorTag;
    16771683              end;
    16781684            end
     
    16991705                if SequenceStepIndex = 0 then
    17001706                  // and ends it.
    1701                   StringToAdd := StringToAdd
    1702                            + State.BackgroundColorTag;
     1707                  StringToAdd := StringToAdd + State.BackgroundColorTag;
    17031708              end;
    17041709
     
    18211826  end;
    18221827  State.TextBlock.Destroy;
    1823 
    1824 end;
     1828end;
     1829
    18251830
    18261831function TTopic.SearchForWord( DictIndex: integer;
  • branches/2.19_branch/NewView/SearchUnit.pas

    r312 r333  
    124124                            Results: UInt32ArrayPointer );
    125125var
    126   DictIndex: integer;
     126  tmpDictIndex: integer;
    127127  pDictWord: pstring;
    128128begin
    129   for DictIndex := 0 to HelpFile.DictionaryCount - 1 do
    130   begin
    131     pDictWord := HelpFile.DictionaryWordPtrs[ DictIndex ];
    132     Results[ DictIndex ] := CompareWord( SearchWord,
    133                                          pDictWord^ );
     129  for tmpDictIndex := 0 to HelpFile.DictionaryCount - 1 do
     130  begin
     131    pDictWord := HelpFile.DictionaryWordPtrs[ tmpDictIndex ];
     132    Results[ tmpDictIndex ] := CompareWord( SearchWord, pDictWord^ );
    134133  end;
    135134end;
     
    163162  DictWord: string;
    164163begin
     164  if IsLogAspectsEnabled(LogSearch) then
     165  begin
     166    LogEvent(LogSearch, '  calling SearchDictionaryStarts "' + SearchWord + '"');
     167  end;
     168 
    165169  FillUInt32Array( Results, HelpFile.DictionaryCount, 0 );
    166170
     
    169173    DictWord := HelpFile.DictionaryWords[ DictIndex ];
    170174    if StrStartsWithIgnoringCase(DictWord, SearchWord) then
    171       Results[ DictIndex ] := MatchedWordRelevance( SearchWord, DictWord );
    172   end;
    173 end;
     175    begin
     176      Results[ DictIndex ] := MatchedWordRelevance( SearchWord, DictWord )
     177    end;
     178  end;
     179end;
     180
    174181
    175182// Search the help file dictionary for words that
     
    182189  DictWord: string;
    183190begin
     191  if IsLogAspectsEnabled(LogSearch) then
     192  begin
     193    LogEvent(LogSearch, ' calling SearchDictionaryEnds for "' + SearchWord + '"');
     194  end;
    184195  FillUInt32Array( Results, HelpFile.DictionaryCount, 0 );
    185196
     
    188199    DictWord := HelpFile.DictionaryWords[ DictIndex ];
    189200    if StrEndsWithIgnoringCase(DictWord, SearchWord ) then
     201    begin
    190202      Results[ DictIndex ] := MatchedWordRelevance( SearchWord, DictWord );
     203    end;
    191204  end;
    192205end;
     
    207220  i : integer;
    208221begin
     222  if IsLogAspectsEnabled(LogSearch) then
     223  begin
     224    LogEvent(LogSearch, ' calling SearchTopicTitles for "' + SearchWord + '"');
     225  end;
     226
    209227  tmpTitleWords := TStringList.Create;
    210228
     
    223241      TitleWord := tmpTitleWords[i];
    224242
    225       WordRelevance := CompareWord( SearchWord,
    226                                     TitleWord );
     243      WordRelevance := CompareWord( SearchWord, TitleWord );
    227244      if WordRelevance > 0 then
    228245      begin
     
    233250          begin
    234251            // in fact it's the only word
    235             TitleWordRelevance := mwOnlyTitleWord
    236                                   * WordRelevance
     252            TitleWordRelevance := mwOnlyTitleWord * WordRelevance
    237253          end
    238254          else
    239             TitleWordRelevance := mwFirstTitleWord
    240                                   * WordRelevance
     255            TitleWordRelevance := mwFirstTitleWord * WordRelevance
    241256        end
    242257        else
    243258        begin
    244           TitleWordRelevance := mwTitleWord
    245                                 * WordRelevance;
     259          TitleWordRelevance := mwTitleWord * WordRelevance;
    246260        end;
    247261        inc( Results[ TopicIndex ],
     
    269283  i : integer;
    270284begin
     285  if IsLogAspectsEnabled(LogSearch) then
     286  begin
     287    LogEvent(LogSearch, ' calling SearchIndex for "' + SearchWord + '"');
     288  end;
     289 
    271290  tmpIndexWords := TStringList.Create;
    272291
     
    284303      IndexEntryWord := tmpIndexWords[i];
    285304
    286       WordRelevance := CompareWord( SearchWord,
    287                                     IndexEntryWord );
     305      WordRelevance := CompareWord( SearchWord, IndexEntryWord );
    288306      if WordRelevance > 0 then
    289307      begin
     
    294312          begin
    295313            // in fact it's the only word
    296             IndexEntryWordRelevance := mwOnlyIndexWord
    297                                        * WordRelevance
     314            IndexEntryWordRelevance := mwOnlyIndexWord * WordRelevance
    298315          end
    299316          else
    300             IndexEntryWordRelevance := mwFirstIndexWord
    301                                     * WordRelevance
     317            IndexEntryWordRelevance := mwFirstIndexWord * WordRelevance
    302318        end
    303319        else
    304320        begin
    305           IndexEntryWordRelevance := mwIndexWord
    306                                   * WordRelevance;
     321          IndexEntryWordRelevance := mwIndexWord * WordRelevance;
    307322        end;
    308323        inc( Results[ Topic.Index ],
     
    327342                          WordSequences: TList );
    328343var
    329   TopicCount: longint;
    330   Topic: TTopic;
    331   TopicIndex: longint;
    332   TermIndex: longint;
    333   Term: TSearchTerm;
     344  tmpTopicCount: longint;
     345  tmpTopic: TTopic;
     346  tmpTopicIndex: longint;
     347  tmpTermIndex: longint;
     348  tmpTerm: TSearchTerm;
    334349
    335350  DictionaryRelevances: UInt32ArrayPointer;
     
    353368  TermWordSequence: TList;
    354369begin
     370  LogEvent(LogSearch, 'SearchHelpFile');
     371  Query.Log;
     372
    355373  if HelpFile.SearchTable = nil then
    356374  begin
     
    359377
    360378  // Reset flags per topic
    361   TopicCount := HelpFile.TopicCount;
     379  tmpTopicCount := HelpFile.TopicCount;
    362380
    363381  // Get memory for topic relevance arrays
    364382
    365   AllocUInt32Array( TopicsMatchingDictWord,
    366                     TopicCount );
    367   AllocUInt32Array( TopicsMatchingTermPart,
    368                     TopicCount );
    369   AllocUInt32Array( TopicsMatchingTerm,
    370                     TopicCount );
    371   AllocUInt32Array( TopicRelevances,  // functions as a flag and a cumulative relevance
    372                     TopicCount );
    373   AllocUInt32Array( TopicsExcluded, // Exclusions are treated as boolean only
    374                     TopicCount );
    375 
    376   ClearUInt32Array( TopicRelevances,
    377                     TopicCount );
    378   ClearUInt32Array( TopicsExcluded,
    379                     TopicCount );
    380 
    381   for TermIndex := 0 to Query.TermCount - 1 do
    382   begin
    383     Term := Query.Term[ TermIndex ];
    384 
    385     LogEvent(LogSearch, 'Searching for term "'
    386                   + Term.Text
     383  AllocUInt32Array( TopicsMatchingDictWord, tmpTopicCount );
     384  AllocUInt32Array( TopicsMatchingTermPart, tmpTopicCount );
     385  AllocUInt32Array( TopicsMatchingTerm, tmpTopicCount );
     386  AllocUInt32Array( TopicRelevances, tmpTopicCount ); // functions as a flag and a cumulative relevance
     387
     388  AllocUInt32Array( TopicsExcluded, tmpTopicCount ); // Exclusions are treated as boolean only
     389
     390
     391  ClearUInt32Array( TopicRelevances, tmpTopicCount );
     392  ClearUInt32Array( TopicsExcluded, tmpTopicCount );
     393
     394  for tmpTermIndex := 0 to Query.TermCount - 1 do
     395  begin
     396    tmpTerm := Query.Term[ tmpTermIndex ];
     397
     398    if IsLogAspectsEnabled(LogSearch) then
     399    begin
     400      LogEvent(LogSearch, 'Searching for term "'
     401                  + tmpTerm.Text
    387402                  + '", '
    388                   + IntToStr( Term.Parts.Count )
     403                  + IntToStr( tmpTerm.Parts.Count )
    389404                  + ' parts' );
     405    end;
    390406
    391407    // look thru all parts of the term.           eg. CAKE_SAUSAGE
     
    394410
    395411    if WordSequences <> nil then
    396       if Term.CombineMethod <> cmExcluded then
     412      if tmpTerm.CombineMethod <> cmExcluded then
     413      begin
    397414        // this term is an inclusive one, so we want to remember the matches
    398415        WordSequences.Add( TermWordSequence );
    399 
    400     for TermPartIndex := 0 to Term.Parts.Count - 1 do
    401     begin
    402       TermPart := Term.Parts[ TermPartIndex ];
    403 
    404       LogEvent(LogSearch, '  Searching for [' + TermPart + ']' );
    405 
    406       AllocUInt32Array( DictionaryRelevances,
    407                         HelpFile.DictionaryCount );
     416      end;
     417
     418    for TermPartIndex := 0 to tmpTerm.Parts.Count - 1 do
     419    begin
     420      TermPart := tmpTerm.Parts[ TermPartIndex ];
     421
     422      if IsLogAspectsEnabled(LogSearch) then
     423      begin
     424        LogEvent(LogSearch, '  Searching for TermPart [' + TermPart + ']' );
     425      end;
     426
     427      AllocUInt32Array( DictionaryRelevances, HelpFile.DictionaryCount );
    408428
    409429      TermWordSequence.Add( DictionaryRelevances );
     
    412432      // alpha numeric match
    413433
    414       if Term.Parts.Count = 1 then
     434      if tmpTerm.Parts.Count = 1 then
     435      begin
     436        if IsLogAspectsEnabled(LogSearch) then
     437        begin
     438          LogEvent(LogSearch, '  Term has only one part...' );
     439          LogEvent(LogSearch, '  SearchDictionary [' + TermPart + ']' );
     440        end;
     441
    415442        // general match allowing all kinds of partial matches
    416         SearchDictionary( HelpFile,
    417                           TermPart,
    418                           DictionaryRelevances )
     443        SearchDictionary( HelpFile, TermPart, DictionaryRelevances )
     444      end
    419445
    420446      else if TermPartIndex = 0 then
     447      begin
     448        if IsLogAspectsEnabled(LogSearch) then
     449        begin
     450          LogEvent(LogSearch, '  Term has more then one part... we are at first' );
     451          LogEvent(LogSearch, '  SearchDictionaryEnd [' + TermPart + ']' );
     452        end;
     453
    421454        // first term part: word must match end of a topic word e.g. must end in "cake"
    422         SearchDictionaryEnds( HelpFile,
    423                               TermPart,
    424                               DictionaryRelevances )
    425 
    426       else if TermPartIndex = Term.Parts.Count - 1 then
     455        SearchDictionaryEnds( HelpFile, TermPart, DictionaryRelevances )
     456      end
     457
     458      else if TermPartIndex = tmpTerm.Parts.Count - 1 then
     459      begin
     460        if IsLogAspectsEnabled(LogSearch) then
     461        begin
     462          LogEvent(LogSearch, '  Term has more then one part... we are at last' );
     463          LogEvent(LogSearch, '  SearchDictionaryEnd [' + TermPart + ']' );
     464        end;
     465
    427466        // last term part: word must match start of a topic word e.g. must start with "sausage"
    428         SearchDictionaryStarts( HelpFile,
    429                                 TermPart,
    430                                 DictionaryRelevances )
     467        SearchDictionaryStarts( HelpFile, TermPart, DictionaryRelevances )
     468      end
    431469
    432470      else
     471      begin
     472        if IsLogAspectsEnabled(LogSearch) then
     473        begin
     474          LogEvent(LogSearch, '  Term has more then one part... we are inside' );
     475          LogEvent(LogSearch, '  SearchDictionaryEnd [' + TermPart + ']' );
     476        end;
     477
    433478        // intermediate term part: word must match exactly  e.g. must be "_"
    434         SearchDictionaryExact( HelpFile,
    435                                TermPart,
    436                                DictionaryRelevances );
     479        SearchDictionaryExact( HelpFile, TermPart, DictionaryRelevances )
     480      end;
    437481
    438482      // For each word in the dictionary that matches
     
    440484
    441485      LogEvent(LogSearch, '  Dictionary search done' );
    442       ClearUInt32Array( TopicsMatchingTermPart,
    443                         TopicCount );
     486      ClearUInt32Array( TopicsMatchingTermPart, tmpTopicCount );
    444487
    445488      for DictIndex := 0 to HelpFile.DictionaryCount - 1 do
     
    460503          OrUInt32Array( TopicsMatchingDictWord,
    461504                         TopicsMatchingTermPart,
    462                          TopicCount );
     505                         tmpTopicCount );
    463506        end
    464507      end;
    465508
    466       LogEvent(LogSearch, 'Topic searches done' );
     509      LogEvent(LogSearch, '  Topic searches done' );
    467510
    468511      if TermPartIndex = 0 then
     
    470513        CopyUInt32Array( TopicsMatchingTermPart,
    471514                         TopicsMatchingTerm,
    472                          TopicCount )
     515                         tmpTopicCount )
    473516      else
    474517        // and with previous term part results
    475518        AndUInt32Array( TopicsMatchingTermPart,
    476519                        TopicsMatchingTerm,
    477                         TopicCount );
     520                        tmpTopicCount );
    478521
    479522      // loop for next term part (IPF word)
     
    484527
    485528    LogEvent(LogSearch, 'Checking for sequences' );
    486     for TopicIndex := 0 to TopicCount - 1 do
    487     begin
    488       if TopicsMatchingTerm[ TopicIndex ] > 0 then
    489       begin
    490         Topic := HelpFile.Topics[ TopicIndex ];
     529    for tmpTopicIndex := 0 to tmpTopicCount - 1 do
     530    begin
     531      if TopicsMatchingTerm[ tmpTopicIndex ] > 0 then
     532      begin
     533        tmpTopic := HelpFile.Topics[ tmpTopicIndex ];
    491534        // Topic text contained a match for the all the parts
    492535        // of the term.
     
    495538        // - count occurrences for relevance.
    496539
    497         TopicRelevanceForTerm :=
    498           Topic.SearchForWordSequences( TermWordSequence,
    499                                         false ); // don't stop at first match
    500 
    501         TopicRelevanceForTerm :=
    502           TopicRelevanceForTerm div Term.Parts.Count; // divide to bring back into scale
    503 
    504         TopicsMatchingTerm[ TopicIndex ] := TopicRelevanceForTerm;
     540        TopicRelevanceForTerm := tmpTopic.SearchForWordSequences( TermWordSequence, false ); // don't stop at first match
     541
     542        TopicRelevanceForTerm := TopicRelevanceForTerm div tmpTerm.Parts.Count; // divide to bring back into scale
     543
     544        TopicsMatchingTerm[ tmpTopicIndex ] := TopicRelevanceForTerm;
    505545
    506546      end;
     
    518558
    519559    LogEvent(LogSearch, '  Searching titles' );
    520     SearchTopicTitles( HelpFile, Term.Text, TopicsMatchingTerm );
     560    SearchTopicTitles( HelpFile, tmpTerm.Text, TopicsMatchingTerm );
    521561
    522562    LogEvent(LogSearch, '  Searching index' );
    523     SearchIndex( HelpFile, Term.Text, TopicsMatchingTerm );
     563    SearchIndex( HelpFile, tmpTerm.Text, TopicsMatchingTerm );
    524564
    525565    LogEvent(LogSearch, '  Combining' );
    526     case Term.CombineMethod of
     566    case tmpTerm.CombineMethod of
    527567      cmOptional:
     568      begin
     569        LogEvent(LogSearch, '  Combining optional');
    528570        AddUInt32Array( TopicsMatchingTerm,
    529571                        TopicRelevances,
    530                         TopicCount );
     572                        tmpTopicCount );
     573      end;
    531574
    532575      cmRequired:
    533576      begin
     577        LogEvent(LogSearch, '  Combining required');
    534578        // if zero then add to exclusions
    535579        NotOrUInt32Array( TopicsMatchingTerm,
    536580                          TopicsExcluded,
    537                           TopicCount );
     581                          tmpTopicCount );
    538582
    539583        AddUInt32Array( TopicsMatchingTerm,
    540584                        TopicRelevances,
    541                         TopicCount );
     585                        tmpTopicCount );
    542586      end;
    543587
    544588      cmExcluded:
     589      begin
     590        LogEvent(LogSearch, '  Combining excluded');
    545591        OrUInt32Array( TopicsMatchingTerm,
    546592                       TopicsExcluded,
    547                        TopicCount );
     593                       tmpTopicCount );
     594      end;
    548595    end;
    549596
     
    557604  // Now convert to list form.
    558605
    559   for TopicIndex := 0 to TopicCount - 1 do
    560   begin
    561     if TopicsExcluded[ TopicIndex ] = 0 then
    562     begin
    563       Topic := HelpFile.Topics[ TopicIndex ];
    564       Topic.SearchRelevance := TopicRelevances[ TopicIndex ];
    565       if Topic.SearchRelevance > 0 then
    566       begin
    567         Results.Add( Topic );
     606  for tmpTopicIndex := 0 to tmpTopicCount - 1 do
     607  begin
     608    if TopicsExcluded[ tmpTopicIndex ] = 0 then
     609    begin
     610      tmpTopic := HelpFile.Topics[ tmpTopicIndex ];
     611      tmpTopic.SearchRelevance := TopicRelevances[ tmpTopicIndex ];
     612      if tmpTopic.SearchRelevance > 0 then
     613      begin
     614        Results.Add( tmpTopic );
    568615      end;
    569616    end;
     
    571618
    572619  LogEvent(LogSearch, 'Freeing arrays' );
    573   FreeUInt32Array( TopicRelevances, TopicCount );
    574   FreeUInt32Array( TopicsExcluded, TopicCount );
    575   FreeUInt32Array( TopicsMatchingTerm, TopicCount );
    576   FreeUInt32Array( TopicsMatchingTermPart, TopicCount );
    577   FreeUInt32Array( TopicsMatchingDictWord, TopicCount );
     620  FreeUInt32Array( TopicRelevances, tmpTopicCount );
     621  FreeUInt32Array( TopicsExcluded, tmpTopicCount );
     622  FreeUInt32Array( TopicsMatchingTerm, tmpTopicCount );
     623  FreeUInt32Array( TopicsMatchingTermPart, tmpTopicCount );
     624  FreeUInt32Array( TopicsMatchingDictWord, tmpTopicCount );
    578625
    579626  LogEvent(LogSearch, 'Done' );
Note: See TracChangeset for help on using the changeset viewer.