mirror of
https://github.com/twitter/the-algorithm.git
synced 2025-06-19 02:58:15 -05:00
Twitter Recommendation Algorithm
Please note we have force-pushed a new initial commit in order to remove some publicly-available Twitter user information. Note that this process may be required in the future.
This commit is contained in:
78
navi/thrift_bpr_adapter/thrift/src/decoder.rs
Normal file
78
navi/thrift_bpr_adapter/thrift/src/decoder.rs
Normal file
@ -0,0 +1,78 @@
|
||||
|
||||
// A feature value can be one of these
|
||||
enum FeatureVal {
|
||||
Empty,
|
||||
U8Vector(Vec<u8>),
|
||||
FloatVector(Vec<f32>),
|
||||
}
|
||||
|
||||
// A Feture has a name and a value
|
||||
// The name for now is 'id' of type string
|
||||
// Eventually this needs to be flexible - example to accomodate feature-id
|
||||
struct Feature {
|
||||
id: String,
|
||||
val: FeatureVal,
|
||||
}
|
||||
|
||||
impl Feature {
|
||||
fn new() -> Feature {
|
||||
Feature {
|
||||
id: String::new(),
|
||||
val: FeatureVal::Empty
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// A single inference record will have multiple features
|
||||
struct Record {
|
||||
fields: Vec<Feature>,
|
||||
}
|
||||
|
||||
impl Record {
|
||||
fn new() -> Record {
|
||||
Record { fields: vec![] }
|
||||
}
|
||||
}
|
||||
|
||||
// This is the main API used by external components
|
||||
// Given a serialized input, decode it into Records
|
||||
fn decode(input: Vec<u8>) -> Vec<Record> {
|
||||
// For helping define the interface
|
||||
vec![get_random_record(), get_random_record()]
|
||||
}
|
||||
|
||||
// Used for testing the API, will be eventually removed
|
||||
fn get_random_record() -> Record {
|
||||
let mut record: Record = Record::new();
|
||||
|
||||
let f1: Feature = Feature {
|
||||
id: String::from("continuous_features"),
|
||||
val: FeatureVal::FloatVector(vec![1.0f32; 2134]),
|
||||
};
|
||||
|
||||
record.fields.push(f1);
|
||||
|
||||
let f2: Feature = Feature {
|
||||
id: String::from("user_embedding"),
|
||||
val: FeatureVal::FloatVector(vec![2.0f32; 200]),
|
||||
};
|
||||
|
||||
record.fields.push(f2);
|
||||
|
||||
let f3: Feature = Feature {
|
||||
id: String::from("author_embedding"),
|
||||
val: FeatureVal::FloatVector(vec![3.0f32; 200]),
|
||||
};
|
||||
|
||||
record.fields.push(f3);
|
||||
|
||||
let f4: Feature = Feature {
|
||||
id: String::from("binary_features"),
|
||||
val: FeatureVal::U8Vector(vec![4u8; 43]),
|
||||
};
|
||||
|
||||
record.fields.push(f4);
|
||||
|
||||
record
|
||||
}
|
||||
|
Reference in New Issue
Block a user