def query(self, q, ranked=True): """Return a ranked list of matching `Document` instances.""" qq = Query.parse(q) res = self.discodex_client.query(self.spec.invindex_name, qq) res = map(TfIdf.undemux, res) if not res: return [] pageranks = None if ranked: scoredb = ScoreDB(self.spec.scoredb_path) uris = [e[0] for e in res] pageranks = dict(scoredb.rank(uris)) if not pageranks: raise Exception("no ranks available") docs = [] for uri,scores in res: doc = self.docset.get(uri) doc.score = Score(**scores) if pageranks: doc.score['pagerank'] = pageranks[uri] doc.excerpt = doc.excerpt(qq) docs.append(doc) return docs
def test_expected_ranking(self): if self.__class__.__name__ == 'IntegrationTestCase': return if not hasattr(self, 'expected_ranking'): return scoredb = ScoreDB(self.fqclient.spec.scoredb_path) actual = scoredb.rank() self.assertResultsSimilar(self.expected_ranking, [Document(uri,scores=dict(pagerank=pr)) \ for uri,pr in actual])
def show_scores(program, spec): """Usage: <spec> Shows the scores of documents in the specified `spec` ScoreDB file. """ from freequery.index.scoredb import ScoreDB from freequery.client.client import Spec spec = Spec(spec) scoredb = ScoreDB(spec.scoredb_path) for uri, score in scoredb.items(): print "%.8f\t%s" % (score, uri)
class TestScoreDB(unittest.TestCase): sample_scores = { "http://example.com/": 0.05, "http://example.com/a.html": 0.04, "http://stanford.edu/": 0.02, "http://zzz.com/": 0.02, } def __write_fixture(self): dbw = ScoreDBWriter(TEST_SCOREDB_PATH) dbw.set_scores(self.sample_scores) dbw.save_and_close() def setUp(self): self.__write_fixture() self.scoredb = ScoreDB(TEST_SCOREDB_PATH) def test_reads_writes(self): self.assertAlmostEqual(0.05, self.scoredb.get_one("http://example.com/")) self.assertAlmostEqual(0.04, self.scoredb.get_one("http://example.com/a.html")) self.assertAlmostEqual(0.02, self.scoredb.get_one("http://stanford.edu/")) self.assertAlmostEqual(0.02, self.scoredb.get_one("http://zzz.com/")) def test_iterates(self): self.assertEquals(sorted(self.sample_scores.items()), sorted(self.scoredb.items())) def test_rank(self): self.assertEquals( [ ("http://example.com/", 0.05), ("http://example.com/a.html", 0.04), ("http://zzz.com/", 0.02), ("http://stanford.edu/", 0.02), ], self.scoredb.rank(), ) self.assertEquals( [("http://example.com/", 0.05), ("http://stanford.edu/", 0.02)], self.scoredb.rank(["http://stanford.edu/", "http://example.com/"]), )
def setUp(self): self.__write_fixture() self.scoredb = ScoreDB(TEST_SCOREDB_PATH)