bookdata/cli/
extract_graph.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//! Graph extraction program.
use std::convert::From;
use std::path::PathBuf;

use crate::graph::{load_graph, save_gml};
use crate::prelude::*;

/// Extract a subgraph.
#[derive(Args, Debug)]
#[command(name = "extract-graph")]
pub struct ExtractGraph {
    #[arg(long = "graph-file")]
    graph_file: Option<PathBuf>,

    #[arg(short = 'c', long = "cluster")]
    cluster: Option<i32>,

    #[arg(long = "output", short = 'o')]
    out_file: Option<PathBuf>,
}

impl Command for ExtractGraph {
    fn exec(&self) -> Result<()> {
        let path = match &self.graph_file {
            Some(p) => p.clone(),
            None => PathBuf::from("book-links/book-graph.mp.zst"),
        };

        info!("loading graph from {}", path.to_string_lossy());
        let mut graph = load_graph(path)?;

        if let Some(c) = &self.cluster {
            graph.retain_nodes(|g, n| {
                let book = g.node_weight(n).unwrap();
                book.cluster == *c
            });
        }

        info!("filtered graph to {} nodes", graph.node_count());

        if let Some(outf) = &self.out_file {
            save_gml(&graph, outf)?;
        }

        Ok(())
    }
}