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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
use core::f32;

use serde_json::Value;

use crate::error::Result;

use crate::object_pool::Pool;
use crate::parsers::ParsedFeature;
use crate::sparse_namespaced_features::{Namespace, SparseFeatures};
use crate::types::{Features, Label, LabelType};
use crate::{CBAdfFeatures, CBLabel, FeatureMask, FeaturesType};

use super::{ParsedNamespaceInfo, TextModeParser, TextModeParserFactory};

#[derive(Default)]
pub struct DsJsonParserFactory;
impl TextModeParserFactory for DsJsonParserFactory {
    type Parser = DsJsonParser;

    fn create(
        &self,
        features_type: FeaturesType,
        label_type: LabelType,
        hash_seed: u32,
        num_bits: u8,
        pool: std::sync::Arc<Pool<SparseFeatures>>,
    ) -> DsJsonParser {
        // Only supports CB
        if features_type != FeaturesType::SparseCBAdf {
            panic!("DsJsonParser only supports SparseCBAdf")
        }

        if label_type != LabelType::CB {
            panic!("DsJsonParser only supports CB labels")
        }

        DsJsonParser {
            _feature_type: features_type,
            _label_type: label_type,
            hash_seed,
            num_bits,
            pool,
        }
    }
}

pub struct DsJsonParser {
    _feature_type: FeaturesType,
    _label_type: LabelType,
    hash_seed: u32,
    num_bits: u8,
    pool: std::sync::Arc<Pool<SparseFeatures>>,
}

impl DsJsonParser {
    pub fn handle_features(
        &self,
        features: &mut SparseFeatures,
        object_key: &str,
        json_value: &Value,
        namespace_stack: &mut Vec<Namespace>,
    ) {
        // All underscore prefixed keys are ignored.
        if object_key.starts_with('_') {
            return;
        }

        // skip everything with _
        match json_value {
            Value::Null => panic!("Null is not supported"),
            Value::Bool(true) => {
                let current_ns = namespace_stack
                    .last()
                    .expect("namespace stack should not be empty here")
                    .clone();
                let current_ns_hash = current_ns.hash(self.hash_seed);
                let current_feats = features.get_or_create_namespace(current_ns);
                current_feats.add_feature(
                    ParsedFeature::Simple { name: object_key }
                        .hash(current_ns_hash)
                        .mask(FeatureMask::from_num_bits(self.num_bits)),
                    1.0,
                );
            }
            Value::Bool(false) => (),
            Value::Number(value) => {
                let current_ns = namespace_stack
                    .last()
                    .expect("namespace stack should not be empty here")
                    .clone();
                let current_ns_hash = current_ns.hash(self.hash_seed);
                let current_feats = features.get_or_create_namespace(current_ns);
                current_feats.add_feature(
                    ParsedFeature::Simple { name: object_key }
                        .hash(current_ns_hash)
                        .mask(FeatureMask::from_num_bits(self.num_bits)),
                    value.as_f64().unwrap() as f32,
                );
            }
            Value::String(value) => {
                let current_ns = namespace_stack
                    .last()
                    .expect("namespace stack should not be empty here")
                    .clone();
                let current_ns_hash = current_ns.hash(self.hash_seed);
                let current_feats = features.get_or_create_namespace(current_ns);
                current_feats.add_feature(
                    ParsedFeature::SimpleWithStringValue {
                        name: object_key,
                        value,
                    }
                    .hash(current_ns_hash)
                    .mask(FeatureMask::from_num_bits(self.num_bits)),
                    1.0,
                );
            }
            Value::Array(value) => {
                namespace_stack.push(Namespace::from_name(object_key, self.hash_seed));
                let current_ns = namespace_stack
                    .last()
                    .expect("namespace stack should not be empty here")
                    .clone();
                let current_ns_hash = current_ns.hash(self.hash_seed);
                for (anon_idx, v) in value.iter().enumerate() {
                    match v {
                        Value::Number(value) => {
                            // Not super efficient but it works
                            // Doing this in the outside doesn't work as it would mean two mutable borrows.
                            let current_feats = features.get_or_create_namespace(current_ns);
                            current_feats.add_feature(
                                ParsedFeature::Anonymous {
                                    offset: anon_idx as u32,
                                }
                                .hash(current_ns_hash)
                                .mask(FeatureMask::from_num_bits(self.num_bits)),
                                value.as_f64().unwrap() as f32,
                            );
                        }
                        Value::Object(_) => {
                            self.handle_features(features, object_key, v, namespace_stack);
                        }
                        // Just ignore null and do nothing
                        Value::Null => (),
                        _ => panic!(
                            "Array of non-number or object is not supported key:{} value:{:?}",
                            object_key, v
                        ),
                    }
                }
                namespace_stack.pop().unwrap();
            }
            Value::Object(value) => {
                namespace_stack.push(Namespace::from_name(object_key, self.hash_seed));
                for (key, v) in value {
                    self.handle_features(features, key, v, namespace_stack);
                }
                namespace_stack.pop().unwrap();
            }
        }
    }
}

impl TextModeParser for DsJsonParser {
    fn get_next_chunk(
        &self,
        input: &mut dyn std::io::BufRead,
        mut output_buffer: String,
    ) -> Result<Option<String>> {
        output_buffer.clear();
        input.read_line(&mut output_buffer)?;
        if output_buffer.is_empty() {
            return Ok(None);
        }
        Ok(Some(output_buffer))
    }

    fn parse_chunk<'a, 'b>(&self, chunk: &'a str) -> Result<(Features<'b>, Option<Label>)> {
        let json: serde_json::Value =
            serde_json::from_str(chunk).expect("JSON was not well-formatted");

        let mut namespace_stack = Vec::new();

        let mut shared_ex = self.pool.get_object();
        self.handle_features(&mut shared_ex, " ", &json["c"], &mut namespace_stack);
        assert!(namespace_stack.is_empty());

        let mut actions = Vec::new();
        for item in json["c"]["_multi"].as_array().unwrap() {
            let mut action = self.pool.get_object();
            self.handle_features(&mut action, " ", &item, &mut namespace_stack);
            actions.push(action);
            assert!(namespace_stack.is_empty());
        }

        let cost = json.get("_label_cost").map(|v| v.as_f64().unwrap() as f32);
        let probability = json
            .get("_label_probability")
            .map(|v| v.as_f64().unwrap() as f32);
        let action = json
            .get("_labelIndex")
            .map(|v| v.as_u64().unwrap() as usize);

        let label = match (cost, probability, action) {
            (Some(cost), Some(probability), Some(action)) => Some(CBLabel {
                action,
                cost,
                probability,
            }),
            (None, None, None) => None,
            _ => panic!("Invalid label, all 3 or none must be present"),
        };

        Ok((
            Features::SparseCBAdf(CBAdfFeatures {
                shared: Some(shared_ex),
                actions,
            }),
            label.map(|x| Label::CB(x)),
        ))
    }

    fn extract_feature_names<'a>(
        &self,
        _chunk: &'a str,
    ) -> Result<std::collections::HashMap<ParsedNamespaceInfo<'a>, Vec<ParsedFeature<'a>>>> {
        todo!()
    }
}

#[cfg(test)]
mod test {
    use std::sync::Arc;

    use approx::assert_relative_eq;
    use serde_json::json;

    use crate::{
        object_pool::Pool,
        parsers::{DsJsonParserFactory, TextModeParser, TextModeParserFactory},
        sparse_namespaced_features::Namespace,
        utils::GetInner,
        CBAdfFeatures, CBLabel, FeaturesType, LabelType,
    };
    #[test]
    fn extract_dsjson_test_chain_hash() {
        let json_obj = json!({
          "_label_cost": -0.0,
          "_label_probability": 0.05000000074505806,
          "_label_Action": 4,
          "_labelIndex": 3,
          "o": [
            {
              "v": 0.0,
              "EventId": "13118d9b4c114f8485d9dec417e3aefe",
              "ActionTaken": false
            }
          ],
          "Timestamp": "2021-02-04T16:31:29.2460000Z",
          "Version": "1",
          "EventId": "13118d9b4c114f8485d9dec417e3aefe",
          "a": [4, 2, 1, 3],
          "c": {
            "bool_true": true,
            "bool_false": false,
            "numbers": [4, 5.6],
            "FromUrl": [
              { "timeofday": "Afternoon", "weather": "Sunny", "name": "Cathy" }
            ],
            "_multi": [
              {
                "_tag": "Cappucino",
                "i": { "constant": 1, "id": "Cappucino" },
                "j": [
                  {
                    "type": "hot",
                    "origin": "kenya",
                    "organic": "yes",
                    "roast": "dark"
                  }
                ]
              }
            ]
          },
          "p": [0.05, 0.05, 0.05, 0.85],
          "VWState": {
            "m": "ff0744c1aa494e1ab39ba0c78d048146/550c12cbd3aa47f09fbed3387fb9c6ec"
          },
          "_original_label_cost": -0.0
        });

        let pool = Arc::new(Pool::new());
        let parser = DsJsonParserFactory::default().create(
            FeaturesType::SparseCBAdf,
            LabelType::CB,
            0,
            18,
            pool,
        );

        let (features, label) = parser.parse_chunk(&json_obj.to_string()).unwrap();
        let cb_label: &CBLabel = label.as_ref().unwrap().get_inner_ref().unwrap();
        assert_eq!(cb_label.action, 3);
        assert_relative_eq!(cb_label.cost, 0.0);
        assert_relative_eq!(cb_label.probability, 0.05);

        let cb_feats: &CBAdfFeatures = features.get_inner_ref().unwrap();
        assert_eq!(cb_feats.actions.len(), 1);
        assert!(cb_feats.shared.is_some());
        let shared = cb_feats.shared.as_ref().unwrap();
        assert_eq!(shared.namespaces().count(), 3);
        let shared_default_ns = shared.get_namespace(Namespace::Default).unwrap();
        assert_eq!(shared_default_ns.iter().count(), 1);

        let shared_from_url_ns = shared
            .get_namespace(Namespace::from_name("FromUrl", 0))
            .unwrap();
        assert_eq!(shared_from_url_ns.iter().count(), 3);

        let shared_numbers_ns = shared
            .get_namespace(Namespace::from_name("numbers", 0))
            .unwrap();
        assert_eq!(shared_numbers_ns.iter().count(), 2);
        assert_relative_eq!(
            shared_numbers_ns.iter().map(|(_, val)| val).sum::<f32>(),
            9.6
        );

        let action = cb_feats.actions.get(0).unwrap();
        assert_eq!(action.namespaces().count(), 2);
        assert!(action.get_namespace(Namespace::Default).is_none());
        let action_i_ns = action.get_namespace(Namespace::from_name("i", 0)).unwrap();
        assert_eq!(action_i_ns.iter().count(), 2);
        let action_j_ns = action.get_namespace(Namespace::from_name("j", 0)).unwrap();
        assert_eq!(action_j_ns.iter().count(), 4);
    }
}