summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/test/unit/lib/PersonalityProvider/Tokenize.test.js
blob: 8503c2903b5668df943160790a3981750d69eb7d (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
import {
  tokenize,
  toksToTfIdfVector,
} from "lib/PersonalityProvider/Tokenize.jsm";

const EPSILON = 0.00001;

describe("TF-IDF Term Vectorizer", () => {
  describe("#tokenize", () => {
    let testCases = [
      { input: "HELLO there", expected: ["hello", "there"] },
      { input: "blah,,,blah,blah", expected: ["blah", "blah", "blah"] },
      {
        input: "Call Jenny: 967-5309",
        expected: ["call", "jenny", "967", "5309"],
      },
      {
        input: "Yo(what)[[hello]]{{jim}}}bob{1:2:1+2=$3",
        expected: [
          "yo",
          "what",
          "hello",
          "jim",
          "bob",
          "1",
          "2",
          "1",
          "2",
          "3",
        ],
      },
      { input: "čÄfė 80's", expected: ["čäfė", "80", "s"] },
      { input: "我知道很多东西。", expected: ["我知道很多东西"] },
    ];
    let checkTokenization = tc => {
      it(`${tc.input} should tokenize to ${tc.expected}`, () => {
        assert.deepEqual(tc.expected, tokenize(tc.input));
      });
    };

    for (let i = 0; i < testCases.length; i++) {
      checkTokenization(testCases[i]);
    }
  });

  describe("#tfidf", () => {
    let vocab_idfs = {
      deal: [221, 5.5058519847862275],
      easy: [269, 5.5058519847862275],
      tanks: [867, 5.601162164590552],
      sites: [792, 5.957837108529285],
      care: [153, 5.957837108529285],
      needs: [596, 5.824305715904762],
      finally: [334, 5.706522680248379],
    };
    let testCases = [
      {
        input: "Finally! Easy care for your tanks!",
        expected: {
          finally: [334, 0.5009816295853761],
          easy: [269, 0.48336453811728713],
          care: [153, 0.5230447876368227],
          tanks: [867, 0.49173191907236774],
        },
      },
      {
        input: "Easy easy EASY",
        expected: { easy: [269, 1.0] },
      },
      {
        input: "Easy easy care",
        expected: {
          easy: [269, 0.8795205218806832],
          care: [153, 0.4758609582543317],
        },
      },
      {
        input: "easy care",
        expected: {
          easy: [269, 0.6786999710383944],
          care: [153, 0.7344156515982504],
        },
      },
      {
        input: "这个空间故意留空。",
        expected: {
          /* This space is left intentionally blank. */
        },
      },
    ];
    let checkTokenGeneration = tc => {
      describe(`${tc.input} should have only vocabulary tokens`, () => {
        let actual = toksToTfIdfVector(tokenize(tc.input), vocab_idfs);

        it(`${tc.input} should generate exactly ${Object.keys(
          tc.expected
        )}`, () => {
          let seen = {};
          Object.keys(actual).forEach(actualTok => {
            assert.isTrue(actualTok in tc.expected);
            seen[actualTok] = true;
          });
          Object.keys(tc.expected).forEach(expectedTok => {
            assert.isTrue(expectedTok in seen);
          });
        });

        it(`${tc.input} should have the correct token ids`, () => {
          Object.keys(actual).forEach(actualTok => {
            assert.equal(tc.expected[actualTok][0], actual[actualTok][0]);
          });
        });
      });
    };

    let checkTfIdfVector = tc => {
      let actual = toksToTfIdfVector(tokenize(tc.input), vocab_idfs);
      it(`${tc.input} should have the correct tf-idf`, () => {
        Object.keys(actual).forEach(actualTok => {
          let delta = Math.abs(
            tc.expected[actualTok][1] - actual[actualTok][1]
          );
          assert.isTrue(delta <= EPSILON);
        });
      });
    };

    // run the tests
    for (let i = 0; i < testCases.length; i++) {
      checkTokenGeneration(testCases[i]);
      checkTfIdfVector(testCases[i]);
    }
  });
});