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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
/*
* debtags - Implement package tags support for Debian
*
* Copyright (C) 2003--2006 Enrico Zini <enrico@debian.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "SmartSearcher.h"
#include "Printer.h"
#include <tagcoll/utils/set.h>
using namespace std;
void SmartSearcher::splitPattern(const std::string& pattern)
{
string rest = pattern;
string item;
patterns.clear();
while (!rest.empty())
{
size_t pos = rest.find(" ");
if (pos != string::npos)
{
// Skip spaces
for ( ; pos < rest.size() && isspace(rest[pos]); ++pos)
;
item = rest.substr(0, pos);
rest = rest.substr(pos);
} else {
item = rest;
rest.clear();
}
patterns.push_back(item);
}
}
bool SmartSearcher::patternMatch(const Package& pkg)
{
for (std::vector<std::string>::const_iterator i = patterns.begin();
i != patterns.end(); ++i)
if (pkg.name().find(*i) == string::npos
&& pkg.shortDescription(string("")).find(*i) == string::npos
&& pkg.longDescription(string("")).find(*i) == string::npos)
return false;
return true;
}
bool SmartSearcher::tagMatch(const Package& pkg)
{
using namespace wibble::operators;
std::set<Tag> tags = ept.tagmap().getTagsOfItem(pkg);
if (!wanted.empty() && !utils::set_contains(tags, wanted))
return false;
if (!unwanted.empty() && !(tags & unwanted).empty())
return false;
return true;
}
void SmartSearcher::showSet(const std::set<Tag>& tags, const std::string& type)
{
for (std::set<Tag>::const_iterator i = tags.begin();
i != tags.end(); ++i)
{
tagsInMenu.push_back(*i);
cout << tagsInMenu.size() << ") " << i->fullname() << " (" << type << ")" << endl;
}
}
void SmartSearcher::showInteresting(int max)
{
for (std::vector<Tag>::const_reverse_iterator i = interesting.rbegin();
i != interesting.rend() && max > 0; ++i)
{
using namespace wibble::operators;
if (utils::set_contains(wanted, *i)
|| utils::set_contains(unwanted, *i)
|| utils::set_contains(ignored, *i)
|| coll.getCardinality(*i) == 0)
continue;
tagsInMenu.push_back(*i);
cout << tagsInMenu.size() << ") " << i->fullname()
<< " (" << coll.getCardinality(*i) << "/" << coll.itemCount() << ")" << endl;
--max;
}
}
void SmartSearcher::showDiscriminant(int max)
{
// Compute the most interesting tags by discriminance
vector<Tag> discr = coll.tagsInDiscriminanceOrder();
for (std::vector<Tag>::const_reverse_iterator i = discr.rbegin();
i != discr.rend() && max > 0; ++i)
{
using namespace wibble::operators;
if (utils::set_contains(wanted, *i)
|| utils::set_contains(unwanted, *i)
|| utils::set_contains(ignored, *i))
continue;
tagsInMenu.push_back(*i);
cout << tagsInMenu.size() << ") " << i->fullname()
<< " (" << coll.getCardinality(*i) << "/" << coll.itemCount() << ")" << endl;
--max;
}
}
void SmartSearcher::showTags()
{
tagsInMenu.clear();
showSet(wanted, "wanted");
showSet(unwanted, "unwanted");
showSet(ignored, "ignored");
cout << endl;
showInteresting();
cout << endl;
showDiscriminant();
}
void SmartSearcher::refilter()
{
// Regenerate coll
coll = coll::Fast<Package, Tag>();
fullColl.output(filter(inserter(coll)));
}
void SmartSearcher::computeInteresting(const std::string& pattern)
{
splitPattern(pattern);
coll::Fast<Package, Tag> filtered;
for (coll::Fast<Package, Tag>::const_iterator i = fullColl.begin();
i != fullColl.end(); ++i)
if (patternMatch(i->first))
filtered.insert(wibble::singleton(i->first), i->second);
interesting = filtered.tagsInRelevanceOrder(fullColl);
#if 0
// Compute the set of tags that better represent the keyword search
TagMetrics<Tag, int> metrics1 = TagMetrics<Tag, int>::computeFromTags(fullColl);
TagMetrics<Tag, int> metrics2 = TagMetrics<Tag, int>::computeFromTags(filtered);
#if 0
// Use the jump algorithm
TagMetrics<Tag, double> jumps = metrics2.jumpsFrom(metrics1);
interesting = jumps.tagsSortedByMetrics();
#else
// Use the reduction percentage algorithm (a bit faster, and more
// optimizable)
interesting = metrics2.tagsSortedByRelevance(metrics1);
#endif
#endif
}
SmartSearcher::SmartSearcher(Ept& ept, const std::string& pattern) : ept(ept)
{
// Perform the initial filtering using the keyword search
for (Index::iterator i = ept.packages().begin();
i != ept.packages().end(); ++i)
fullColl.insert(wibble::singleton(*i), ept.tagmap().getTagsOfItem(*i));
computeInteresting(pattern);
coll = fullColl;
}
void SmartSearcher::interact()
{
bool done = false;
while (!done)
{
cout << "Tag selection:" << endl;
showTags();
cout << coll.itemCount() << " packages selected so far." << endl;
string ans;
bool changed = false;
// TODO: allow to add tags based on a keyword search on coll
cout << "Your choice (+#, -#, =#, K word, View, Done, Quit, ?): ";
if (!getline(cin, ans))
{
cout << endl;
done = true;
}
while (ans != "")
{
// If we're setting a new keyword search, process now and skip
// processing as a list
if (ans[0] == '?')
{
cout << "+ number select the tag with the given number as a tag you want" << endl
<< "- number select the tag with the given number as a tag you do not want" << endl
<< "= number select the tag with the given number as a tag you don't care about" << endl
<< "K word recompute the set of interesting tags from a full-text search using the given word" << endl
<< "V view the packages selected so far" << endl
<< "D print the packages selected so far and exit" << endl
<< "Q quit debtags smart search" << endl
<< "? print this help information" << endl;
}
else if (ans[0] == 'k' || ans[0] == 'K')
{
// Strip initial command and empty spaces
ans = ans.substr(1);
while (!ans.empty() && isspace(ans[0]))
ans = ans.substr(1);
if (ans == "")
cout << "The 'k' command needs a keyword to use for finding new interesting tags." << endl;
else
computeInteresting(ans);
ans.clear();
} else {
// Split the answer by spaces
string rest;
size_t pos = ans.find(" ");
if (pos != string::npos)
{
// Skip spaces
for ( ; pos < ans.size() && isspace(ans[pos]); ++pos)
;
rest = ans.substr(pos);
ans = ans.substr(0, pos);
}
if (ans[0] == '+' || ans[0] == '-' || ans[0] == '=') {
int idx = strtoul(ans.substr(1).c_str(), NULL, 10);
if (idx <= 0 || (unsigned)idx > tagsInMenu.size())
cout << "Tag " << idx << " was not on the menu." << endl;
else
{
Tag tag = tagsInMenu[idx - 1];
//cout << "Understood " << ans << " as " << ans[0] << tag.fullname() << endl;
switch (ans[0])
{
case '+':
wanted.insert(tag);
unwanted.erase(tag);
ignored.erase(tag);
break;
case '-':
wanted.erase(tag);
unwanted.insert(tag);
ignored.erase(tag);
break;
case '=':
wanted.erase(tag);
unwanted.erase(tag);
ignored.insert(tag);
break;
}
changed = true;
}
} else if (ans == "V" || ans == "v") {
coll.output(PackagePrinter(ept.tagmap(), PackagePrinter::SHORT));
} else if (ans == "D" || ans == "d") {
coll.output(PackagePrinter(ept.tagmap(), PackagePrinter::SHORT));
done = true;
} else if (ans == "Q" || ans == "q") {
done = true;
} else {
cout << "Ignoring command \"" << ans << "\"" << endl;
}
ans = rest;
}
}
if (changed)
refilter();
}
}
void SmartSearcher::outputRelevantTags()
{
for (std::vector<Tag>::const_iterator i = interesting.begin();
i != interesting.end(); ++i)
{
using namespace wibble::operators;
if (utils::set_contains(wanted, *i)
|| utils::set_contains(unwanted, *i)
|| utils::set_contains(ignored, *i)
|| coll.getCardinality(*i) == 0)
continue;
cout << i->fullname() << " - " << i->shortDescription() << endl;
}
}
void SmartSearcher::outputDiscriminantTags()
{
// Compute the most interesting tags by discriminance
coll::Fast<Package, Tag> filtered;
for (coll::Fast<Package, Tag>::const_iterator i = fullColl.begin();
i != fullColl.end(); ++i)
if (patternMatch(i->first))
filtered.insert(wibble::singleton(i->first), i->second);
vector<Tag> discr = filtered.tagsInDiscriminanceOrder();
for (std::vector<Tag>::const_iterator i = discr.begin();
i != discr.end(); ++i)
{
using namespace wibble::operators;
if (utils::set_contains(wanted, *i)
|| utils::set_contains(unwanted, *i)
|| utils::set_contains(ignored, *i))
continue;
cout << i->fullname() << " - " << i->shortDescription()
<< " (" << filtered.getDiscriminance(*i) * 200 /filtered.itemCount() << "%)" << endl;
}
}
#include <tagcoll/coll/fast.tcc>
// vim:set ts=4 sw=4:
|