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