def test_transition_model(self): # Links exist corpus = { "1.html": {"2.html", "3.html"}, "2.html": {"3.html"}, "3.html": {"2.html"}, } page = "1.html" damping_factor = 0.85 result = {"1.html": 0.05, "2.html": 0.475, "3.html": 0.475} for page, rank in pr.transition_model(corpus, page, damping_factor).items(): self.assertAlmostEqual(result[page], rank) # No links exist corpus = { "1.html": {}, "2.html": {"3.html"}, "3.html": {"2.html"}, } page = "1.html" damping_factor = 0.85 result = {"1.html": 0.333, "2.html": 0.333, "3.html": 0.333} for page, rank in pr.transition_model(corpus, page, damping_factor).items(): self.assertAlmostEqual(result[page], rank, 3)
def testTransitionModel(self): trans_model_0 = pr.transition_model(self.corpus_0, "1.html", DAMPING) self.assertEqual(trans_model_0, {"1.html": 0.05, "2.html": 0.475, "3.html": 0.475}) trans_model_1 = pr.transition_model(self.corpus_1, "1.html", DAMPING) self.assertEqual(trans_model_1, {"1.html": 0.3333333333333333, "2.html": 0.3333333333333333, "3.html": 0.3333333333333333}) trans_model_2 = pr.transition_model(self.corpus_2, "2.html", DAMPING) self.assertEqual(trans_model_2, {"2.html": 0.500, "3.html": 0.500})
def test_transition_model_no_links(self): model_1 = transition_model( {"1.html": {}, "2.html": {"3.html"}, "3.html": {"2.html"}}, "1.html", 0.85 ) model_2 = {"1.html": 0.33, "2.html": 0.33, "3.html": 0.33} for link in model_1: if link in model_2: self.assertAlmostEqual(model_1[link], model_2[link], places=2)
def test_transition_model_example(self): corpus = { "1.html": {"2.html", "3.html"}, "2.html": {"3.html"}, "3.html": {"2.html"} } page = "1.html" damping_factor = 0.85 expected = {"1.html": 0.05, "2.html": 0.475, "3.html": 0.475} result = transition_model(corpus, page, damping_factor) self.assertEqual(result, expected)
def test_transition_model_no_links(self): corpus = { "1.html": {"2.html", "3.html"}, "2.html": {"3.html"}, "3.html": {"2.html"}, "4.html": {} } page = "4.html" damping_factor = 0.85 expected = { "1.html": 0.25, "2.html": 0.25, "3.html": 0.25, "4.html": 0.25 } result = transition_model(corpus, page, damping_factor) self.assertEqual(result, expected)
from pagerank import transition_model transition_model(transition_model('corpus0', '1.html', 0.85))
def test_transition_model(): assert pagerank.transition_model(CRAWL0, "1.html") == PAGE_PROBS_1 assert pagerank.transition_model(CRAWL0, "2.html") == PAGE_PROBS_2 assert pagerank.transition_model(CRAWL0, "3.html") == PAGE_PROBS_3 assert pagerank.transition_model(CRAWL0, "4.html") == PAGE_PROBS_4
from pagerank import transition_model, crawl if len(sys.argv) != 2: sys.exit("Usage: python pagerank.py corpus") corpus = crawl(sys.argv[1]) print(corpus) trans = transition_model(corpus, "1.html", 0.85) print(trans)
from pagerank import transition_model, sample_pagerank, iterate_pagerank corpus = { "1.html": {"2.html", "3.html"}, "2.html": {"3.html"}, "3.html": {"2.html"} } page = "1.html" damp = 0.85 transition_model(corpus, page, damp) print(sample_pagerank(corpus, 0.85, 5000)) print(iterate_pagerank(corpus, 0.85))