|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package opennlp.dl.doccat; |
| 18 | + |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 23 | + |
| 24 | + |
| 25 | +public class DocumentCategorizerConfigTest { |
| 26 | + |
| 27 | + @Test |
| 28 | + public void testId2LabelsFromJsonPrettyValid() { |
| 29 | + final String json = """ |
| 30 | + { |
| 31 | + "_num_labels": 5, |
| 32 | + "architectures": [ |
| 33 | + "BertForSequenceClassification" |
| 34 | + ], |
| 35 | + "attention_probs_dropout_prob": 0.1, |
| 36 | + "directionality": "bidi", |
| 37 | + "finetuning_task": "sentiment-analysis", |
| 38 | + "hidden_act": "gelu", |
| 39 | + "hidden_dropout_prob": 0.1, |
| 40 | + "hidden_size": 768, |
| 41 | + "id2label": { |
| 42 | + "0": "1 star", |
| 43 | + "1": "2 stars", |
| 44 | + "2": "3 stars", |
| 45 | + "3": "4 stars", |
| 46 | + "4": "5 stars" |
| 47 | + }, |
| 48 | + "initializer_range": 0.02, |
| 49 | + "intermediate_size": 3072, |
| 50 | + "label2id": { |
| 51 | + "1 star": 0, |
| 52 | + "2 stars": 1, |
| 53 | + "3 stars": 2, |
| 54 | + "4 stars": 3, |
| 55 | + "5 stars": 4 |
| 56 | + }, |
| 57 | + "layer_norm_eps": 1e-12, |
| 58 | + "max_position_embeddings": 512, |
| 59 | + "model_type": "bert", |
| 60 | + "num_attention_heads": 12, |
| 61 | + "num_hidden_layers": 12, |
| 62 | + "output_past": true, |
| 63 | + "pad_token_id": 0, |
| 64 | + "pooler_fc_size": 768, |
| 65 | + "pooler_num_attention_heads": 12, |
| 66 | + "pooler_num_fc_layers": 3, |
| 67 | + "pooler_size_per_head": 128, |
| 68 | + "pooler_type": "first_token_transform", |
| 69 | + "type_vocab_size": 2, |
| 70 | + "vocab_size": 105879 |
| 71 | + } |
| 72 | + """; |
| 73 | + |
| 74 | + final DocumentCategorizerConfig config = DocumentCategorizerConfig.fromJson(json); |
| 75 | + assertNotNull(config); |
| 76 | + assertEquals(5, config.id2label().size()); |
| 77 | + assertEquals("1 star", config.id2label().get("0")); |
| 78 | + assertEquals("2 stars", config.id2label().get("1")); |
| 79 | + assertEquals("3 stars", config.id2label().get("2")); |
| 80 | + assertEquals("4 stars", config.id2label().get("3")); |
| 81 | + assertEquals("5 stars", config.id2label().get("4")); |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testId2LabelsFromJsonUglyValid() { |
| 86 | + final String json = """ |
| 87 | + {"_num_labels":5,"architectures":["BertForSequenceClassification"],"attention_probs_ |
| 88 | + dropout_prob":0.1,"directionality":"bidi","finetuning_task":"sentiment-analysis", |
| 89 | + "hidden_act":"gelu","hidden_dropout_prob":0.1,"hidden_size":768,"id2label":{"0":"1 star", |
| 90 | + "1":"2 stars","2":"3 stars","3":"4 stars","4":"5 stars"},"initializer_range":0.02, |
| 91 | + "intermediate_size":3072,"label2id":{"1 star":0,"2 stars":1,"3 stars":2,"4 stars":3,"5 |
| 92 | + stars":4},"layer_norm_eps":1e-12,"max_position_embeddings":512,"model_type":"bert", |
| 93 | + "num_attention_heads":12,"num_hidden_layers":12,"output_past":true,"pad_token_id":0," |
| 94 | + pooler_fc_size":768,"pooler_num_attention_heads":12,"pooler_num_fc_layers":3, |
| 95 | + "pooler_size_per_head":128,"pooler_type":"first_token_transform","type_vocab_size":2, |
| 96 | + "vocab_size":105879} |
| 97 | + """; |
| 98 | + |
| 99 | + final DocumentCategorizerConfig config = DocumentCategorizerConfig.fromJson(json); |
| 100 | + assertNotNull(config); |
| 101 | + assertEquals(5, config.id2label().size()); |
| 102 | + assertEquals("1 star", config.id2label().get("0")); |
| 103 | + assertEquals("2 stars", config.id2label().get("1")); |
| 104 | + assertEquals("3 stars", config.id2label().get("2")); |
| 105 | + assertEquals("4 stars", config.id2label().get("3")); |
| 106 | + assertEquals("5 stars", config.id2label().get("4")); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testId2LabelsFromJsonNoValues() { |
| 111 | + final String json = """ |
| 112 | + {"_num_labels":5,"architectures":["BertForSequenceClassification"],"attention_probs |
| 113 | + _dropout_prob":0.1,"directionality":"bidi","finetuning_task":"sentiment-analysis", |
| 114 | + "hidden_act":"gelu","hidden_dropout_prob":0.1,"hidden_size":768,"layer_norm_eps":1e-12, |
| 115 | + "max_position_embeddings":512,"model_type":"bert", |
| 116 | + "num_attention_heads":12,"num_hidden_layers":12,"output_past":true,"pad_token_id":0, |
| 117 | + "pooler_fc_size":768,"pooler_num_attention_heads":12,"pooler_num_fc_layers":3, |
| 118 | + "pooler_size_per_head":128,"pooler_type":"first_token_transform","type_vocab_size":2, |
| 119 | + "vocab_size":105879} |
| 120 | + """; |
| 121 | + |
| 122 | + final DocumentCategorizerConfig config = DocumentCategorizerConfig.fromJson(json); |
| 123 | + assertNotNull(config); |
| 124 | + assertEquals(0, config.id2label().size()); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void testId2LabelsFromJsonEmptyInput() { |
| 129 | + final String json = ""; |
| 130 | + final DocumentCategorizerConfig config = DocumentCategorizerConfig.fromJson(json); |
| 131 | + assertNotNull(config); |
| 132 | + assertEquals(0, config.id2label().size()); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void testId2LabelsFromJsonPrettyIdIsNotANumberValid() { |
| 137 | + final String json = """ |
| 138 | + { |
| 139 | + "_num_labels": 5, |
| 140 | + "architectures": [ |
| 141 | + "BertForSequenceClassification" |
| 142 | + ], |
| 143 | + "attention_probs_dropout_prob": 0.1, |
| 144 | + "directionality": "bidi", |
| 145 | + "finetuning_task": "sentiment-analysis", |
| 146 | + "hidden_act": "gelu", |
| 147 | + "hidden_dropout_prob": 0.1, |
| 148 | + "hidden_size": 768, |
| 149 | + "id2label": { |
| 150 | + "a0": "1 star", |
| 151 | + "a1": "2 stars", |
| 152 | + "a2": "3 stars", |
| 153 | + "a3": "4 stars", |
| 154 | + "a4": "5 stars" |
| 155 | + }, |
| 156 | + "initializer_range": 0.02, |
| 157 | + "intermediate_size": 3072, |
| 158 | + "label2id": { |
| 159 | + "1 star": "a0", |
| 160 | + "2 stars": "a1", |
| 161 | + "3 stars": "a2", |
| 162 | + "4 stars": "a3", |
| 163 | + "5 stars": "a4" |
| 164 | + }, |
| 165 | + "layer_norm_eps": 1e-12, |
| 166 | + "max_position_embeddings": 512, |
| 167 | + "model_type": "bert", |
| 168 | + "num_attention_heads": 12, |
| 169 | + "num_hidden_layers": 12, |
| 170 | + "output_past": true, |
| 171 | + "pad_token_id": 0, |
| 172 | + "pooler_fc_size": 768, |
| 173 | + "pooler_num_attention_heads": 12, |
| 174 | + "pooler_num_fc_layers": 3, |
| 175 | + "pooler_size_per_head": 128, |
| 176 | + "pooler_type": "first_token_transform", |
| 177 | + "type_vocab_size": 2, |
| 178 | + "vocab_size": 105879 |
| 179 | + } |
| 180 | + """; |
| 181 | + |
| 182 | + final DocumentCategorizerConfig config = DocumentCategorizerConfig.fromJson(json); |
| 183 | + assertNotNull(config); |
| 184 | + assertEquals(5, config.id2label().size()); |
| 185 | + assertEquals("1 star", config.id2label().get("a0")); |
| 186 | + assertEquals("2 stars", config.id2label().get("a1")); |
| 187 | + assertEquals("3 stars", config.id2label().get("a2")); |
| 188 | + assertEquals("4 stars", config.id2label().get("a3")); |
| 189 | + assertEquals("5 stars", config.id2label().get("a4")); |
| 190 | + } |
| 191 | +} |
0 commit comments