Created
June 27, 2015 18:45
-
-
Save rohozhnikoff/edae5e0457a0d8bbc462 to your computer and use it in GitHub Desktop.
базовый алгоритм для пересчета перетеканий внутреннего page-rank
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_ = require('lodash') | |
raw = { | |
main: { | |
pr: 1 | |
in: {} | |
out: [ | |
'login' | |
'tabs' | |
] | |
} | |
tabs: { | |
pr: 1 | |
in: {} | |
out: [ | |
'tab1' | |
'tab2' | |
'login' | |
'main' | |
] | |
} | |
tab1: { | |
pr: 1 | |
in: {} | |
out: [ | |
'main' | |
] | |
} | |
tab2: { | |
pr: 1 | |
in: {} | |
out: [ | |
'main' | |
] | |
} | |
login: { | |
pr: 1 | |
in: {} | |
out: [ | |
'main' | |
] | |
} | |
} | |
summ = (x, y) -> x + y | |
dropSmall = (x) -> Number((x).toFixed(2)) | |
iterations = 0 | |
calc = (db) -> | |
iterations++ | |
changesInc = 0 | |
# пересчитываем pr перед итерацией | |
for url, info of db | |
newPR = dropSmall(_.reduce(info.in, summ, 0) + 1) | |
if newPR isnt info.pr | |
changesInc++ | |
info.pr = newPR | |
# пересчитываем перетекаемый pr | |
for url, info of db | |
pr = dropSmall((info.pr * 0.85) / info.out.length) | |
console.log(pr) | |
for outName in info.out | |
outLinks = db[outName].in | |
if not outLinks[url]? or (outLinks[url] isnt pr) | |
changesInc++ | |
outLinks[url] = pr | |
if changesInc > 0 | |
calc(_.clone(db)) | |
else | |
db | |
console.log calc(raw) | |
console.log { | |
'количество страниц': Object.keys(raw).length | |
'количество связей': (info.out.length for page, info of raw).reduce(summ, 0) | |
'количество итераций': iterations | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment