|
3 | 3 |
|
4 | 4 | class TestModel < Test::Unit::TestCase
|
5 | 5 |
|
6 |
| - def setup |
7 |
| - @features ||= [ |
8 |
| - [ [1,0.6], [11, 0.0], [34, 0.1] ], |
9 |
| - [ [5,0.4], [15, 0.0], [30, 0.1] ], |
10 |
| - [ [1,0.1], [13, 0.0], [31, 0.1] ], |
11 |
| - [ [7,0.7], [15, 0.0], [35, 0.1] ], |
12 |
| - [ [5,0.6], [19, 0.0], [44, 0.1] ], |
13 |
| - ] |
14 |
| - @docs_and_labels ||= @features.each_with_index.map{|f,i| [ Document.create(i + 1, 1, 0, 0, f), i%2 * -1]} |
15 |
| - end |
| 6 | + context "reading a model from file" do |
| 7 | + |
| 8 | + setup do |
| 9 | + @file_name = 'test/assets/model' |
| 10 | + end |
16 | 11 |
|
17 |
| - def test_learn_classification_with_alpha |
18 |
| - m = Model.new(:classification, @docs_and_labels, {}, {}, [1, 0.0] * 50) |
19 |
| - assert_kind_of Model, m |
| 12 | + should "read properly from a well formed file" do |
| 13 | + assert m = Model.read_from_file(@file_name) |
| 14 | + assert_equal 3877, m.support_vectors_count |
| 15 | + assert_equal 39118, m.total_words |
| 16 | + end |
20 | 17 |
|
21 |
| - @docs_and_labels.each_with_index do |item, i| |
22 |
| - assert_kind_of Numeric, m.classify(item.first), "failed in item # #{i}" |
| 18 | + should "classify successfully after reading the model from a file" do |
| 19 | + m = Model.read_from_file(@file_name) |
| 20 | + assert_kind_of Numeric, m.classify( Document.create(-1, 1, 0, 0,[1.0, 0, 0, 0, 0.5 ].each_with_index.map{|v, i| [i + 1 ,v.to_f]} ) ) |
| 21 | + assert_kind_of Numeric, m.classify( Document.create(-1, 1, 0, 0,[1.0, 0, 0, 0, 0.5 ].each_with_index.map{|v, i| [i + 1,v.to_f]}) ) |
| 22 | + assert_kind_of Numeric, m.classify( Document.create(-1, 1, 0, 0,[1, 0, 0, 0, 0.8, 0, 0 , 0 ].each_with_index.map{|v, i| [i + 1,v.to_f]}) ) |
| 23 | + assert_kind_of Numeric, m.classify( Document.create(-1, 1, 0, 0,[1, 0.5, 0, 0, 0, 0 , 0 ].each_with_index.map{|v, i| [i + 1,v.to_f]}) ) |
23 | 24 | end
|
24 | 25 | end
|
25 | 26 |
|
26 |
| - def test_learn_classification |
27 |
| - m = Model.new(:classification, @docs_and_labels, {}, {}, nil) |
28 |
| - assert_kind_of Model, m |
29 |
| - assert_equal 44, m.total_words |
30 |
| - assert_equal 5, m.totdoc |
| 27 | + context "when learning from new documents" do |
31 | 28 |
|
32 |
| - @docs_and_labels.each_with_index do |item, i| |
33 |
| - assert_kind_of Numeric, m.classify(item.first), "failed in item # #{i}" |
| 29 | + setup do |
| 30 | + @features ||= [ |
| 31 | + [ [1,0.6], [11, 0.0], [34, 0.1] ], |
| 32 | + [ [5,0.4], [15, 0.0], [30, 0.1] ], |
| 33 | + [ [1,0.1], [13, 0.0], [31, 0.1] ], |
| 34 | + [ [7,0.7], [15, 0.0], [35, 0.1] ], |
| 35 | + [ [5,0.6], [19, 0.0], [44, 0.1] ], |
| 36 | + ] |
| 37 | + |
| 38 | + @docs_and_labels ||= @features.each_with_index.map do |feature, index| |
| 39 | + [ Document.create(index + 1, 1, 0, 0, feature), index%2 * -1] |
| 40 | + end |
34 | 41 | end
|
35 | 42 |
|
36 |
| - end |
| 43 | + should "learn classification with default arguments" do |
| 44 | + m = Model.new(:classification, @docs_and_labels, {}, {}, nil) |
| 45 | + assert_kind_of Model, m |
| 46 | + assert_equal 44, m.total_words |
| 47 | + assert_equal 5, m.totdoc |
| 48 | + |
| 49 | + @docs_and_labels.each_with_index do |item, i| |
| 50 | + assert_kind_of Numeric, m.classify(item.first), "failed in item # #{i}" |
| 51 | + end |
| 52 | + end |
| 53 | + |
| 54 | + should "learn classification with alpha values" do |
| 55 | + m = Model.new(:classification, @docs_and_labels, {}, {}, [1, 0.0] * 50) |
| 56 | + assert_kind_of Model, m |
| 57 | + |
| 58 | + @docs_and_labels.each_with_index do |item, i| |
| 59 | + assert_kind_of Numeric, m.classify(item.first), "failed in item # #{i}" |
| 60 | + end |
| 61 | + end |
| 62 | + |
| 63 | + should "raise argument error when one of the alphas is not numeric " do |
| 64 | + assert_raises(ArgumentError){Model.new(:classification, @docs_and_labels, {}, {}, [1, {}] )} |
| 65 | + end |
37 | 66 |
|
38 |
| - def test_learn_classification_with_learn_params |
39 |
| - |
40 |
| - learn_params = { |
| 67 | + should "learn classification and accept learn parameters" do |
| 68 | + learn_params = { |
41 | 69 | "predfile" => "custom_file",
|
42 | 70 | "alphafile" => "alpha",
|
43 | 71 | "biased_hyperplane" => false,
|
44 | 72 | "sharedslack" => false,
|
45 | 73 | "remove_inconsistent" => true
|
46 |
| - } |
| 74 | + } |
47 | 75 |
|
48 |
| - m = Model.new(:classification, @docs_and_labels, learn_params, {}, nil) |
49 |
| - assert_kind_of Model, m |
| 76 | + m = Model.new(:classification, @docs_and_labels, learn_params, {}, nil) |
| 77 | + assert_kind_of Model, m |
50 | 78 |
|
51 |
| - @docs_and_labels.each_with_index do |item, i| |
52 |
| - assert_kind_of Numeric, m.classify(item.first), "failed in item # #{i}" |
| 79 | + @docs_and_labels.each_with_index do |item, i| |
| 80 | + assert_kind_of Numeric, m.classify(item.first), "failed in item # #{i}" |
| 81 | + end |
53 | 82 | end
|
54 |
| - end |
55 | 83 |
|
56 |
| - def test_learn_classification_with_invalid_learn_params |
57 |
| - learn_params = {"svm_c" => -1} |
58 |
| - assert_raises(ArgumentError){Model.new(:classification, @docs_and_labels, learn_params, {}, nil)} |
59 |
| - learn_params = {"svm_iter_to_shrink" => -1} |
60 |
| - assert_raises(ArgumentError){Model.new(:classification, @docs_and_labels, learn_params, {}, nil)} |
61 |
| - end |
| 84 | + should "raise argument error when learn parameters are invalid" do |
| 85 | + learn_params = {"svm_c" => -1} |
| 86 | + assert_raises(ArgumentError){Model.new(:classification, @docs_and_labels, learn_params, {}, nil)} |
| 87 | + learn_params = {"svm_iter_to_shrink" => -1} |
| 88 | + assert_raises(ArgumentError){Model.new(:classification, @docs_and_labels, learn_params, {}, nil)} |
| 89 | + end |
| 90 | + |
| 91 | + should "learn calssification while accepting kernel paramters" do |
62 | 92 |
|
63 |
| - def test_learn_classification_with_kernel_params |
64 |
| - |
65 |
| - kernel_params = { |
| 93 | + kernel_params = { |
66 | 94 | "poly_degree" => 3,
|
67 | 95 | "rbf_gamma" => 0.5,
|
68 | 96 | "coef_lin" => 0.4,
|
69 | 97 | "coef_const" => 0.56
|
70 |
| - } |
| 98 | + } |
71 | 99 |
|
72 |
| - m = Model.new(:classification, @docs_and_labels, {}, kernel_params, nil) |
73 |
| - assert_kind_of Model, m |
| 100 | + m = Model.new(:classification, @docs_and_labels, {}, kernel_params, nil) |
| 101 | + assert_kind_of Model, m |
74 | 102 |
|
75 |
| - @docs_and_labels.each_with_index do |item, i| |
76 |
| - assert_kind_of Numeric, m.classify(item.first), "failed in item # #{i}" |
77 |
| - end |
78 |
| - end |
79 |
| - |
80 |
| - def test_learn_classification_with_learn_params_when_predfile_is_not_string |
81 |
| - |
82 |
| - learn_params = { "predfile" => {}} |
83 |
| - |
84 |
| - assert_raise(ArgumentError) do |
85 |
| - Model.new(:classification, @docs_and_labels, learn_params, {}, [1, 0.0, 1]) |
| 103 | + @docs_and_labels.each_with_index do |item, i| |
| 104 | + assert_kind_of Numeric, m.classify(item.first), "failed in item # #{i}" |
| 105 | + end |
86 | 106 | end
|
87 | 107 |
|
88 |
| - end |
| 108 | + should "raise argument error when predfile is not string" do |
89 | 109 |
|
90 |
| - def test_learn_classification_fails_when_element_is_not_array |
91 |
| - @docs_and_labels << [] |
92 |
| - assert_raises(ArgumentError){Model.new(:classification, @docs_and_labels, {}, {}, nil)} |
93 |
| - end |
94 |
| - |
95 |
| - def test_learn_classification_fails_when_element_is_arry_with_the_wrong_types |
96 |
| - assert_raises(ArgumentError){Model.new(:classification, @docs_and_labels, {}, {}, [1, {}] )} |
97 |
| - end |
| 110 | + learn_params = { "predfile" => {}} |
98 | 111 |
|
99 |
| - def test_read |
100 |
| - assert m = Model.read_from_file('test/assets/model') |
101 |
| - assert_equal 3877, m.support_vectors_count |
102 |
| - assert_equal 39118, m.total_words |
103 |
| - end |
| 112 | + assert_raise(ArgumentError) do |
| 113 | + Model.new(:classification, @docs_and_labels, learn_params, {}, [1, 0.0, 1]) |
| 114 | + end |
| 115 | + end |
104 | 116 |
|
105 |
| - def test_classify |
106 |
| - m = Model.read_from_file('test/assets/model') |
107 |
| - assert_kind_of Numeric, m.classify( Document.create(-1, 1, 0, 0,[1.0, 0, 0, 0, 0.5 ].each_with_index.map{|v, i| [i + 1 ,v.to_f]} ) ) |
108 |
| - assert_kind_of Numeric, m.classify( Document.create(-1, 1, 0, 0,[1.0, 0, 0, 0, 0.5 ].each_with_index.map{|v, i| [i + 1,v.to_f]}) ) |
109 |
| - assert_kind_of Numeric, m.classify( Document.create(-1, 1, 0, 0,[1, 0, 0, 0, 0.8, 0, 0 , 0 ].each_with_index.map{|v, i| [i + 1,v.to_f]}) ) |
110 |
| - assert_kind_of Numeric, m.classify( Document.create(-1, 1, 0, 0,[1, 0.5, 0, 0, 0, 0 , 0 ].each_with_index.map{|v, i| [i + 1,v.to_f]}) ) |
111 | 117 | end
|
112 |
| - |
113 | 118 | end
|
114 | 119 |
|
0 commit comments