1 : // -*- C++ -*- (c) 2007 Enrico Zini <enrico@enricozini.org>
2 : // Petr Rockai <me@mornfall.net>
3 :
4 : /*
5 : * Copyright (C) 2007 Enrico Zini <enrico@debian.org>
6 : *
7 : * This program is free software; you can redistribute it and/or modify
8 : * it under the terms of the GNU General Public License as published by
9 : * the Free Software Foundation; either version 2 of the License, or
10 : * (at your option) any later version.
11 : *
12 : * This program is distributed in the hope that it will be useful,
13 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : * GNU General Public License for more details.
16 : *
17 : * You should have received a copy of the GNU General Public License
18 : * along with this program; if not, write to the Free Software
19 : * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 : */
21 :
22 : #include <ept/core/xapian.h>
23 : #include <ept/core/apt.h>
24 : #include <ept/core/apt/record.h>
25 :
26 : #include <ept/textsearch/maint/path.h>
27 :
28 : #include <wibble/regexp.h>
29 : #include <cctype>
30 : #include <cmath>
31 :
32 : #include <xapian/queryparser.h>
33 : #include <xapian/base.h>
34 :
35 : #include <algorithm>
36 :
37 : #include <iostream>
38 :
39 : using namespace std;
40 : using namespace ept;
41 : // using namespace ept::debtags;
42 :
43 : namespace ept {
44 : namespace core {
45 : namespace xapian {
46 :
47 : size_t max_index = 0;
48 :
49 4 : Source::Source()
50 4 : : m_stem("en"), m_opened( false )
51 : {
52 4 : }
53 :
54 9 : time_t Source::timestamp() const {
55 9 : return textsearch::Path::indexTimestamp();
56 : }
57 :
58 9 : void Source::open() const {
59 9 : if (timestamp()) {
60 9 : std::string path = textsearch::Path::index();
61 9 : m_db = Xapian::Database( path );
62 9 : m_opened = true;
63 : }
64 9 : }
65 :
66 0 : std::string Source::toLower(const std::string& str)
67 : {
68 0 : std::string res;
69 0 : res.reserve(str.size());
70 0 : for (std::string::const_iterator i = str.begin(); i != str.end(); ++i)
71 0 : res += tolower(*i);
72 0 : return res;
73 : }
74 :
75 : void Source::normalize_and_add(Xapian::Document& doc, const std::string& term,
76 0 : int& /* pos */) const
77 : {
78 0 : string t = Source::toLower(term);
79 0 : string s = m_stem(t);
80 0 : doc.add_term(t);
81 0 : if (s != t)
82 0 : doc.add_term(s);
83 0 : }
84 :
85 0 : Xapian::docid Source::docidByName(const std::string& pkgname) const
86 : {
87 0 : Xapian::PostingIterator i = m_db.postlist_begin("pkg:"+pkgname);
88 0 : if (i == m_db.postlist_end("pkg:"+pkgname))
89 0 : return 0;
90 : else
91 0 : return *i;
92 : }
93 :
94 0 : Xapian::Query Source::makeRelatedQuery(const std::string& pkgname) const
95 : {
96 0 : Xapian::Enquire enquire(db());
97 :
98 : // Retrieve the document for the given package
99 0 : enquire.set_query(Xapian::Query("pkg:"+pkgname));
100 0 : Xapian::MSet matches = enquire.get_mset(0, 1);
101 0 : Xapian::MSetIterator mi = matches.begin();
102 0 : if (mi == matches.end()) return Xapian::Query();
103 0 : Xapian::Document doc = mi.get_document();
104 :
105 : // Return the query to get the list of similar documents
106 0 : return Xapian::Query(Xapian::Query::OP_OR, doc.termlist_begin(), doc.termlist_end());
107 : }
108 :
109 : double Source::getDoubleValue(const std::string& pkgname,
110 0 : Xapian::valueno val_id) const
111 : {
112 0 : Xapian::docid id = docidByName(pkgname);
113 0 : if (id == 0)
114 0 : return 0.0;
115 0 : Xapian::Document doc = db().get_document(id);
116 0 : string val = doc.get_value(val_id);
117 0 : if (val.empty())
118 0 : return 0.0;
119 : else
120 0 : return Xapian::sortable_unserialise(val);
121 : }
122 :
123 : int Source::getIntValue(const std::string& pkgname,
124 0 : Xapian::valueno val_id) const
125 : {
126 0 : Xapian::docid id = docidByName(pkgname);
127 0 : if (id == 0)
128 0 : return 0;
129 0 : Xapian::Document doc = db().get_document(id);
130 0 : string val = doc.get_value(val_id);
131 0 : if (val.empty())
132 0 : return 0;
133 : else
134 0 : return (int)nearbyint(Xapian::sortable_unserialise(val));
135 : }
136 :
137 : }
138 : }
139 3 : }
140 :
141 : // vim:set ts=4 sw=4:
|