private static void usage() { System.out.print("OpenNLP Morfologik Addon " + Version.currentVersion().toString() + ". "); System.out.println("Usage: " + CMD + " TOOL"); System.out.println("where TOOL is one of:"); // distance of tool name from line start int numberOfSpaces = -1; for (String toolName : toolLookupMap.keySet()) { if (toolName.length() > numberOfSpaces) { numberOfSpaces = toolName.length(); } } numberOfSpaces = numberOfSpaces + 4; for (CmdLineTool tool : toolLookupMap.values()) { System.out.print(" " + tool.getName()); for (int i = 0; i < Math.abs(tool.getName().length() - numberOfSpaces); i++) { System.out.print(" "); } System.out.println(tool.getShortDescription()); } System.out.println("All tools print help when invoked with help parameter"); System.out.println("Example: opennlp-morfologik-addon POSDictionaryBuilder help"); }
@Test public void testParse() { Version referenceVersion = Version.currentVersion(); Assert.assertEquals(referenceVersion, Version.parse(referenceVersion.toString())); Assert.assertEquals(new Version(1,5,2, false), Version.parse("1.5.2-incubating")); Assert.assertEquals(new Version(1,5,2, false), Version.parse("1.5.2")); }
version = Version.parse(versionString); if (!Version.currentVersion().equals(Version.DEV_VERSION)) { if (Version.currentVersion().getMajor() != version.getMajor() || Version.currentVersion().getMinor() - 4 > version.getMinor()) { throw new InvalidFormatException("Model version " + version + " is not supported by this (" + Version.currentVersion() + ") version of OpenNLP!"); if (!Version.currentVersion().isSnapshot() && version.isSnapshot()) { throw new InvalidFormatException("Model version " + version + " is a snapshot - snapshot models are not supported by this non-snapshot version (" + Version.currentVersion() + ") of OpenNLP!");
@Override public boolean equals(Object obj) { if (obj == this) { return true; } if (obj instanceof Version) { Version version = (Version) obj; return getMajor() == version.getMajor() && getMinor() == version.getMinor() && getRevision() == version.getRevision() && isSnapshot() == version.isSnapshot(); } return false; }
@Override protected void createArtifactSerializers( Map<String, ArtifactSerializer> serializers) { super.createArtifactSerializers(serializers); // In 1.6.x the headrules artifact is serialized with the new API // which uses the Serializeable interface // This change is not backward compatible with the 1.5.x models. // In order to laod 1.5.x model the English headrules serializer must be // put on the serializer map. if (getVersion().getMajor() == 1 && getVersion().getMinor() == 5) { serializers.put("headrules", new HeadRulesSerializer()); } serializers.put("postagger", new POSModelSerializer()); serializers.put("chunker", new ChunkerModelSerializer()); }
public POSContextGenerator getPOSContextGenerator(int cacheSize) { if (artifactProvider != null) { Properties manifest = (Properties) artifactProvider.getArtifact("manifest.properties"); String version = manifest.getProperty("OpenNLP-Version"); if (Version.parse(version).getMinor() < 8) { return new DefaultPOSContextGenerator(cacheSize, getDictionary()); } } return new ConfigurablePOSContextGenerator(cacheSize, createFeatureGenerators()); }
/** * Retrieves the OpenNLP version which was used * to create the model. * * @return the version */ public final Version getVersion() { String version = getManifestProperty(VERSION_PROPERTY); return Version.parse(version); }
/** * Return a new {@link Version} initialized to the value * represented by the specified {@link String} * * @param version the string to be parsed * * @return the version represented by the string value * * @throws NumberFormatException if the string does * not contain a valid version */ public static Version parse(String version) { int indexFirstDot = version.indexOf('.'); int indexSecondDot = version.indexOf('.', indexFirstDot + 1); if (indexFirstDot == -1 || indexSecondDot == -1) { throw new NumberFormatException("Invalid version format '" + version + "', expected two dots!"); } int indexFirstDash = version.indexOf('-'); int versionEnd; if (indexFirstDash == -1) { versionEnd = version.length(); } else { versionEnd = indexFirstDash; } boolean snapshot = version.endsWith(SNAPSHOT_MARKER); return new Version(Integer.parseInt(version.substring(0, indexFirstDot)), Integer.parseInt(version.substring(indexFirstDot + 1, indexSecondDot)), Integer.parseInt(version.substring(indexSecondDot + 1, versionEnd)), snapshot); }
@Override public int hashCode() { return Objects.hash(getMajor(), getMinor(), getRevision(), isSnapshot()); }
@Override protected void validateArtifactMap() throws InvalidFormatException { super.validateArtifactMap(); if (!(artifactMap.get(CHUNKER_MODEL_ENTRY_NAME) instanceof AbstractModel)) { throw new InvalidFormatException("Chunker model is incomplete!"); } // Since 1.8.0 we changed the ChunkerFactory signature. This will check the if the model // declares a not default factory, and if yes, check if it was created before 1.8 if ( (getManifestProperty(FACTORY_NAME) != null && !getManifestProperty(FACTORY_NAME).equals("opennlp.tools.chunker.ChunkerFactory") ) && this.getVersion().getMajor() <= 1 && this.getVersion().getMinor() < 8) { throw new InvalidFormatException("The Chunker factory '" + getManifestProperty(FACTORY_NAME) + "' is no longer compatible. Please update it to match the latest ChunkerFactory."); } }
public POSContextGenerator getPOSContextGenerator(int cacheSize) { if (artifactProvider != null) { Properties manifest = (Properties) artifactProvider.getArtifact("manifest.properties"); String version = manifest.getProperty("OpenNLP-Version"); if (Version.parse(version).getMinor() < 8) { return new DefaultPOSContextGenerator(cacheSize, getDictionary()); } } return new ConfigurablePOSContextGenerator(cacheSize, createFeatureGenerators()); }
/** * Retrieves the current version of the OpenNlp Tools library. * * @return the current version */ public static Version currentVersion() { Properties manifest = new Properties(); // Try to read the version from the version file if it is available, // otherwise set the version to the development version try (InputStream versionIn = Version.class.getResourceAsStream("opennlp.version")) { if (versionIn != null) { manifest.load(versionIn); } } catch (IOException e) { // ignore error } String versionString = manifest.getProperty("OpenNLP-Version", DEV_VERSION_STRING); if (versionString.equals("${pom.version}")) versionString = DEV_VERSION_STRING; return Version.parse(versionString); } }
/** * Return a new {@link Version} initialized to the value * represented by the specified {@link String} * * @param version the string to be parsed * * @return the version represented by the string value * * @throws NumberFormatException if the string does * not contain a valid version */ public static Version parse(String version) { int indexFirstDot = version.indexOf('.'); int indexSecondDot = version.indexOf('.', indexFirstDot + 1); if (indexFirstDot == -1 || indexSecondDot == -1) { throw new NumberFormatException("Invalid version format '" + version + "', expected two dots!"); } int indexFirstDash = version.indexOf('-'); int versionEnd; if (indexFirstDash == -1) { versionEnd = version.length(); } else { versionEnd = indexFirstDash; } boolean snapshot = version.endsWith(SNAPSHOT_MARKER); return new Version(Integer.parseInt(version.substring(0, indexFirstDot)), Integer.parseInt(version.substring(indexFirstDot + 1, indexSecondDot)), Integer.parseInt(version.substring(indexSecondDot + 1, versionEnd)), snapshot); }
private static void usage() { System.out.print("OpenNLP " + Version.currentVersion().toString() + ". "); System.out.println("Usage: " + CMD + " TOOL"); System.out.println("where TOOL is one of:"); // distance of tool name from line start int numberOfSpaces = -1; for (String toolName : toolLookupMap.keySet()) { if (toolName.length() > numberOfSpaces) { numberOfSpaces = toolName.length(); } } numberOfSpaces = numberOfSpaces + 4; for (CmdLineTool tool : toolLookupMap.values()) { System.out.print(" " + tool.getName()); for (int i = 0; i < Math.abs(tool.getName().length() - numberOfSpaces); i++) { System.out.print(" "); } System.out.println(tool.getShortDescription()); } System.out.println("All tools print help when invoked with help parameter"); System.out.println("Example: opennlp SimpleTokenizer help"); }
version = Version.parse(versionString); if (!Version.currentVersion().equals(Version.DEV_VERSION)) { if (Version.currentVersion().getMajor() != version.getMajor() || Version.currentVersion().getMinor() - 3 > version.getMinor()) { throw new InvalidFormatException("Model version " + version + " is not supported by this (" + Version.currentVersion() + ") version of OpenNLP!"); if (!Version.currentVersion().isSnapshot() && version.isSnapshot()) { throw new InvalidFormatException("Model version " + version + " is a snapshot - snapshot models are not supported by this non-snapshot version (" + Version.currentVersion() + ") of OpenNLP!");
/** * Retrieves the version string. * * The {@link #parse(String)} method can create an instance * of {@link Version} with the returned version value string. * * @return the version value string */ @Override public String toString() { return Integer.toString(getMajor()) + "." + Integer.toString(getMinor()) + "." + Integer.toString(getRevision()) + (isSnapshot() ? SNAPSHOT_MARKER : ""); }
public POSModel create(InputStream in) throws IOException { POSModel posModel = new POSModel(new UncloseableInputStream(in)); // The 1.6.x models write the non-default beam size into the model itself. // In 1.5.x the parser configured the beam size when the model was loaded, // this is not possible anymore with the new APIs Version version = posModel.getVersion(); if (version.getMajor() == 1 && version.getMinor() == 5) { if (posModel.getManifestProperty(BeamSearch.BEAM_SIZE_PARAMETER) == null) { Map<String, String> manifestInfoEntries = new HashMap<>(); // The version in the model must be correct or otherwise version // dependent code branches in other places fail manifestInfoEntries.put("OpenNLP-Version", "1.5.0"); posModel = new POSModel(posModel.getLanguage(), posModel.getPosModel(), 10, manifestInfoEntries, posModel.getFactory()); } } return posModel; }
public POSContextGenerator getPOSContextGenerator(int cacheSize) { if (artifactProvider != null) { Properties manifest = (Properties) artifactProvider.getArtifact("manifest.properties"); String version = manifest.getProperty("OpenNLP-Version"); if (Version.parse(version).getMinor() < 8) { return new DefaultPOSContextGenerator(cacheSize, getDictionary()); } } return new ConfigurablePOSContextGenerator(cacheSize, createFeatureGenerators()); }
@Test public void testParseInvalidVersion() { try { Version.parse("1.5."); } catch (NumberFormatException e) { return; } Assert.assertFalse(false); }