arrange to enable passing a match term as an argument when opening a document with the native editor (only / mostly pdf for now)
This commit is contained in:
@@ -19,6 +19,9 @@
|
||||
#include <math.h>
|
||||
|
||||
#include <map>
|
||||
#include <tr1/unordered_set>
|
||||
using std::tr1::unordered_set;
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include "debuglog.h"
|
||||
@@ -204,11 +207,11 @@ double Query::Native::qualityTerms(Xapian::docid docid,
|
||||
}
|
||||
|
||||
// Return page number for first match of "significant" term.
|
||||
int Query::Native::getFirstMatchPage(Xapian::docid docid)
|
||||
int Query::Native::getFirstMatchPage(Xapian::docid docid, string& term)
|
||||
{
|
||||
if (!m_q|| !m_q->m_db || !m_q->m_db->m_ndb || !m_q->m_db->m_ndb->m_isopen) {
|
||||
LOGERR(("Query::getFirstMatchPage: no db\n"));
|
||||
return false;
|
||||
return -1;
|
||||
}
|
||||
Rcl::Db::Native *ndb(m_q->m_db->m_ndb);
|
||||
Xapian::Database& xrdb(ndb->xrdb);
|
||||
@@ -246,8 +249,10 @@ int Query::Native::getFirstMatchPage(Xapian::docid docid)
|
||||
for (pos = xrdb.positionlist_begin(docid, qterm);
|
||||
pos != xrdb.positionlist_end(docid, qterm); pos++) {
|
||||
int pagenum = ndb->getPageNumberForPosition(pagepos, *pos);
|
||||
if (pagenum > 0)
|
||||
if (pagenum > 0) {
|
||||
term = qterm;
|
||||
return pagenum;
|
||||
}
|
||||
}
|
||||
} catch (...) {
|
||||
// Term does not occur. No problem.
|
||||
@@ -263,7 +268,7 @@ int Query::Native::getFirstMatchPage(Xapian::docid docid)
|
||||
// DatabaseModified and other general exceptions are catched and
|
||||
// possibly retried by our caller
|
||||
abstract_result Query::Native::makeAbstract(Xapian::docid docid,
|
||||
vector<pair<int, string> >& vabs,
|
||||
vector<Snippet>& vabs,
|
||||
int imaxoccs, int ictxwords)
|
||||
{
|
||||
Chrono chron;
|
||||
@@ -315,6 +320,9 @@ abstract_result Query::Native::makeAbstract(Xapian::docid docid,
|
||||
// The terms 'array' that we partially populate with the document
|
||||
// terms, at their positions around the search terms positions:
|
||||
map<unsigned int, string> sparseDoc;
|
||||
// Also remember apart the search term positions so that we can list
|
||||
// them with their snippets.
|
||||
unordered_set<unsigned int> searchTermPositions;
|
||||
|
||||
// Total number of occurences for all terms. We stop when we have too much
|
||||
unsigned int totaloccs = 0;
|
||||
@@ -392,6 +400,7 @@ abstract_result Query::Native::makeAbstract(Xapian::docid docid,
|
||||
for (unsigned int ii = sta; ii <= sto; ii++) {
|
||||
if (ii == (unsigned int)ipos) {
|
||||
sparseDoc[ii] = qterm;
|
||||
searchTermPositions.insert(ii);
|
||||
} else if (ii > (unsigned int)ipos &&
|
||||
ii < (unsigned int)ipos + qtrmwrdcnt) {
|
||||
sparseDoc[ii] = occupiedmarker;
|
||||
@@ -470,7 +479,7 @@ abstract_result Query::Native::makeAbstract(Xapian::docid docid,
|
||||
break;
|
||||
}
|
||||
map<unsigned int, string>::iterator vit;
|
||||
if ((vit=sparseDoc.find(*pos)) != sparseDoc.end()) {
|
||||
if ((vit = sparseDoc.find(*pos)) != sparseDoc.end()) {
|
||||
// Don't replace a term: the terms list is in
|
||||
// alphabetic order, and we may have several terms
|
||||
// at the same position, we want to keep only the
|
||||
@@ -513,6 +522,7 @@ abstract_result Query::Native::makeAbstract(Xapian::docid docid,
|
||||
string chunk;
|
||||
bool incjk = false;
|
||||
int page = 0;
|
||||
string term;
|
||||
for (map<unsigned int, string>::const_iterator it = sparseDoc.begin();
|
||||
it != sparseDoc.end(); it++) {
|
||||
LOGDEB2(("Abtract:output %u -> [%s]\n", it->first,it->second.c_str()));
|
||||
@@ -522,6 +532,7 @@ abstract_result Query::Native::makeAbstract(Xapian::docid docid,
|
||||
page = ndb->getPageNumberForPosition(vpbreaks, it->first);
|
||||
if (page < 0)
|
||||
page = 0;
|
||||
term.clear();
|
||||
}
|
||||
Utf8Iter uit(it->second);
|
||||
bool newcjk = false;
|
||||
@@ -530,8 +541,10 @@ abstract_result Query::Native::makeAbstract(Xapian::docid docid,
|
||||
if (!incjk || (incjk && !newcjk))
|
||||
chunk += " ";
|
||||
incjk = newcjk;
|
||||
if (searchTermPositions.find(it->first) != searchTermPositions.end())
|
||||
term = it->second;
|
||||
if (it->second == cstr_ellipsis) {
|
||||
vabs.push_back(pair<int,string>(page, chunk));
|
||||
vabs.push_back(Snippet(page, chunk).setTerm(term));
|
||||
chunk.clear();
|
||||
} else {
|
||||
if (it->second.compare(end_of_field_term) &&
|
||||
@@ -540,7 +553,7 @@ abstract_result Query::Native::makeAbstract(Xapian::docid docid,
|
||||
}
|
||||
}
|
||||
if (!chunk.empty())
|
||||
vabs.push_back(pair<int, string>(page, chunk));
|
||||
vabs.push_back(Snippet(page, chunk).setTerm(term));
|
||||
|
||||
LOGDEB2(("makeAbtract: done in %d mS\n", chron.millis()));
|
||||
return ret;
|
||||
|
||||
+11
-11
@@ -300,7 +300,7 @@ bool Query::getMatchTerms(unsigned long xdocid, vector<string>& terms)
|
||||
}
|
||||
|
||||
abstract_result Query::makeDocAbstract(Doc &doc,
|
||||
vector<pair<int, string> >& abstract,
|
||||
vector<Snippet>& abstract,
|
||||
int maxoccs, int ctxwords)
|
||||
{
|
||||
LOGDEB(("makeDocAbstract: maxoccs %d ctxwords %d\n", maxoccs, ctxwords));
|
||||
@@ -318,19 +318,19 @@ abstract_result Query::makeDocAbstract(Doc &doc,
|
||||
|
||||
bool Query::makeDocAbstract(Doc &doc, vector<string>& abstract)
|
||||
{
|
||||
vector<pair<int, string> > vpabs;
|
||||
vector<Snippet> vpabs;
|
||||
if (!makeDocAbstract(doc, vpabs))
|
||||
return false;
|
||||
for (vector<pair<int, string> >::const_iterator it = vpabs.begin();
|
||||
for (vector<Snippet>::const_iterator it = vpabs.begin();
|
||||
it != vpabs.end(); it++) {
|
||||
string chunk;
|
||||
if (it->first > 0) {
|
||||
if (it->page > 0) {
|
||||
doc.haspages = true;
|
||||
ostringstream ss;
|
||||
ss << it->first;
|
||||
ss << it->page;
|
||||
chunk += string(" [p ") + ss.str() + "] ";
|
||||
}
|
||||
chunk += it->second;
|
||||
chunk += it->snippet;
|
||||
abstract.push_back(chunk);
|
||||
}
|
||||
return true;
|
||||
@@ -338,18 +338,18 @@ bool Query::makeDocAbstract(Doc &doc, vector<string>& abstract)
|
||||
|
||||
bool Query::makeDocAbstract(Doc &doc, string& abstract)
|
||||
{
|
||||
vector<pair<int, string> > vpabs;
|
||||
vector<Snippet> vpabs;
|
||||
if (!makeDocAbstract(doc, vpabs))
|
||||
return false;
|
||||
for (vector<pair<int, string> >::const_iterator it = vpabs.begin();
|
||||
for (vector<Snippet>::const_iterator it = vpabs.begin();
|
||||
it != vpabs.end(); it++) {
|
||||
abstract.append(it->second);
|
||||
abstract.append(it->snippet);
|
||||
abstract.append(cstr_ellipsis);
|
||||
}
|
||||
return m_reason.empty() ? true : false;
|
||||
}
|
||||
|
||||
int Query::getFirstMatchPage(Doc &doc)
|
||||
int Query::getFirstMatchPage(Doc &doc, string& term)
|
||||
{
|
||||
LOGDEB1(("Db::getFirstMatchPages\n"));;
|
||||
if (!m_nq) {
|
||||
@@ -357,7 +357,7 @@ int Query::getFirstMatchPage(Doc &doc)
|
||||
return false;
|
||||
}
|
||||
int pagenum = -1;
|
||||
XAPTRY(pagenum = m_nq->getFirstMatchPage(Xapian::docid(doc.xdocid)),
|
||||
XAPTRY(pagenum = m_nq->getFirstMatchPage(Xapian::docid(doc.xdocid), term),
|
||||
m_db->m_ndb->xrdb, m_reason);
|
||||
return m_reason.empty() ? pagenum : -1;
|
||||
}
|
||||
|
||||
+21
-3
@@ -25,12 +25,12 @@ using std::vector;
|
||||
#endif
|
||||
|
||||
#include "refcntr.h"
|
||||
#include "searchdata.h"
|
||||
|
||||
#ifndef NO_NAMESPACES
|
||||
namespace Rcl {
|
||||
#endif
|
||||
|
||||
class SearchData;
|
||||
class Db;
|
||||
class Doc;
|
||||
|
||||
@@ -40,6 +40,24 @@ enum abstract_result {
|
||||
ABSRES_TRUNC = 2
|
||||
};
|
||||
|
||||
// Snippet entry for makeDocAbstract
|
||||
class Snippet {
|
||||
public:
|
||||
Snippet(int page, const string& snip)
|
||||
: page(page), snippet(snip)
|
||||
{
|
||||
}
|
||||
Snippet& setTerm(const string& trm)
|
||||
{
|
||||
term = trm;
|
||||
return *this;
|
||||
}
|
||||
int page;
|
||||
string term;
|
||||
string snippet;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* An Rcl::Query is a question (SearchData) applied to a
|
||||
* database. Handles access to the results. Somewhat equivalent to a
|
||||
@@ -89,10 +107,10 @@ class Query {
|
||||
// Returned as a snippets vector
|
||||
bool makeDocAbstract(Doc &doc, vector<string>& abstract);
|
||||
// Returned as a vector of pair<page,snippet> page is 0 if unknown
|
||||
abstract_result makeDocAbstract(Doc &doc, vector<pair<int, string> >& abst,
|
||||
abstract_result makeDocAbstract(Doc &doc, vector<Snippet>& abst,
|
||||
int maxoccs= -1, int ctxwords = -1);
|
||||
/** Retrieve detected page breaks positions */
|
||||
int getFirstMatchPage(Doc &doc);
|
||||
int getFirstMatchPage(Doc &doc, std::string& term);
|
||||
|
||||
/** Expand query to look for documents like the one passed in */
|
||||
vector<string> expand(const Doc &doc);
|
||||
|
||||
@@ -54,9 +54,9 @@ public:
|
||||
delete xenquire; xenquire = 0;
|
||||
termfreqs.clear();
|
||||
}
|
||||
abstract_result makeAbstract(Xapian::docid id, vector<pair<int, string> >&,
|
||||
abstract_result makeAbstract(Xapian::docid id, vector<Snippet>&,
|
||||
int maxoccs = -1, int ctxwords = -1);
|
||||
int getFirstMatchPage(Xapian::docid docid);
|
||||
int getFirstMatchPage(Xapian::docid docid, std::string& term);
|
||||
void setDbWideQTermsFreqs();
|
||||
double qualityTerms(Xapian::docid docid,
|
||||
const std::vector<std::string>& terms,
|
||||
|
||||
Reference in New Issue
Block a user