bookdata/cli/goodreads/
cluster.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use std::path::{Path, PathBuf};

use clap::Args;

use crate::arrow::*;
use crate::ids::codes::{NS_GR_BOOK, NS_GR_WORK};
use crate::prelude::*;

use polars::prelude::*;

#[derive(Args, Debug)]
pub struct CICommand {
    /// Cluster ratings.
    #[arg(long = "ratings")]
    ratings: bool,

    /// Cluster add-to-shelf actions.
    #[arg(long = "add-actions")]
    add_actions: bool,

    /// Cluster using native GoodReads works instead of book clusters.
    #[arg(long = "native-works")]
    native_works: bool,

    /// Write output to FILE.
    #[arg(short = 'o', long = "output", name = "FILE")]
    output: PathBuf,
}

#[derive(Debug, PartialEq, Eq)]
enum ActionType {
    Ratings,
    AddActions,
}

#[derive(Debug, PartialEq, Eq)]
enum AggType {
    Clusters,
    NativeWorks,
}

#[derive(Debug)]
pub struct ClusterOp {
    actions: ActionType,
    clusters: AggType,
    output: PathBuf,
}

impl CICommand {
    pub fn exec(&self) -> Result<()> {
        let mut op = if self.add_actions {
            ClusterOp::add_actions(&self.output)
        } else if self.ratings {
            ClusterOp::ratings(&self.output)
        } else {
            error!("must specify one of --add-actions, --ratings, or --reviews");
            return Err(anyhow!("no operating mode specified"));
        };
        if self.native_works {
            op = op.native_works();
        }

        op.cluster()
    }
}

impl ClusterOp {
    /// Start a new action-clustering operation.
    pub fn add_actions<P: AsRef<Path>>(path: P) -> ClusterOp {
        ClusterOp {
            actions: ActionType::AddActions,
            clusters: AggType::Clusters,
            output: path.as_ref().to_path_buf(),
        }
    }

    /// Start a new rating-clustering operation.
    pub fn ratings<P: AsRef<Path>>(path: P) -> ClusterOp {
        ClusterOp {
            actions: ActionType::Ratings,
            clusters: AggType::Clusters,
            output: path.as_ref().to_path_buf(),
        }
    }

    /// Set operation to cluster with native works instead of clusters.
    pub fn native_works(self) -> ClusterOp {
        ClusterOp {
            clusters: AggType::NativeWorks,
            ..self
        }
    }

    /// Run the clustering operation.
    pub fn cluster(self) -> Result<()> {
        let interactions = self.load_interactions()?;
        let interactions = self.filter(interactions);
        let interactions = self.project_and_sort(interactions);
        let actions = interactions
            .clone()
            .group_by(&[col("user"), col("item")])
            .agg(self.aggregates());

        let actions = self.maybe_integrate_ratings(actions, &interactions);

        debug!("logical plan: {:?}", actions.describe_plan());
        debug!("optimized plan: {:?}", actions.describe_optimized_plan()?);
        info!("collecting results");
        let actions = actions.collect()?;

        info!("writing {} actions to {:?}", actions.height(), &self.output);
        save_df_parquet(actions, &self.output)?;

        Ok(())
    }

    /// Load the interaction file.
    fn load_interactions(&self) -> Result<LazyFrame> {
        let path = "goodreads/gr-interactions.parquet";
        let data = LazyFrame::scan_parquet(path, Default::default())?;

        let links = LazyFrame::scan_parquet("goodreads/gr-book-link.parquet", Default::default())?;

        let data = data.join(
            links,
            &[col("book_id")],
            &[col("book_id")],
            JoinType::Inner.into(),
        );
        Ok(data)
    }

    /// Filter the data frame to only the actions we want
    fn filter(&self, frame: LazyFrame) -> LazyFrame {
        match self.actions {
            ActionType::Ratings => frame.filter(col("rating").is_not_null()),
            _ => frame,
        }
    }

    /// Create an identity column reference.
    fn id_col(&self) -> Expr {
        match self.clusters {
            AggType::Clusters => {
                info!("grouping by integrated clusters");
                col("cluster")
            }
            AggType::NativeWorks => {
                info!("grouping by native works");
                when(col("work_id").is_not_null())
                    .then(col("work_id") + lit(NS_GR_WORK.base()))
                    .otherwise(col("book_id") + lit(NS_GR_BOOK.base()))
            }
        }
    }

    /// Project and sort (if possible) the data.
    fn project_and_sort(&self, frame: LazyFrame) -> LazyFrame {
        frame.select(&[
            col("user_id").alias("user"),
            self.id_col().alias("item"),
            (col("updated").cast(DataType::Int64)).alias("timestamp"),
            col("rating"),
        ])
    }

    /// Aggreate the interactions.
    fn aggregates(&self) -> Vec<Expr> {
        match &self.actions {
            ActionType::Ratings => {
                vec![
                    col("rating").median().alias("rating"),
                    col("rating").last().alias("last_rating"),
                    col("timestamp").min().alias("first_time"),
                    col("timestamp").max().alias("last_time"),
                    col("item").count().alias("nratings"),
                ]
            }
            ActionType::AddActions => {
                vec![
                    col("timestamp").min().alias("first_time"),
                    col("timestamp").max().alias("last_time"),
                    col("item").count().alias("nactions"),
                ]
            }
        }
    }

    fn maybe_integrate_ratings(&self, actions: LazyFrame, source: &LazyFrame) -> LazyFrame {
        match &self.actions {
            ActionType::AddActions => {
                let ratings = source.clone().filter(col("rating").is_not_null());
                let ratings = ratings
                    .group_by(["user", "item"])
                    .agg(&[col("rating").last().alias("last_rating")]);
                actions.join(
                    ratings,
                    &[col("user"), col("item")],
                    &[col("user"), col("item")],
                    JoinType::Left.into(),
                )
            }
            _ => actions,
        }
    }
}