Child: [b0caf0] (diff)

Download this file

wasatorcl.cpp    156 lines (135 with data), 3.8 kB

  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
#ifndef lint
static char rcsid[] = "@(#$Id: wasatorcl.cpp,v 1.1 2006-11-30 18:12:16 dockes Exp $ (C) 2006 J.F.Dockes";
#endif
#ifndef TEST_WASATORCL
#include "wasastringtoquery.h"
#include "rcldb.h"
#include "searchdata.h"
#include "wasatorcl.h"
Rcl::SearchData *wasatorcl(WasaQuery *wasa)
{
if (wasa == 0)
return 0;
Rcl::SearchData *sdata = new Rcl::SearchData(Rcl::SCLT_AND);
WasaQuery::subqlist_t::iterator it;
for (it = wasa->m_subs.begin(); it != wasa->m_subs.end(); it++) {
switch ((*it)->m_op) {
case WasaQuery::OP_NULL:
case WasaQuery::OP_AND:
default:
// ??
continue;
case WasaQuery::OP_LEAF:
if ((*it)->m_value.find_first_of(" \t\n\r") != string::npos) {
sdata->addClause
(new Rcl::SearchDataClauseDist(Rcl::SCLT_PHRASE,
(*it)->m_value, 0));
} else {
sdata->addClause
(new Rcl::SearchDataClauseSimple(Rcl::SCLT_AND,
(*it)->m_value));
}
break;
case WasaQuery::OP_EXCL:
// Note: have to add dquotes which will be translated to
// phrase if there are several words in there. Not pretty
// but should work
sdata->addClause
(new Rcl::SearchDataClauseSimple(Rcl::SCLT_EXCL,
string("\"") +
(*it)->m_value + "\""));
break;
case WasaQuery::OP_OR:
// Concatenate all OR values as phrases. Hope there are no
// stray dquotes in there
{
string orvalue;
WasaQuery::subqlist_t::iterator orit;
for (orit = (*it)->m_subs.begin();
orit != (*it)->m_subs.end(); orit++) {
orvalue += string("\"") + (*orit)->m_value + "\"";
}
sdata->addClause
(new Rcl::SearchDataClauseSimple(Rcl::SCLT_OR,
orvalue));
}
}
}
return sdata;
}
#else // TEST
#ifdef HAVE_CONFIG_H
#include "autoconfig.h"
#endif
#include <stdio.h>
#include <signal.h>
#include <sys/stat.h>
#include <iostream>
#include <list>
#include <string>
using namespace std;
#include "debuglog.h"
#include "rclinit.h"
#include "rclconfig.h"
#include "rcldb.h"
#include "searchdata.h"
#include "refcntr.h"
#include "wasastringtoquery.h"
#include "wasatorcl.h"
static char *thisprog;
int main(int argc, char *argv[])
{
thisprog = argv[0];
argc--; argv++;
if (argc != 1) {
fprintf(stderr, "need one arg\n");
exit(1);
}
const string str = *argv++;argc--;
string reason;
RclConfig *config = recollinit(RCLINIT_NONE, 0, 0, reason, 0);
if (config == 0 || !config->ok()) {
cerr << "Configuration problem: " << reason << endl;
exit(1);
}
string dbdir = config->getDbDir();
if (dbdir.empty()) {
// Note: this will have to be replaced by a call to a
// configuration buildin dialog for initial configuration
cerr << "Configuration problem: " << "No dbdir" << endl;
exit(1);
}
Rcl::Db rcldb;
if (!rcldb.open(dbdir, Rcl::Db::DbRO, 0)) {
cerr << "Could not open database in " << dbdir << endl;
return 1;
}
StringToWasaQuery qparser;
WasaQuery *wq = qparser.stringToQuery(str, reason);
if (wq == 0) {
fprintf(stderr, "wasastringtoquery failed: %s\n", reason.c_str());
return 1;
}
string desc;
wq->describe(desc);
cout << endl << "Wasabi query description: " << desc << endl << endl;
Rcl::SearchData *sdata = wasatorcl(wq);
RefCntr<Rcl::SearchData> rq(sdata);
if (!rcldb.setQuery(rq)) {
cerr << "setQuery failed" << endl;
return 1;
}
int maxi = rcldb.getResCnt() > 10 ? 10 : rcldb.getResCnt();
cout << endl << "Rcl Query description: " << sdata->getDescription()
<< endl << endl << "Results: " << endl;
for (int i = 0; i < maxi ; i++) {
Rcl::Doc doc;
if (!rcldb.getDoc(i, doc)) {
cerr << "getDoc failed" << endl;
return 1;
}
cout << i << ": " << doc.url << endl;
}
return 0;
}
#endif // TEST_WASATORCL