summaryrefslogtreecommitdiff
path: root/docview/src/TextSearchQuery.pas
blob: 3a8037a2ce682cce77a476f70717f02fbd7d6094 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
Unit TextSearchQuery;

{$mode objfpc}{$H+}

// NewView - a new OS/2 Help Viewer
// Copyright 2003 Aaron Lawrence (aaronl at consultant dot com)
// This software is released under the Gnu Public License - see readme.txt

Interface

// Encapsulates a parsed search query.

uses
  Classes, SysUtils;

Type
  ESearchSyntaxError = class( Exception )
  end;

  TSearchTermCombineMethod =
  (
    cmOptional,
    cmRequired,
    cmExcluded
  );

  TSearchTerm = class(TObject)
  public
    Text: string;
    Parts: TStringList;
    CombineMethod: TSearchTermCombineMethod;
    constructor Create( const TheText: string;
                        const TheCombineMethod: TSearchTermCombineMethod );
    destructor Destroy; override;
  end;


  TTextSearchQuery = class(TObject)
  protected
    Terms: TList;
    function GetTerm( Index: longint ): TSearchTerm;
    function GetTermCount: longint;
  public
    constructor Create( const SearchString: string );
    destructor  Destroy; override;
    property    Term[ Index: longint ]: TSearchTerm read GetTerm;
    property    TermCount: longint read GetTermCount;
  end;


implementation

uses
  nvUtilities
  ,ACLStringUtility
  ;


const
  QueryErrorMissingWord1 = 'No search word given after';
  QueryErrorMissingWord2 = ' before ';


constructor TTextSearchQuery.Create( const SearchString: string );
var
  TermText: string;
  CombineMethod: TSearchTermCombineMethod;
  lTerm: TSearchTerm;
  tmpTerms : TStrings;
  i : integer;
begin
  inherited Create;
  Terms := TList.Create;
  try
    tmpTerms := TStringList.Create;
    StrExtractStringsQuoted(tmpTerms, SearchString);

    for i := 0 to tmpTerms.count-1 do
    begin
      TermText := tmpTerms[i];

      // Check for modifiers:
      //  + word must be matched
      //  - word must not be matched
      case TermText[ 1 ] of
       '+':
         CombineMethod := cmRequired;
       '-':
         CombineMethod := cmExcluded;
       else
         CombineMethod := cmOptional;
      end;

      if CombineMethod <> cmOptional then
      begin
        // delete + or -
        if Length( TermText ) = 1 then
          if (i < tmpTerms.count-1) then
            raise ESearchSyntaxError.Create( QueryErrorMissingWord1
                                             + StrInDoubleQuotes(TermText)
                                             + QueryErrorMissingWord2
                                             + StrInDoubleQuotes(tmpTerms[i+1]) )
          else
            raise ESearchSyntaxError.Create( QueryErrorMissingWord1
                                             + StrInDoubleQuotes(TermText));
        Delete( TermText, 1, 1 );
      end;

      lTerm := TSearchTerm.Create( TermText,
                                  CombineMethod );
      Terms.Add( lTerm );
    end;
    tmpTerms.Free;
  except
      while Terms.Count > 0 do
      begin
        lTerm := TSearchTerm(Terms.Last);
        Terms.Remove(lTerm);
        lTerm.Free;
      end;
      Terms.Free;
      raise; // reraise exception
  end;
end;

destructor TTextSearchQuery.Destroy;
begin
  DestroyListObjects( Terms );
  Terms.Free;
  inherited Destroy;
end;

function TTextSearchQuery.GetTerm( index: longint ): TSearchTerm;
begin
  Result := TSearchTerm(Terms[ Index ]);
end;

function TTextSearchQuery.GetTermCount: longint;
begin
  Result := Terms.Count;
end;

constructor TSearchTerm.Create( const TheText: string;
                                const TheCombineMethod: TSearchTermCombineMethod );
var
  TermParseIndex: longint;
  TermChar: char;
  TermPart: string;
begin
  Parts := TStringList.Create;

  Text := TheText;
  CombineMethod := TheCombineMethod;

  // Break out each part of the term as IPF does:
  // consecutive alphanumeric chars become a "word"
  // but each symbol is a separate word, and symbols break
  // up alphanumerics into multiple words. e.g.
  // CAKE_SAUSAGE becomes three words in IPF,
  // one each for "CAKE" "_" and "SAUSAGE"

  TermParseIndex := 1;
  while TermParseIndex <= Length( Text ) do
  begin
    // collect alphanumeric chars
    TermPart := '';
    while TermParseIndex <= Length( Text ) do
    begin
      TermChar := Text[ TermParseIndex ];
      if  (    IsAlpha( TermChar )
            or IsDigit( TermChar ) ) then
      begin
        // alpha numeric, collect it
        TermPart := TermPart + TermChar;
        inc( TermParseIndex );
      end
      else
      begin
        // not alpha numeric, so stop
        break;
      end;
    end;
    if Length( TermPart ) > 0 then
    begin
      Parts.Add( TermPart ); // add collected alphanumeric part
    end;

    if TermParseIndex <= Length( Text ) then
    begin
      // must be a symbol,
      // each symbol (excluding space) is an individual item
      if Text[ TermParseIndex ] <> ' ' then
        Parts.Add( Text[ TermParseIndex ] );
      inc( TermParseIndex );
    end;

  end;

end;

destructor TSearchTerm.Destroy;
begin
  Parts.Free;
  inherited Destroy;
end;


end.