1 //
2 // OrFuzzyExpander.cc
3 //
4 // OrFuzzyExpander: a concrete Fuzzy expander that makes a OR with
5 //                  all the results returned by the applicable Fuzzies.
6 //
7 // Part of the ht://Dig package   <http://www.htdig.org/>
8 // Copyright (c) 1995-2004 The ht://Dig Group
9 // For copyright details, see the file COPYING in your distribution
10 // or the GNU Library General Public License (LGPL) version 2 or later
11 // <http://www.gnu.org/copyleft/lgpl.html>
12 //
13 // $Id: OrFuzzyExpander.cc,v 1.4 2004/05/28 13:15:24 lha Exp $
14 //
15 
16 #include "OrFuzzyExpander.h"
17 #include "Dictionary.h"
18 #include "ExactWordQuery.h"
19 #include "OrQuery.h"
20 
21 extern int debug;
22 
23 //
24 // creates a query with a OrQuery with all the
25 // distinct fuzzy results
26 //
27 // additionally, sets fuzzy scores for used words
28 //
29 Query *
MakeQuery(const String & word)30 OrFuzzyExpander::MakeQuery(const String &word)
31 {
32 	Query *result = 0;
33 	Dictionary exacts;
34 
35 	// for each configured fuzzy
36 	filters.Start_Get();
37 	Fuzzy *fuzzy = (Fuzzy *)filters.Get_Next();
38 	while(fuzzy)
39 	{
40 		// for each word expanded by fuzzy
41 		List words;
42 		String nonconst = word;
43 		fuzzy->getWords(nonconst, words);
44 		words.Start_Get();
45 		String *w = (String *)words.Get_Next();
46 		while(w)
47 		{
48 			// if not yet expanded by another fuzzy
49 			// add it to the big Or
50 			if(debug) cerr << "fuzzy " << word << "=" << *w << endl;
51 			ExactWordQuery *exact = (ExactWordQuery *)exacts[*w];
52 			if(!exact)
53 			{
54 				exact = new ExactWordQuery(*w);
55 				exact->SetWeight(fuzzy->getWeight());
56 				exacts.Add(*w, exact);
57 			}
58 			// otherwise, just adjust the weight
59 			else
60 			{
61 				exact->SetWeight(
62 						exact->GetWeight() +
63 						fuzzy->getWeight());
64 			}
65 			w = (String *)words.Get_Next();
66 		}
67 		fuzzy = (Fuzzy *)filters.Get_Next();
68 	}
69 
70 	// return the expanded query
71 	// a single word or
72 	// a Or with all the expanded words
73 	exacts.Start_Get();
74 	Query *exact = (Query *)exacts.Get_NextElement();
75 	if(exact)
76 	{
77 		result = exact;
78 		exact = (Query *)exacts.Get_NextElement();
79 	}
80 	if(exact)
81 	{
82 		Query *tmp = result;
83 		result = new OrQuery;
84 		result->Add(tmp);
85 		while(exact)
86 		{
87 			result->Add(exact);
88 			exact = (Query *)exacts.Get_NextElement();
89 		}
90 	}
91 	exacts.Release();
92 
93 	return result;
94 }
95