|
23 | 23 | import java.io.IOException;
|
24 | 24 | import java.io.InputStream;
|
25 | 25 | import java.io.InputStreamReader;
|
| 26 | +import java.net.URISyntaxException; |
| 27 | +import java.net.URL; |
26 | 28 | import java.nio.charset.StandardCharsets;
|
27 | 29 | import java.nio.file.Files;
|
28 | 30 | import java.nio.file.Path;
|
| 31 | +import java.nio.file.StandardCopyOption; |
29 | 32 | import java.util.Map;
|
30 | 33 | import java.util.stream.Collectors;
|
31 | 34 |
|
32 | 35 | import org.junit.jupiter.api.Assertions;
|
33 | 36 | import org.junit.jupiter.api.Test;
|
34 | 37 |
|
| 38 | +import opennlp.tools.EnabledWhenCDNAvailable; |
| 39 | +import opennlp.tools.cmdline.AbstractModelLoaderTest; |
35 | 40 | import opennlp.tools.cmdline.TerminateToolException;
|
36 | 41 | import opennlp.tools.cmdline.namefind.TokenNameFinderTrainerTool;
|
37 | 42 | import opennlp.tools.postag.POSModel;
|
|
43 | 48 | import opennlp.tools.util.TrainingParameters;
|
44 | 49 | import opennlp.tools.util.model.ModelType;
|
45 | 50 |
|
46 |
| -public class TokenNameFinderModelTest { |
| 51 | +public class TokenNameFinderModelTest extends AbstractModelLoaderTest { |
47 | 52 |
|
48 | 53 | @Test
|
49 | 54 | void testNERWithPOSModel() throws IOException {
|
@@ -104,4 +109,80 @@ void testNERWithPOSModel() throws IOException {
|
104 | 109 | FileUtil.deleteDirectory(resourcesFolder.toFile());
|
105 | 110 | }
|
106 | 111 | }
|
| 112 | + |
| 113 | + /* |
| 114 | + * OPENNLP-1369 |
| 115 | + */ |
| 116 | + @EnabledWhenCDNAvailable(hostname = "opennlp.sourceforge.net") |
| 117 | + @Test |
| 118 | + void testNERWithPOSModelV15() throws IOException, URISyntaxException { |
| 119 | + |
| 120 | + // 0. Download model from sourceforge and place at the right location |
| 121 | + final String modelName = "pt-pos-perceptron.bin"; |
| 122 | + |
| 123 | + downloadVersion15Model(modelName); |
| 124 | + |
| 125 | + final Path model = OPENNLP_DIR.resolve(modelName); |
| 126 | + final Path resourcesFolder = Files.createTempDirectory("resources").toAbsolutePath(); |
| 127 | + |
| 128 | + Assertions.assertNotNull(model); |
| 129 | + Assertions.assertNotNull(resourcesFolder); |
| 130 | + |
| 131 | + // 1. Copy the downloaded model to the temporary resource folder, so it can be referenced from |
| 132 | + // the feature gen xml file. |
| 133 | + |
| 134 | + final Path copy = resourcesFolder.resolve(modelName); |
| 135 | + |
| 136 | + Files.copy(OPENNLP_DIR.resolve(modelName), copy, StandardCopyOption.REPLACE_EXISTING); |
| 137 | + |
| 138 | + Assertions.assertTrue(copy.toFile().exists()); |
| 139 | + |
| 140 | + // 2. Load feature generator xml bytes |
| 141 | + final URL featureGeneratorXmlUrl = this.getClass().getResource("ner-pos-features-v15.xml"); |
| 142 | + Assertions.assertNotNull(featureGeneratorXmlUrl); |
| 143 | + |
| 144 | + final Path featureGeneratorXmlPath = Path.of(featureGeneratorXmlUrl.toURI()); |
| 145 | + Assertions.assertNotNull(featureGeneratorXmlPath); |
| 146 | + |
| 147 | + final Path featureGenerator = Files.createTempFile("ner-featuregen-v15", ".xml"); |
| 148 | + Assertions.assertNotNull(featureGenerator); |
| 149 | + |
| 150 | + Files.copy(featureGeneratorXmlPath, featureGenerator, StandardCopyOption.REPLACE_EXISTING); |
| 151 | + Assertions.assertTrue(featureGenerator.toFile().exists()); |
| 152 | + |
| 153 | + Map<String, Object> resources; |
| 154 | + try { |
| 155 | + resources = TokenNameFinderTrainerTool.loadResources(resourcesFolder.toFile(), |
| 156 | + featureGenerator.toAbsolutePath().toFile()); |
| 157 | + } catch (IOException e) { |
| 158 | + throw new TerminateToolException(-1, e.getMessage(), e); |
| 159 | + } finally { |
| 160 | + Files.delete(featureGenerator); |
| 161 | + } |
| 162 | + |
| 163 | + |
| 164 | + // train a name finder |
| 165 | + ObjectStream<NameSample> sampleStream = new NameSampleDataStream( |
| 166 | + new PlainTextByLineStream(new MockInputStreamFactory( |
| 167 | + new File("opennlp/tools/namefind/voa1.train")), StandardCharsets.UTF_8)); |
| 168 | + |
| 169 | + TrainingParameters params = new TrainingParameters(); |
| 170 | + params.put(TrainingParameters.ITERATIONS_PARAM, 70); |
| 171 | + params.put(TrainingParameters.CUTOFF_PARAM, 1); |
| 172 | + |
| 173 | + TokenNameFinderModel nameFinderModel = NameFinderME.train("en", null, sampleStream, |
| 174 | + params, TokenNameFinderFactory.create(null, |
| 175 | + Files.readString(featureGeneratorXmlPath, StandardCharsets.UTF_8) |
| 176 | + .getBytes(StandardCharsets.UTF_8), resources, new BioCodec())); |
| 177 | + |
| 178 | + |
| 179 | + File nerModel = Files.createTempFile("nermodel", ".bin").toFile(); |
| 180 | + try (FileOutputStream modelOut = new FileOutputStream(nerModel)) { |
| 181 | + nameFinderModel.serialize(modelOut); |
| 182 | + Assertions.assertTrue(nerModel.exists()); |
| 183 | + } finally { |
| 184 | + Assertions.assertTrue(nerModel.delete()); |
| 185 | + FileUtil.deleteDirectory(resourcesFolder.toFile()); |
| 186 | + } |
| 187 | + } |
107 | 188 | }
|
0 commit comments