def test_match(self): t = PrefTree() urls = [ ('aab/ced',True), ('aab/ru',False), ('bcadr/ste',False), ('aab/t/est',True), ('aab/t/le',True), ('aab/stts',False), ] for u, s in urls: t.add_url(u, s) t, pruned = t.prune(min_rate=0.9, min_children=2,min_urls=1) c, s, b = t.match_with_branch('aab/t/lu') self.assertEqual((c,s),(2,2)) self.assertEqual(''.join(b), 'aab/t/*') c, s, b = t.match_with_branch('aab/xx') self.assertEqual((c,s),(0,0)) self.assertEqual(''.join(b), 'aab/<unk>') c, s, b = t.match_with_branch('aab/ced') self.assertEqual((c,s),(1,1)) self.assertEqual(''.join(b), 'aab/ced')
def test_prune(self): t = PrefTree() with self.assertRaises(ValueError): t.prune(min_urls=0) for url, success in [ ('arxiv.org/pdf/1410.1234', True), ('arxiv.org/pdf/1409.1094', True), ('arxiv.org/pdf/1201.5480', True), ('arxiv.org/pdf/1601.01234', True), ('arxiv.org/pdf/1602.01i34', False), # oops ]: t.add_url(url, success) t, pruned = t.prune(min_rate=0.75,min_children=2,min_urls=1) self.assertEqual(len(t.urls()), 1) self.assertTrue(t.has_wildcard()) self.assertEqual(t.match('arxiv.org/pdf/1784.1920'), (5,4)) self.assertEqual(t.match('arxiv.org/pdf/2340.0124'), (0,0)) self.assertTrue(t.predict_success('arxiv.org/pdf/1784.1920', threshold=0.6, min_urls=3)) t.print_as_tree()
def test_prune_failures(self): t = PrefTree() for url, success in [ ('sciencedirect.com/paper1.pdf', False), ('sciencedirect.com/paper2.pdf', False), ('sciencedirect.com/paper3.pdf', False), ('mysciencework.com/paper9.pdf', True), ('mysciencework.com/paper8.pdf', False)]: t.add_url(url, success) t, pruned = t.prune(min_rate=0.95, min_children=2, min_urls=2) self.assertEqual(t.match('sciencedirect.com/paper4.pdf'), (3,0)) self.assertFalse(t.predict_success('sciencedirect.com/paper4.pdf'))
def test_prune_with_reverse(self): t = PrefTree() for url, success in [ ('researchgate.net/publication/233865122_uriset', False), ('researchgate.net/publication/143874230_albtedru', False), ('researchgate.net/publication/320748374_kelbcad', False), ('researchgate.net/publication/233865122_uriset.pdf', True), ('researchgate.net/publication/143874230_albtedru.pdf', True), ('researchgate.net/publication/320748374_kelbcad.pdf', True), ('onlinelibrary.wiley.com/wol1/doi/10.1002/anie.200800037.abstract', False), ('onlinelibrary.wiley.com/wol1/doi/10.1002/anie.200800037.pdf', False)]: t.add_url(url, success) t.print_as_tree() t, pruned = t.prune(reverse=True) t.print_as_tree() self.assertTrue(t.check_sanity()) for u, c, s in t.urls(): print flatten(u), c, s
def test_prune_empty_tree(self): t = PrefTree() t.prune() # should not raise anything