summaryrefslogtreecommitdiff
path: root/docview/src/CompareWordUnit.pas
diff options
context:
space:
mode:
authorAndrew Haines <andrewd207@aol.com>2010-08-15 09:22:59 -0400
committerAndrew Haines <andrewd207@aol.com>2010-08-15 09:22:59 -0400
commit917a2daf4ff769ad27631e6c71a7b919c47e4ecb (patch)
treec524983404bd70c97c17395995f962cdf41899cc /docview/src/CompareWordUnit.pas
parent77245bbf79e8568ba143cd7654e8aba352253a81 (diff)
parent59df247d7a5ff46cc8ac697526510b2ff6cbe5d2 (diff)
downloadfpGUI-917a2daf4ff769ad27631e6c71a7b919c47e4ecb.tar.xz
Merge branch 'master' of ssh://fpgui.git.sourceforge.net/gitroot/fpgui/fpgui
Diffstat (limited to 'docview/src/CompareWordUnit.pas')
-rw-r--r--docview/src/CompareWordUnit.pas107
1 files changed, 107 insertions, 0 deletions
diff --git a/docview/src/CompareWordUnit.pas b/docview/src/CompareWordUnit.pas
new file mode 100644
index 00000000..49c2ea26
--- /dev/null
+++ b/docview/src/CompareWordUnit.pas
@@ -0,0 +1,107 @@
+Unit CompareWordUnit;
+
+{$mode objfpc}{$H+}
+
+// NewView - a new OS/2 Help Viewer
+// Copyright 2001 Aaron Lawrence (aaronl at consultant dot com)
+// This software is released under the Gnu Public License - see readme.txt
+
+Interface
+
+// Compares words and produces a match level (relevance) based
+// on the relative sizes etc. Used in searching help files
+// to sort by relevance.
+
+const
+ // word weightings
+ mwExactWord = 20;
+ mwWordStart = 10;
+ mwWordWithin = 5;
+
+// Compares the given search word against the given
+// reference word. Returns a value indicating how well the
+// search word matches, 0 = not at all.
+function CompareWord( const SearchWord: string;
+ const ReferenceWord: string ): longint;
+
+Implementation
+
+uses
+ SysUtils;
+
+// LOoks for string a within string b, case insensitively
+function CaseInsensitivePos( const a, b: string ): longint;
+begin
+ // Budget implementation to begin with.
+ Result := Pos( UpperCase( a ), UpperCase( b ) );
+end;
+
+function CompareWord( const SearchWord: string;
+ const ReferenceWord: string ): longint;
+var
+ OccurrencePos: longint;
+begin
+ Result := 0;
+ // First up, if the word we're searching for is longer than
+ // this word, then it can't match at all.
+ if Length( SearchWord ) > Length( ReferenceWord ) then
+ exit;
+
+ OccurrencePos := CaseInsensitivePos( SearchWord, ReferenceWord );
+ if OccurrencePos = 0 then
+ // no match.
+ exit;
+
+ if Length( SearchWord ) = Length( ReferenceWord ) then
+ begin
+ // exact word match (except case)
+ Result := mwExactWord;
+ exit;
+ end;
+
+ // partial word match
+ if OccurrencePos = 1 then
+ begin
+ // word starts with searchword
+ Result := mwWordStart
+ * Length( SearchWord )
+ div Length( ReferenceWord );
+ if Result = 0 then
+ Result := 1;
+ exit;
+ end;
+
+ // Matched searchword somewhere within word
+ Result := mwWordWithin
+ * Length( SearchWord )
+ div Length( ReferenceWord );
+ if Result = 0 then
+ Result := 1;
+
+end;
+
+{// Note: searchstring must be uppercase,
+function IsMatching( const SearchString: string;
+ const SearchType: TSearchType;
+ const Item: string ): boolean;
+var
+ temp: string;
+begin
+ case SearchType of
+ stStarts:
+ Result:= StrStarts( SearchString, Item );
+
+ stContains:
+ begin
+ temp:= UpperCase( Item );
+ Result:= Pos( SearchString, temp ) <> 0;
+ end;
+
+ stMatches:
+ Result:= CompareText( SearchString,
+ Item )= 0;
+ end;
+end;
+}
+Initialization
+End.