Skip to content
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

Add explicit for-item mapping to nearest-neighbor set #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
354 changes: 196 additions & 158 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "recoreco"
description = "Fast item-to-item recommendations on the command line."
version = "0.1.9"
version = "0.1.10"
authors = ["Sebastian Schelter <ssc [at] apache [dot] org>"]
license = "GPL-3.0-or-later"
homepage = "https://github.com/sscdotopen/recoreco"
Expand All @@ -22,4 +22,4 @@ serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
getopts = "0.2"
rayon = "1.0"
rayon = "1.0"
2 changes: 1 addition & 1 deletion src/bin/indicators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn compute_indicators(
interactions_path: &str,
n: usize,
indicators_path: Option<String>,
) -> Result<(), Box<Error>> {
) -> Result<(), Box<dyn Error>> {

// We use constants here for the moment, these should result in a good runtime/quality ratio.
const F_MAX: u32 = 500;
Expand Down
14 changes: 7 additions & 7 deletions src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ struct Indicators<'a> {
/// `{ "for_item": "michael jackson", "indicated_items": ["justin timberlake", "queen"] }`
///
pub fn write_indicators(
indicators: &[FnvHashSet<u32>],
indicators: &[(u32, FnvHashSet<u32>)],
renaming: &Renaming,
indicators_path: Option<String>,
) -> io::Result<()> {
Expand All @@ -107,13 +107,13 @@ pub fn write_indicators(
_ => boxed_writer(stdout()),
};

for (item_index, indicated_item_indices) in indicators.into_iter().enumerate() {
for (_index, indicator_tuple) in indicators.into_iter().enumerate() {

let for_item = renaming.item_name(item_index as u32);
let for_item = renaming.item_name(indicator_tuple.0 as u32);

let indicated_items: FnvHashSet<&str> = indicated_item_indices
let indicated_items: FnvHashSet<&str> = (&indicator_tuple.1)
.into_iter()
.map(|item_index| renaming.item_name(*item_index as u32))
.map(|item_index| renaming.item_name(*item_index))
.collect();

let indicators_as_json = json!(
Expand All @@ -130,9 +130,9 @@ pub fn write_indicators(

fn boxed_writer<T>(
destination: T
) -> Box<Write>
) -> Box<dyn Write>
where
T: Write + 'static
{
Box::new(BufWriter::new(destination))
}
}
20 changes: 11 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub mod stats;
mod usage_tests;

use llr::ScoredItem;
use types::{SparseVector, SparseMatrix, SparseBinaryMatrix};
use types::{SparseVector, SparseMatrix, IndicatorSet};
use stats::DataDictionary;

/// Compute item indicators from a stream of interactions.
Expand Down Expand Up @@ -135,7 +135,7 @@ pub fn indicators<T>(
num_indicators_per_item: usize,
f_max: u32,
k_max: u32
) -> SparseBinaryMatrix
) -> IndicatorSet
where
T: Iterator<Item = (String, String)>
{
Expand Down Expand Up @@ -181,7 +181,7 @@ where
if item_interaction_counts[item_idx] < f_max {

// Retrieve current history sample for interacting user
let mut user_history = &mut samples_of_a[user_idx];
let user_history = &mut samples_of_a[user_idx];
let num_items_in_user_history = user_history.len();

// Check whether we have seen enough interactions for this user yet
Expand Down Expand Up @@ -259,9 +259,10 @@ where
num_cooccurrences_observed,
num_indicators_per_item,
&precomputed_logarithms,
//&renaming
)
})
.collect();
.collect::<Vec<(u32, FnvHashSet<u32>)>>();

let duration = to_millis(start.elapsed());
println!(
Expand All @@ -285,20 +286,21 @@ fn rescore(
num_cooccurrences_observed: u64,
n: usize,
logarithms_table: &[f64],
) -> FnvHashSet<u32> {
) -> (u32, FnvHashSet<u32>) {

// We can skip the scoring if we have seen less than n items
if cooccurrence_counts.len() <= n {
cooccurrence_counts
(item, cooccurrence_counts
.keys()
.cloned()
.collect::<FnvHashSet<_>>()
.collect::<FnvHashSet<_>>())
} else {
// We'll use a heap to keep track of the current top-n scored items
let mut top_indicators: BinaryHeap<ScoredItem> = BinaryHeap::with_capacity(n);

for (other_item, num_cooccurrences) in cooccurrence_counts.iter() {
if *other_item != item {

// Compute counts of contingency table
let k11 = u64::from(*num_cooccurrences);
let k12 = u64::from(row_sums_of_c[item as usize]) - k11;
Expand Down Expand Up @@ -327,6 +329,6 @@ fn rescore(
.map(|scored_item| scored_item.item)
.collect();

indicators_for_item
(item, indicators_for_item)
}
}
}
2 changes: 1 addition & 1 deletion src/llr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,4 +184,4 @@ mod tests {
assert_eq!(top_k[2].item, 2);
assert!(within_epsilon(top_k[2].score, 1.5));
}
}
}
2 changes: 1 addition & 1 deletion src/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,4 @@ mod tests {
assert_eq!(renaming.item_name(1), "item_b");
assert_eq!(renaming.item_name(2), "item_c");
}
}
}
2 changes: 2 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ pub fn new_dense_vector(dimensions: usize) -> DenseVector {
pub fn new_sparse_matrix(num_rows: usize) -> SparseMatrix {
vec![FnvHashMap::with_capacity_and_hasher(0, Default::default()); num_rows]
}

pub type IndicatorSet = Vec<(u32, FnvHashSet<u32>)>;