-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathusage_tests.rs
More file actions
81 lines (69 loc) · 3.48 KB
/
Copy pathusage_tests.rs
File metadata and controls
81 lines (69 loc) · 3.48 KB
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
/**
* RecoReco
* Copyright (C) 2018 Sebastian Schelter
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#[cfg(test)]
mod tests {
use super::super::indicators;
use stats::{DataDictionary, Renaming};
#[test]
fn programmatic_usage() {
/* Our input data comprises of observed interactions between users and items.
The identifiers used can be strings of arbitrary length and structure. */
let interactions = vec![
(String::from("alice"), String::from("apple")),
(String::from("alice"), String::from("dog")),
(String::from("alice"), String::from("pony")),
(String::from("bob"), String::from("apple")),
(String::from("bob"), String::from("pony")),
(String::from("charles"), String::from("pony")),
(String::from("charles"), String::from("bike")),
];
/* Internally, recoreco uses consecutive integer ids and requires some knowledge about the
statistics of the data for efficient allocation. Therefore, we read the interaction data
once to compute a data dictionary that helps us map from string to integer identifiers
and has basic statistics of the data */
let data_dict = DataDictionary::from(interactions.iter());
println!(
"Found {} interactions between {} users and {} items.",
data_dict.num_interactions(),
data_dict.num_users(),
data_dict.num_items(),
);
/* Now we read the interactions a second time and compute the indicator matrix from item
cooccurrences. The result is the so-called indicator matrix, where each entry indicates
highly associated pairs of items. */
let indicated_items = indicators(
interactions.into_iter(), // The observed interactions
&data_dict, // The data dictionary which maps string to integer identifiers
10, // The number of highly associated items to compute per item
500, // The maximum number of interactions to account for per user (use 500 as default)
500, // The maximum number of interactions to account for per item (use 500 as default)
);
/* The renaming data structure helps us map the integer ids back to the original
string ids. */
let renaming = Renaming::from(data_dict);
/* We print the resulting highly associated pairs of items. */
for (item_index, indicated_items_for_item) in indicated_items.iter().enumerate() {
let item_name = renaming.item_name(item_index as u32);
println!("Items highly associated with {}:", item_name);
for indicated_item_index in indicated_items_for_item.iter() {
let indicated_item_name = renaming.item_name(*indicated_item_index as u32);
println!("\t{}", indicated_item_name);
}
}
}
}