[Debtags-commits] [svn] r1816 - in debtags/1.6.0: . tools

Enrico Zini enrico at costa.debian.org
Sat Jul 8 13:19:06 UTC 2006


Author: enrico
Date: Sat Jul  8 13:19:04 2006
New Revision: 1816

Modified:
   debtags/1.6.0/   (props changed)
   debtags/1.6.0/tools/debtags.cc
Log:
 r3001 at viaza:  enrico | 2006-07-08 15:18:33 +0200
 Allow keyword search to use multiple keywords
 Allow to change the keyword of the keyword search to redefine the set of 'interesting' tags during the search


Modified: debtags/1.6.0/tools/debtags.cc
==============================================================================
--- debtags/1.6.0/tools/debtags.cc	(original)
+++ debtags/1.6.0/tools/debtags.cc	Sat Jul  8 13:19:04 2006
@@ -894,7 +894,7 @@
 	coll::Fast<Package, Tag> coll;
 	TagMetrics<Tag, int> collMetrics;
 
-	std::string pattern;
+	std::vector<std::string> patterns;
 	std::set<Tag> wanted;
 	std::set<Tag> unwanted;
 	std::set<Tag> ignored;
@@ -902,11 +902,40 @@
 
 	vector<Tag> tagsInMenu;
 
+	void 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 patternMatch(const Package& pkg)
 	{
-		return pkg.name().find(pattern) != string::npos ||
-			pkg.shortDescription(string("")).find(pattern) != string::npos ||
-			pkg.longDescription(string("")).find(pattern) != string::npos;
+		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 tagMatch(const Package& pkg)
@@ -1094,53 +1123,37 @@
 #endif
 	}
 
+	void 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);
+
+		// 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);
+		TagMetrics<Tag, double> jumps = metrics2.jumpsFrom(metrics1);
+		interesting = jumps.tagsSortedByMetrics();
+	}
+
 public:
-	SmartSearcher(Ept& ept, const std::string& pattern) : ept(ept), pattern(pattern)
+	SmartSearcher(Ept& ept, const std::string& pattern) : ept(ept)
 	{
 		// Perform the initial filtering using the keyword search
 		for (Packages::iterator i = ept.packages().begin();
 				i != ept.packages().end(); ++i)
-		{
 			fullColl.insert(wibble::singleton(*i), ept.tagmap().getTagsOfItem(*i));
-			if (patternMatch(*i))
-				coll.insert(wibble::singleton(*i), ept.tagmap().getTagsOfItem(*i));
-		}
-
-		// Compute the set of tags that better represent the keyword search
-		TagMetrics<Tag, int> metrics1 = TagMetrics<Tag, int>::computeFromTags(fullColl);
-		collMetrics = TagMetrics<Tag, int>::computeFromTags(coll);
-		TagMetrics<Tag, double> jumps = collMetrics.jumpsFrom(metrics1);
-		interesting = jumps.tagsSortedByMetrics();
-		coll = fullColl;
 
-		//metrics1.dump("BEF ", cout);
-		//metrics2.dump("AFT ", cout);
-		//jumps.dump("JMP ", cout);
+		computeInteresting(pattern);
 
-		//autoSelect(tags, 1);
-		//autoSelect(tags, 0);
+		coll = fullColl;
+		collMetrics = TagMetrics<Tag, int>::computeFromTags(coll);
 	}
 
-#if 0
-	// TODO: convert to using a vector of patterns, to start with multiple keywords
-	void initialSearch(const std::string& pattern)
-	{
-#if 0
-		apt::predicate::Predicate<Package> fts = 
-			apt::match::Package::name(pattern) or
-			apt::match::Package::longDescription(pattern);
-
-		Range<Package> r = filteredRange(ept->packages().range(), fts);
-		for (Range<Package>::iterator i = r.begin(); i != r.end(); ++i)
-			coll.insert(wibble::singleton(*i), ept->tagmap().getTagsOfItem(*i));
-#endif
-		KeywordPackageChooser chooser(pattern);
-		wanted = topTags(chooser, 5);
-		cerr << "SIZE " << wanted.size() << endl;
-		refilter();
-	}		
-#endif
-
 	void interact()
 	{
 		bool done = false;
@@ -1153,7 +1166,7 @@
 			bool changed = false;
 
 			// TODO: allow to add tags based on a keyword search on coll
-			cout << "Your choice (+#, -#, =#, View, Done, Quit): ";
+			cout << "Your choice (+#, -#, =#, K word, View, Done, Quit, ?): ";
 			if (!getline(cin, ans))
 			{
 				cout << endl;
@@ -1162,57 +1175,83 @@
 
 			while (ans != "")
 			{
-				// Split the answer by spaces
-				string rest;
-				size_t pos = ans.find(" ");
-				if (pos != string::npos)
+				// If we're setting a new keyword search, process now and skip
+				// processing as a list
+				if (ans[0] == '?')
 				{
-					// Skip spaces
-					for ( ; pos < ans.size() && isspace(ans[pos]); ++pos)
-						;
-					rest = ans.substr(pos);
-					ans = ans.substr(0, pos);
+					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;
 				}
-				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 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)
 					{
-						Tag tag = tagsInMenu[idx - 1];
-						cout << "Understood " << ans << " as " << ans[0] << tag.fullname() << endl;
-
-						switch (ans[0])
+						// 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
 						{
-							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;
+							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;
 						}
-						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;
 					}
-				} 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;
 				}
-				ans = rest;
 			}
 			if (changed)
 				refilter();



More information about the Debtags-commits mailing list