summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/lib/PersonalityProvider/RecipeExecutor.jsm
blob: 9dbf8b802d44f2409c54ec396726db07aa960a9a (plain)
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

// We load this into a worker using importScripts, and in tests using import.
// We use var to avoid name collision errors.
// eslint-disable-next-line no-var
var EXPORTED_SYMBOLS = ["RecipeExecutor"];

/**
 * RecipeExecutor is the core feature engineering pipeline for the in-browser
 * personalization work. These pipelines are called "recipes". A recipe is an
 * array of objects that define a "step" in the recipe. A step is simply an
 * object with a field "function" that specifies what is being done in the step
 * along with other fields that are semantically defined for that step.
 *
 * There are two types of recipes "builder" recipes and "combiner" recipes. Builder
 * recipes mutate an object until it matches some set of critera. Combiner
 * recipes take two objects, (a "left" and a "right"), and specify the steps
 * to merge the right object into the left object.
 *
 * A short nonsense example recipe is:
 * [ {"function": "get_url_domain", "path_length": 1, "field": "url", "dest": "url_domain"},
 *   {"function": "nb_tag", "fields": ["title", "description"]},
 *   {"function": "conditionally_nmf_tag", "fields": ["title", "description"]} ]
 *
 * Recipes are sandboxed by the fact that the step functions must be explicitly
 * allowed. Functions allowed for builder recipes are specifed in the
 * RecipeExecutor.ITEM_BUILDER_REGISTRY, while combiner functions are allowed
 * in RecipeExecutor.ITEM_COMBINER_REGISTRY .
 */
const RecipeExecutor = class RecipeExecutor {
  constructor(nbTaggers, nmfTaggers, tokenize) {
    this.ITEM_BUILDER_REGISTRY = {
      nb_tag: this.naiveBayesTag,
      conditionally_nmf_tag: this.conditionallyNmfTag,
      accept_item_by_field_value: this.acceptItemByFieldValue,
      tokenize_url: this.tokenizeUrl,
      get_url_domain: this.getUrlDomain,
      tokenize_field: this.tokenizeField,
      copy_value: this.copyValue,
      keep_top_k: this.keepTopK,
      scalar_multiply: this.scalarMultiply,
      elementwise_multiply: this.elementwiseMultiply,
      vector_multiply: this.vectorMultiply,
      scalar_add: this.scalarAdd,
      vector_add: this.vectorAdd,
      make_boolean: this.makeBoolean,
      allow_fields: this.allowFields,
      filter_by_value: this.filterByValue,
      l2_normalize: this.l2Normalize,
      prob_normalize: this.probNormalize,
      set_default: this.setDefault,
      lookup_value: this.lookupValue,
      copy_to_map: this.copyToMap,
      scalar_multiply_tag: this.scalarMultiplyTag,
      apply_softmax_tags: this.applySoftmaxTags,
    };
    this.ITEM_COMBINER_REGISTRY = {
      combiner_add: this.combinerAdd,
      combiner_max: this.combinerMax,
      combiner_collect_values: this.combinerCollectValues,
    };
    this.nbTaggers = nbTaggers;
    this.nmfTaggers = nmfTaggers;
    this.tokenize = tokenize;
  }

  /**
   * Determines the type of a field. Valid types are:
   *   string
   *   number
   *   array
   *   map (strings to anything)
   */
  _typeOf(data) {
    let t = typeof data;
    if (t === "object") {
      if (data === null) {
        return "null";
      }
      if (Array.isArray(data)) {
        return "array";
      }
      return "map";
    }
    return t;
  }

  /**
   * Returns a scalar, either because it was a constant, or by
   * looking it up from the item. Allows for a default value if the lookup
   * fails.
   */
  _lookupScalar(item, k, dfault) {
    if (this._typeOf(k) === "number") {
      return k;
    } else if (
      this._typeOf(k) === "string" &&
      k in item &&
      this._typeOf(item[k]) === "number"
    ) {
      return item[k];
    }
    return dfault;
  }

  /**
   * Simply appends all the strings from a set fields together. If the field
   * is a list, then the cells of the list are append.
   */
  _assembleText(item, fields) {
    let textArr = [];
    for (let field of fields) {
      if (field in item) {
        let type = this._typeOf(item[field]);
        if (type === "string") {
          textArr.push(item[field]);
        } else if (type === "array") {
          for (let ele of item[field]) {
            textArr.push(String(ele));
          }
        } else {
          textArr.push(String(item[field]));
        }
      }
    }
    return textArr.join(" ");
  }

  /**
   * Runs the naive bayes text taggers over a set of text fields. Stores the
   * results in new fields:
   *  nb_tags:         a map of text strings to probabilites
   *  nb_tokens:       the tokenized text that was tagged
   *
   * Config:
   *  fields:          an array containing a list of fields to concatenate and tag
   */
  naiveBayesTag(item, config) {
    let text = this._assembleText(item, config.fields);
    let tokens = this.tokenize(text);
    let tags = {};
    let extended_tags = {};

    for (let nbTagger of this.nbTaggers) {
      let result = nbTagger.tagTokens(tokens);
      if (result.label !== null && result.confident) {
        extended_tags[result.label] = result;
        tags[result.label] = Math.exp(result.logProb);
      }
    }
    item.nb_tags = tags;
    item.nb_tags_extended = extended_tags;
    item.nb_tokens = tokens;
    return item;
  }

  /**
   * Selectively runs NMF text taggers depending on which tags were found
   * by the naive bayes taggers. Writes the results in into new fields:
   *  nmf_tags_parent_weights:  map of pareent tags to probabilites of those parent tags
   *  nmf_tags:                 map of strings to maps of strings to probabilities
   *  nmf_tags_parent           map of child tags to parent tags
   *
   * Config:
   *  Not configurable
   */
  conditionallyNmfTag(item, config) {
    let nestedNmfTags = {};
    let parentTags = {};
    let parentWeights = {};

    if (!("nb_tags" in item) || !("nb_tokens" in item)) {
      return null;
    }

    Object.keys(item.nb_tags).forEach(parentTag => {
      let nmfTagger = this.nmfTaggers[parentTag];
      if (nmfTagger !== undefined) {
        nestedNmfTags[parentTag] = {};
        parentWeights[parentTag] = item.nb_tags[parentTag];
        let nmfTags = nmfTagger.tagTokens(item.nb_tokens);
        Object.keys(nmfTags).forEach(nmfTag => {
          nestedNmfTags[parentTag][nmfTag] = nmfTags[nmfTag];
          parentTags[nmfTag] = parentTag;
        });
      }
    });

    item.nmf_tags = nestedNmfTags;
    item.nmf_tags_parent = parentTags;
    item.nmf_tags_parent_weights = parentWeights;

    return item;
  }

  /**
   * Checks a field's value against another value (either from another field
   * or a constant). If the test passes, then the item is emitted, otherwise
   * the pipeline is aborted.
   *
   * Config:
   *  field      Field to read the value to test. Left side of operator.
   *  op         one of ==, !=, <, <=, >, >=
   *  rhsValue   Constant value to compare against. Right side of operator.
   *  rhsField   Field to read value to compare against. Right side of operator.
   *
   * NOTE: rhsValue takes precidence over rhsField.
   */
  acceptItemByFieldValue(item, config) {
    if (!(config.field in item)) {
      return null;
    }
    let rhs = null;
    if ("rhsValue" in config) {
      rhs = config.rhsValue;
    } else if ("rhsField" in config && config.rhsField in item) {
      rhs = item[config.rhsField];
    }
    if (rhs === null) {
      return null;
    }

    if (
      // eslint-disable-next-line eqeqeq
      (config.op === "==" && item[config.field] == rhs) ||
      // eslint-disable-next-line eqeqeq
      (config.op === "!=" && item[config.field] != rhs) ||
      (config.op === "<" && item[config.field] < rhs) ||
      (config.op === "<=" && item[config.field] <= rhs) ||
      (config.op === ">" && item[config.field] > rhs) ||
      (config.op === ">=" && item[config.field] >= rhs)
    ) {
      return item;
    }

    return null;
  }

  /**
   * Splits a URL into text-like tokens.
   *
   * Config:
   *  field   Field containing a URL
   *  dest    Field to write the tokens to as an array of strings
   *
   * NOTE: Any initial 'www' on the hostname is removed.
   */
  tokenizeUrl(item, config) {
    if (!(config.field in item)) {
      return null;
    }

    let url = new URL(item[config.field]);
    let domain = url.hostname;
    if (domain.startsWith("www.")) {
      domain = domain.substring(4);
    }
    let toks = this.tokenize(domain);
    let pathToks = this.tokenize(
      decodeURIComponent(url.pathname.replace(/\+/g, " "))
    );
    for (let tok of pathToks) {
      toks.push(tok);
    }
    for (let pair of url.searchParams.entries()) {
      let k = this.tokenize(decodeURIComponent(pair[0].replace(/\+/g, " ")));
      for (let tok of k) {
        toks.push(tok);
      }
      if (pair[1] !== null && pair[1] !== "") {
        let v = this.tokenize(decodeURIComponent(pair[1].replace(/\+/g, " ")));
        for (let tok of v) {
          toks.push(tok);
        }
      }
    }
    item[config.dest] = toks;

    return item;
  }

  /**
   * Gets the hostname (minus any initial "www." along with the left most
   * directories on the path.
   *
   * Config:
   *  field          Field containing the URL
   *  dest           Field to write the array of strings to
   *  path_length    OPTIONAL (DEFAULT: 0) Number of leftmost subdirectories to include
   */
  getUrlDomain(item, config) {
    if (!(config.field in item)) {
      return null;
    }

    let url = new URL(item[config.field]);
    let domain = url.hostname.toLocaleLowerCase();
    if (domain.startsWith("www.")) {
      domain = domain.substring(4);
    }
    item[config.dest] = domain;
    let pathLength = 0;
    if ("path_length" in config) {
      pathLength = config.path_length;
    }
    if (pathLength > 0) {
      item[config.dest] += url.pathname
        .toLocaleLowerCase()
        .split("/")
        .slice(0, pathLength + 1)
        .join("/");
    }

    return item;
  }

  /**
   * Splits a field into tokens.
   * Config:
   *  field         Field containing a string to tokenize
   *  dest          Field to write the array of strings to
   */
  tokenizeField(item, config) {
    if (!(config.field in item)) {
      return null;
    }

    item[config.dest] = this.tokenize(item[config.field]);

    return item;
  }

  /**
   * Deep copy from one field to another.
   * Config:
   *  src           Field to read from
   *  dest          Field to write to
   */
  copyValue(item, config) {
    if (!(config.src in item)) {
      return null;
    }

    item[config.dest] = JSON.parse(JSON.stringify(item[config.src]));

    return item;
  }

  /**
   * Converts a field containing a map of strings to a map of strings
   * to numbers, to a map of strings to numbers containing at most k elements.
   * This operation is performed by first, promoting all the subkeys up one
   * level, and then taking the top (or bottom) k values.
   *
   * Config:
   *  field         Points to a map of strings to a map of strings to numbers
   *  k             Maximum number of items to keep
   *  descending    OPTIONAL (DEFAULT: True) Sorts score in descending  order
   *                  (i.e. keeps maximum)
   */
  keepTopK(item, config) {
    if (!(config.field in item)) {
      return null;
    }
    let k = this._lookupScalar(item, config.k, 1048576);
    let descending = !("descending" in config) || config.descending !== false;

    // we can't sort by the values in the map, so we have to convert this
    // to an array, and then sort.
    let sortable = [];
    Object.keys(item[config.field]).forEach(outerKey => {
      let innerType = this._typeOf(item[config.field][outerKey]);
      if (innerType === "map") {
        Object.keys(item[config.field][outerKey]).forEach(innerKey => {
          sortable.push({
            key: innerKey,
            value: item[config.field][outerKey][innerKey],
          });
        });
      } else {
        sortable.push({ key: outerKey, value: item[config.field][outerKey] });
      }
    });

    sortable.sort((a, b) => {
      if (descending) {
        return b.value - a.value;
      }
      return a.value - b.value;
    });

    // now take the top k
    let newMap = {};
    let i = 0;
    for (let pair of sortable) {
      if (i >= k) {
        break;
      }
      newMap[pair.key] = pair.value;
      i++;
    }
    item[config.field] = newMap;

    return item;
  }

  /**
   * Scalar multiplies a vector by some constant
   *
   * Config:
   *  field         Points to:
   *                   a map of strings to numbers
   *                   an array of numbers
   *                   a number
   *  k             Either a number, or a string. If it's a number then This
   *                  is the scalar value to multiply by. If it's a string,
   *                  the value in the pointed to field is used.
   *  default       OPTIONAL (DEFAULT: 0), If k is a string, and no numeric
   *                  value is found, then use this value.
   */
  scalarMultiply(item, config) {
    if (!(config.field in item)) {
      return null;
    }
    let k = this._lookupScalar(item, config.k, config.dfault);

    let fieldType = this._typeOf(item[config.field]);
    if (fieldType === "number") {
      item[config.field] *= k;
    } else if (fieldType === "array") {
      for (let i = 0; i < item[config.field].length; i++) {
        item[config.field][i] *= k;
      }
    } else if (fieldType === "map") {
      Object.keys(item[config.field]).forEach(key => {
        item[config.field][key] *= k;
      });
    } else {
      return null;
    }

    return item;
  }

  /**
   * Elementwise multiplies either two maps or two arrays together, storing
   * the result in left. If left and right are of the same type, results in an
   * error.
   *
   * Maps are special case. For maps the left must be a nested map such as:
   * { k1: { k11: 1, k12: 2}, k2: { k21: 3, k22: 4 } } and right needs to be
   * simple map such as: { k1: 5, k2: 6} .  The operation is then to mulitply
   * every value of every right key, to every value every subkey where the
   * parent keys match. Using the previous examples, the result would be:
   * { k1: { k11: 5, k12: 10 }, k2: { k21: 18, k22: 24 } } .
   *
   * Config:
   *  left
   *  right
   */
  elementwiseMultiply(item, config) {
    if (!(config.left in item) || !(config.right in item)) {
      return null;
    }
    let leftType = this._typeOf(item[config.left]);
    if (leftType !== this._typeOf(item[config.right])) {
      return null;
    }
    if (leftType === "array") {
      if (item[config.left].length !== item[config.right].length) {
        return null;
      }
      for (let i = 0; i < item[config.left].length; i++) {
        item[config.left][i] *= item[config.right][i];
      }
    } else if (leftType === "map") {
      Object.keys(item[config.left]).forEach(outerKey => {
        let r = 0.0;
        if (outerKey in item[config.right]) {
          r = item[config.right][outerKey];
        }
        Object.keys(item[config.left][outerKey]).forEach(innerKey => {
          item[config.left][outerKey][innerKey] *= r;
        });
      });
    } else if (leftType === "number") {
      item[config.left] *= item[config.right];
    } else {
      return null;
    }

    return item;
  }

  /**
   * Vector multiplies (i.e. dot products) two vectors and stores the result in
   * third field. Both vectors must either by maps, or arrays of numbers with
   * the same length.
   *
   * Config:
   *   left       A field pointing to either a map of strings to numbers,
   *                or an array of numbers
   *   right      A field pointing to either a map of strings to numbers,
   *                or an array of numbers
   *   dest       The field to store the dot product.
   */
  vectorMultiply(item, config) {
    if (!(config.left in item) || !(config.right in item)) {
      return null;
    }

    let leftType = this._typeOf(item[config.left]);
    if (leftType !== this._typeOf(item[config.right])) {
      return null;
    }

    let destVal = 0.0;
    if (leftType === "array") {
      if (item[config.left].length !== item[config.right].length) {
        return null;
      }
      for (let i = 0; i < item[config.left].length; i++) {
        destVal += item[config.left][i] * item[config.right][i];
      }
    } else if (leftType === "map") {
      Object.keys(item[config.left]).forEach(key => {
        if (key in item[config.right]) {
          destVal += item[config.left][key] * item[config.right][key];
        }
      });
    } else {
      return null;
    }

    item[config.dest] = destVal;
    return item;
  }

  /**
   * Adds a constant value to all elements in the field. Mathematically,
   * this is the same as taking a 1-vector, scalar multiplying it by k,
   * and then vector adding it to a field.
   *
   * Config:
   *  field     A field pointing to either a map of strings to numbers,
   *                  or an array of numbers
   *  k             Either a number, or a string. If it's a number then This
   *                  is the scalar value to multiply by. If it's a string,
   *                  the value in the pointed to field is used.
   *  default       OPTIONAL (DEFAULT: 0), If k is a string, and no numeric
   *                  value is found, then use this value.
   */
  scalarAdd(item, config) {
    let k = this._lookupScalar(item, config.k, config.dfault);
    if (!(config.field in item)) {
      return null;
    }

    let fieldType = this._typeOf(item[config.field]);
    if (fieldType === "array") {
      for (let i = 0; i < item[config.field].length; i++) {
        item[config.field][i] += k;
      }
    } else if (fieldType === "map") {
      Object.keys(item[config.field]).forEach(key => {
        item[config.field][key] += k;
      });
    } else if (fieldType === "number") {
      item[config.field] += k;
    } else {
      return null;
    }

    return item;
  }

  /**
   * Adds two vectors together and stores the result in left.
   *
   * Config:
   *  left      A field pointing to either a map of strings to numbers,
   *                  or an array of numbers
   *  right     A field pointing to either a map of strings to numbers,
   *                  or an array of numbers
   */
  vectorAdd(item, config) {
    if (!(config.left in item)) {
      return this.copyValue(item, { src: config.right, dest: config.left });
    }
    if (!(config.right in item)) {
      return null;
    }

    let leftType = this._typeOf(item[config.left]);
    if (leftType !== this._typeOf(item[config.right])) {
      return null;
    }
    if (leftType === "array") {
      if (item[config.left].length !== item[config.right].length) {
        return null;
      }
      for (let i = 0; i < item[config.left].length; i++) {
        item[config.left][i] += item[config.right][i];
      }
      return item;
    } else if (leftType === "map") {
      Object.keys(item[config.right]).forEach(key => {
        let v = 0;
        if (key in item[config.left]) {
          v = item[config.left][key];
        }
        item[config.left][key] = v + item[config.right][key];
      });
      return item;
    }

    return null;
  }

  /**
   * Converts a vector from real values to boolean integers. (i.e. either 1/0
   * or 1/-1).
   *
   * Config:
   *   field            Field containing either a map of strings to numbers or
   *                      an array of numbers to  convert.
   *   threshold        OPTIONAL (DEFAULT: 0) Values above this will be replaced
   *                      with 1.0. Those below will be converted to 0.
   *   keep_negative    OPTIONAL (DEFAULT: False) If true, values below the
   *                      threshold will be converted to -1 instead of 0.
   */
  makeBoolean(item, config) {
    if (!(config.field in item)) {
      return null;
    }
    let threshold = this._lookupScalar(item, config.threshold, 0.0);
    let type = this._typeOf(item[config.field]);
    if (type === "array") {
      for (let i = 0; i < item[config.field].length; i++) {
        if (item[config.field][i] > threshold) {
          item[config.field][i] = 1.0;
        } else if (config.keep_negative) {
          item[config.field][i] = -1.0;
        } else {
          item[config.field][i] = 0.0;
        }
      }
    } else if (type === "map") {
      Object.keys(item[config.field]).forEach(key => {
        let value = item[config.field][key];
        if (value > threshold) {
          item[config.field][key] = 1.0;
        } else if (config.keep_negative) {
          item[config.field][key] = -1.0;
        } else {
          item[config.field][key] = 0.0;
        }
      });
    } else if (type === "number") {
      let value = item[config.field];
      if (value > threshold) {
        item[config.field] = 1.0;
      } else if (config.keep_negative) {
        item[config.field] = -1.0;
      } else {
        item[config.field] = 0.0;
      }
    } else {
      return null;
    }

    return item;
  }

  /**
   * Removes all keys from the item except for the ones specified.
   *
   * fields           An array of strings indicating the fields to keep
   */
  allowFields(item, config) {
    let newItem = {};
    for (let ele of config.fields) {
      if (ele in item) {
        newItem[ele] = item[ele];
      }
    }
    return newItem;
  }

  /**
   * Removes all keys whose value does not exceed some threshold.
   *
   * Config:
   *   field         Points to a map of strings to numbers
   *   threshold     Values must exceed this value, otherwise they are removed.
   */
  filterByValue(item, config) {
    if (!(config.field in item)) {
      return null;
    }
    let threshold = this._lookupScalar(item, config.threshold, 0.0);
    let filtered = {};
    Object.keys(item[config.field]).forEach(key => {
      let value = item[config.field][key];
      if (value > threshold) {
        filtered[key] = value;
      }
    });
    item[config.field] = filtered;

    return item;
  }

  /**
   * Rewrites a field so that its values are now L2 normed.
   *
   * Config:
   *  field         Points to a map of strings to numbers, or an array of numbers
   */
  l2Normalize(item, config) {
    if (!(config.field in item)) {
      return null;
    }
    let data = item[config.field];
    let type = this._typeOf(data);
    if (type === "array") {
      let norm = 0.0;
      for (let datum of data) {
        norm += datum * datum;
      }
      norm = Math.sqrt(norm);
      if (norm !== 0) {
        for (let i = 0; i < data.length; i++) {
          data[i] /= norm;
        }
      }
    } else if (type === "map") {
      let norm = 0.0;
      Object.keys(data).forEach(key => {
        norm += data[key] * data[key];
      });
      norm = Math.sqrt(norm);
      if (norm !== 0) {
        Object.keys(data).forEach(key => {
          data[key] /= norm;
        });
      }
    } else {
      return null;
    }

    item[config.field] = data;

    return item;
  }

  /**
   * Rewrites a field so that all of its values sum to 1.0
   *
   * Config:
   *  field         Points to a map of strings to numbers, or an array of numbers
   */
  probNormalize(item, config) {
    if (!(config.field in item)) {
      return null;
    }
    let data = item[config.field];
    let type = this._typeOf(data);
    if (type === "array") {
      let norm = 0.0;
      for (let datum of data) {
        norm += datum;
      }
      if (norm !== 0) {
        for (let i = 0; i < data.length; i++) {
          data[i] /= norm;
        }
      }
    } else if (type === "map") {
      let norm = 0.0;
      Object.keys(item[config.field]).forEach(key => {
        norm += item[config.field][key];
      });
      if (norm !== 0) {
        Object.keys(item[config.field]).forEach(key => {
          item[config.field][key] /= norm;
        });
      }
    } else {
      return null;
    }

    return item;
  }

  /**
   * Stores a value, if it is not already present
   *
   * Config:
   *  field             field to write to if it is missing
   *  value             value to store in that field
   */
  setDefault(item, config) {
    let val = this._lookupScalar(item, config.value, config.value);
    if (!(config.field in item)) {
      item[config.field] = val;
    }

    return item;
  }

  /**
   * Selctively promotes an value from an inner map up to the outer map
   *
   * Config:
   *  haystack            Points to a map of strings to values
   *  needle              Key inside the map we should promote up
   *  dest                Where we should write the value of haystack[needle]
   */
  lookupValue(item, config) {
    if (config.haystack in item && config.needle in item[config.haystack]) {
      item[config.dest] = item[config.haystack][config.needle];
    }

    return item;
  }

  /**
   * Demotes a field into a map
   *
   * Config:
   *  src               Field to copy
   *  dest_map          Points to a map
   *  dest_key          Key inside dest_map to copy src to
   */
  copyToMap(item, config) {
    if (config.src in item) {
      if (!(config.dest_map in item)) {
        item[config.dest_map] = {};
      }
      item[config.dest_map][config.dest_key] = item[config.src];
    }

    return item;
  }

  /**
   * Config:
   *  field             Points to a string to number map
   *  k                 Scalar to multiply the values by
   *  log_scale         Boolean, if true, then the values will be transformed
   *                      by a logrithm prior to multiplications
   */
  scalarMultiplyTag(item, config) {
    let EPSILON = 0.000001;
    if (!(config.field in item)) {
      return null;
    }
    let k = this._lookupScalar(item, config.k, 1);
    let type = this._typeOf(item[config.field]);
    if (type === "map") {
      Object.keys(item[config.field]).forEach(parentKey => {
        Object.keys(item[config.field][parentKey]).forEach(key => {
          let v = item[config.field][parentKey][key];
          if (config.log_scale) {
            v = Math.log(v + EPSILON);
          }
          item[config.field][parentKey][key] = v * k;
        });
      });
    } else {
      return null;
    }

    return item;
  }

  /**
   * Independently applies softmax across all subtags.
   *
   * Config:
   *   field        Points to a map of strings with values being another map of strings
   */
  applySoftmaxTags(item, config) {
    let type = this._typeOf(item[config.field]);
    if (type !== "map") {
      return null;
    }

    let abort = false;
    let softmaxSum = {};
    Object.keys(item[config.field]).forEach(tag => {
      if (this._typeOf(item[config.field][tag]) !== "map") {
        abort = true;
        return;
      }
      if (abort) {
        return;
      }
      softmaxSum[tag] = 0;
      Object.keys(item[config.field][tag]).forEach(subtag => {
        if (this._typeOf(item[config.field][tag][subtag]) !== "number") {
          abort = true;
          return;
        }
        let score = item[config.field][tag][subtag];
        softmaxSum[tag] += Math.exp(score);
      });
    });
    if (abort) {
      return null;
    }

    Object.keys(item[config.field]).forEach(tag => {
      Object.keys(item[config.field][tag]).forEach(subtag => {
        item[config.field][tag][subtag] =
          Math.exp(item[config.field][tag][subtag]) / softmaxSum[tag];
      });
    });

    return item;
  }

  /**
   * Vector adds a field and stores the result in left.
   *
   * Config:
   *   field              The field to vector add
   */
  combinerAdd(left, right, config) {
    if (!(config.field in right)) {
      return left;
    }
    let type = this._typeOf(right[config.field]);
    if (!(config.field in left)) {
      if (type === "map") {
        left[config.field] = {};
      } else if (type === "array") {
        left[config.field] = [];
      } else if (type === "number") {
        left[config.field] = 0;
      } else {
        return null;
      }
    }
    if (type !== this._typeOf(left[config.field])) {
      return null;
    }
    if (type === "map") {
      Object.keys(right[config.field]).forEach(key => {
        if (!(key in left[config.field])) {
          left[config.field][key] = 0;
        }
        left[config.field][key] += right[config.field][key];
      });
    } else if (type === "array") {
      for (let i = 0; i < right[config.field].length; i++) {
        if (i < left[config.field].length) {
          left[config.field][i] += right[config.field][i];
        } else {
          left[config.field].push(right[config.field][i]);
        }
      }
    } else if (type === "number") {
      left[config.field] += right[config.field];
    } else {
      return null;
    }

    return left;
  }

  /**
   * Stores the maximum value of the field in left.
   *
   * Config:
   *   field              The field to vector add
   */
  combinerMax(left, right, config) {
    if (!(config.field in right)) {
      return left;
    }
    let type = this._typeOf(right[config.field]);
    if (!(config.field in left)) {
      if (type === "map") {
        left[config.field] = {};
      } else if (type === "array") {
        left[config.field] = [];
      } else if (type === "number") {
        left[config.field] = 0;
      } else {
        return null;
      }
    }
    if (type !== this._typeOf(left[config.field])) {
      return null;
    }
    if (type === "map") {
      Object.keys(right[config.field]).forEach(key => {
        if (
          !(key in left[config.field]) ||
          right[config.field][key] > left[config.field][key]
        ) {
          left[config.field][key] = right[config.field][key];
        }
      });
    } else if (type === "array") {
      for (let i = 0; i < right[config.field].length; i++) {
        if (i < left[config.field].length) {
          if (left[config.field][i] < right[config.field][i]) {
            left[config.field][i] = right[config.field][i];
          }
        } else {
          left[config.field].push(right[config.field][i]);
        }
      }
    } else if (type === "number") {
      if (left[config.field] < right[config.field]) {
        left[config.field] = right[config.field];
      }
    } else {
      return null;
    }

    return left;
  }

  /**
   * Associates a value in right with another value in right. This association
   * is then stored in a map in left.
   *
   *     For example: If a sequence of rights is:
   *     { 'tags': {}, 'url_domain': 'maseratiusa.com/maserati', 'time': 41 }
   *     { 'tags': {}, 'url_domain': 'mbusa.com/mercedes',       'time': 21 }
   *     { 'tags': {}, 'url_domain': 'maseratiusa.com/maserati', 'time': 34 }
   *
   *     Then assuming a 'sum' operation, left can build a map that would look like:
   *     {
   *         'maseratiusa.com/maserati': 75,
   *         'mbusa.com/mercedes': 21,
   *     }
   *
   * Fields:
   *  left_field              field in the left to store / update the map
   *  right_key_field         Field in the right to use as a key
   *  right_value_field       Field in the right to use as a value
   *  operation               One of "sum", "max", "overwrite", "count"
   */
  combinerCollectValues(left, right, config) {
    let op;
    if (config.operation === "sum") {
      op = (a, b) => a + b;
    } else if (config.operation === "max") {
      op = (a, b) => (a > b ? a : b);
    } else if (config.operation === "overwrite") {
      op = (a, b) => b;
    } else if (config.operation === "count") {
      op = (a, b) => a + 1;
    } else {
      return null;
    }
    if (!(config.left_field in left)) {
      left[config.left_field] = {};
    }
    if (
      !(config.right_key_field in right) ||
      !(config.right_value_field in right)
    ) {
      return left;
    }

    let key = right[config.right_key_field];
    let rightValue = right[config.right_value_field];
    let leftValue = 0.0;
    if (key in left[config.left_field]) {
      leftValue = left[config.left_field][key];
    }

    left[config.left_field][key] = op(leftValue, rightValue);

    return left;
  }

  /**
   * Executes a recipe. Returns an object on success, or null on failure.
   */
  executeRecipe(item, recipe) {
    let newItem = item;
    if (recipe) {
      for (let step of recipe) {
        let op = this.ITEM_BUILDER_REGISTRY[step.function];
        if (op === undefined) {
          return null;
        }
        newItem = op.call(this, newItem, step);
        if (newItem === null) {
          break;
        }
      }
    }
    return newItem;
  }

  /**
   * Executes a recipe. Returns an object on success, or null on failure.
   */
  executeCombinerRecipe(item1, item2, recipe) {
    let newItem1 = item1;
    for (let step of recipe) {
      let op = this.ITEM_COMBINER_REGISTRY[step.function];
      if (op === undefined) {
        return null;
      }
      newItem1 = op.call(this, newItem1, item2, step);
      if (newItem1 === null) {
        break;
      }
    }

    return newItem1;
  }
};