Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Fixed mistake in HITS and add test to NLP #441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 17, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions nlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,13 +356,13 @@ def detect(self):
def getInlinks(page):
if not page.inlinks:
page.inlinks = determineInlinks(page)
return [p for addr, p in pagesIndex.items() if addr in page.inlinks]
return [addr for addr, p in pagesIndex.items() if addr in page.inlinks]


def getOutlinks(page):
if not page.outlinks:
page.outlinks = findOutlinks(page)
return [p for addr, p in pagesIndex.items() if addr in page.outlinks]
return [addr for addr, p in pagesIndex.items() if addr in page.outlinks]


# ______________________________________________________________________________
Expand All @@ -389,9 +389,11 @@ def HITS(query):
p.authority = 1
p.hub = 1
while True: # repeat until... convergence
for p in pages.values():
p.authority = sum(x.hub for x in getInlinks(p)) # p.authority ← ∑i Inlinki(p).Hub
p.hub = sum(x.authority for x in getOutlinks(p)) # p.hub ← ∑i Outlinki(p).Authority
authority = {p: pages[p].authority for p in pages}
hub = {p: pages[p].hub for p in pages}
for p in pages:
pages[p].authority = sum(hub[x] for x in getInlinks(pages[p])) # p.authority ← ∑i Inlinki(p).Hub
pages[p].hub = sum(authority[x] for x in getOutlinks(pages[p])) # p.hub ← ∑i Outlinki(p).Authority
normalize(pages)
if convergence():
break
Expand Down
19 changes: 11 additions & 8 deletions tests/test_nlp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import nlp
from nlp import loadPageHTML, stripRawHTML, findOutlinks, onlyWikipediaURLS
from nlp import expand_pages, relevant_pages, normalize, ConvergenceDetector, getInlinks
from nlp import getOutlinks, Page
from nlp import getOutlinks, Page, determineInlinks, HITS
from nlp import Rules, Lexicon
# Clumsy imports because we want to access certain nlp.py globals explicitly, because
# they are accessed by function's within nlp.py
Expand Down Expand Up @@ -61,9 +61,9 @@ def test_stripRawHTML():


def test_determineInlinks():
# TODO
assert True

assert set(determineInlinks(pA)) == set(['B', 'C', 'E'])
assert set(determineInlinks(pE)) == set([])
assert set(determineInlinks(pF)) == set(['E'])

def test_findOutlinks_wiki():
testPage = pageDict[pA.address]
Expand Down Expand Up @@ -122,17 +122,20 @@ def test_detectConvergence():

def test_getInlinks():
inlnks = getInlinks(pageDict['A'])
assert sorted([page.address for page in inlnks]) == pageDict['A'].inlinks
assert sorted(inlnks) == pageDict['A'].inlinks


def test_getOutlinks():
outlnks = getOutlinks(pageDict['A'])
assert sorted([page.address for page in outlnks]) == pageDict['A'].outlinks
assert sorted(outlnks) == pageDict['A'].outlinks


def test_HITS():
# TODO
assert True # leave for now
HITS('inherit')
auth_list = [pA.authority, pB.authority, pC.authority, pD.authority, pE.authority, pF.authority]
hub_list = [pA.hub, pB.hub, pC.hub, pD.hub, pE.hub, pF.hub]
assert max(auth_list) == pD.authority
assert max(hub_list) == pE.hub


if __name__ == '__main__':
Expand Down