NaiveBayesTextTagger.test.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import {NaiveBayesTextTagger} from "lib/NaiveBayesTextTagger.jsm";
  2. import {tokenize} from "lib/Tokenize.jsm";
  3. const EPSILON = 0.00001;
  4. describe("Naive Bayes Tagger", () => {
  5. describe("#tag", () => {
  6. let model = {
  7. model_type: "nb",
  8. positive_class_label: "military",
  9. positive_class_id: 0,
  10. positive_class_threshold_log_prob: -0.5108256237659907,
  11. classes: [
  12. {
  13. log_prior: -0.6881346387364013,
  14. feature_log_probs: [
  15. -6.2149425847276,
  16. -6.829869141665873,
  17. -7.124856122235796,
  18. -7.116661287797188,
  19. -6.694751331313906,
  20. -7.11798266787003,
  21. -6.5094904366004185,
  22. -7.1639509366900604,
  23. -7.218981434452414,
  24. -6.854842907887801,
  25. -7.080328841624584,
  26. ],
  27. },
  28. {
  29. log_prior: -0.6981849745899025,
  30. feature_log_probs: [
  31. -7.0575941199203465,
  32. -6.632333513597953,
  33. -7.382756370680115,
  34. -7.1160793981275905,
  35. -8.467120918791892,
  36. -8.369201274990882,
  37. -8.518506617006922,
  38. -7.015756380369387,
  39. -7.739036845511857,
  40. -9.748294397894645,
  41. -3.9353548206941955,
  42. ],
  43. },
  44. ],
  45. vocab_idfs: {
  46. deal: [0, 5.5058519847862275],
  47. easy: [1, 5.5058519847862275],
  48. tanks: [2, 5.6011621645905520],
  49. sites: [3, 5.9578371085292850],
  50. care: [4, 5.9578371085292850],
  51. needs: [5, 5.8243057159047620],
  52. finally: [6, 5.7065226802483790],
  53. super: [7, 5.2646899279693390],
  54. heard: [8, 5.5058519847862275],
  55. reached: [9, 5.9578371085292850],
  56. words: [10, 5.0705339135283820],
  57. },
  58. };
  59. let instance = new NaiveBayesTextTagger(model);
  60. let testCases = [
  61. {
  62. input: "Finally! Super easy care for your tanks!",
  63. expected: {
  64. label: "military",
  65. logProb: -0.16299510296630082,
  66. confident: true,
  67. },
  68. },
  69. {
  70. input: "heard",
  71. expected: {
  72. label: "military",
  73. logProb: -0.4628170738373294,
  74. confident: false,
  75. },
  76. },
  77. {
  78. input: "words",
  79. expected: {
  80. label: null,
  81. logProb: -0.04258339303757985,
  82. confident: false,
  83. },
  84. },
  85. ];
  86. let checkTag = tc => {
  87. let actual = instance.tagTokens(tokenize(tc.input));
  88. it(`should tag ${tc.input} with ${tc.expected.label}`, () => {
  89. assert.equal(tc.expected.label, actual.label);
  90. });
  91. it(`should give ${tc.input} the correct probability`, () => {
  92. let delta = Math.abs(tc.expected.logProb - actual.logProb);
  93. assert.isTrue(delta <= EPSILON);
  94. });
  95. };
  96. // RELEASE THE TESTS!
  97. for (let tc of testCases) {
  98. checkTag(tc);
  99. }
  100. });
  101. });