Skip to content

Commit

Permalink
calculate pagerank
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianHicks committed Dec 22, 2022
1 parent 10c14b6 commit 6871348
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/main.rs
Expand Up @@ -3,6 +3,7 @@ use color_eyre::Result;
use eyre::WrapErr;
use rayon::prelude::*;
use regex::Regex;
use simple_pagerank::Pagerank;
use std::collections::{HashMap, HashSet};
use std::path::PathBuf;

Expand All @@ -28,7 +29,13 @@ impl Opts {
.discover_links(&files)
.context("could not discover links")?;

println!("{links:?}");
let pagerank = self
.calculate_pagerank(&links)
.wrap_err("could not calculate pagerank")?;

for (node, score) in pagerank.nodes() {
println!("{score:.3}\t{node}");
}

Ok(())
}
Expand Down Expand Up @@ -101,6 +108,27 @@ impl Opts {

return Ok(out);
}

fn calculate_pagerank<'links>(
&self,
links: &'links HashMap<String, HashSet<String>>,
) -> Result<Pagerank<&'links String>> {
let mut pagerank = Pagerank::new();
pagerank
.set_damping_factor(self.damping_factor)
.map_err(|e| eyre::eyre!(e))
.wrap_err("could not set damping factor")?;

for (name, out_edges) in links {
for link in out_edges {
pagerank.add_edge(name, link)
}
}

pagerank.calculate();

Ok(pagerank)
}
}

fn main() {
Expand Down

0 comments on commit 6871348

Please sign in to comment.