private static POSTaggerFactory create(Dictionary ngram, TagDictionary pos) { return new POSTaggerFactory(ngram, pos); } }
&& this.factory.getTagDictionary() == null) { this.factory.setTagDictionary(this.factory .createTagDictionary(tagDictionaryFile)); dict = this.factory.getTagDictionary(); if (dict == null) { dict = this.factory.createEmptyTagDictionary(); this.factory = POSTaggerFactory.create(this.factoryClassName, null, null); factory.init(featureGeneratorBytes, resources, dict); this.factory.setTagDictionary(null);
@Override public Map<String, Object> createArtifactMap() { Map<String, Object> artifactMap = super.createArtifactMap(); artifactMap.put(MORFOLOGIK_POSDICT, this.dictData); artifactMap.put(MORFOLOGIK_DICT_INFO, this.dictInfo); return artifactMap; }
@Deprecated public static POSTaggerFactory create(String subclassName, Dictionary ngramDictionary, TagDictionary posDictionary) throws InvalidFormatException { if (subclassName == null) { // will create the default factory return new POSTaggerFactory(ngramDictionary, posDictionary); } try { POSTaggerFactory theFactory = ExtensionLoader.instantiateExtension( POSTaggerFactory.class, subclassName); theFactory.init(ngramDictionary, posDictionary); return theFactory; } catch (Exception e) { String msg = "Could not instantiate the " + subclassName + ". The initialization throw an exception."; throw new InvalidFormatException(msg, e); } }
/** * Initializes the current instance with the provided model. * * @param model */ public POSTaggerME(POSModel model) { POSTaggerFactory factory = model.getFactory(); int beamSize = POSTaggerME.DEFAULT_BEAM_SIZE; String beamSizeString = model.getManifestProperty(BeamSearch.BEAM_SIZE_PARAMETER); if (beamSizeString != null) { beamSize = Integer.parseInt(beamSizeString); } modelPackage = model; contextGen = factory.getPOSContextGenerator(beamSize); tagDictionary = factory.getTagDictionary(); size = beamSize; sequenceValidator = factory.getSequenceValidator(); if (model.getPosSequenceModel() != null) { this.model = model.getPosSequenceModel(); } else { this.model = new opennlp.tools.ml.BeamSearch<>(beamSize, model.getPosModel(), 0); } }
@Test public void testPOSTaggerWithDefaultFactory() throws IOException { POSDictionary posDict = POSDictionary.create(POSDictionaryTest.class .getResourceAsStream("TagDictionaryCaseSensitive.xml")); POSModel posModel = trainPOSModel(new POSTaggerFactory(null, null, posDict)); POSTaggerFactory factory = posModel.getFactory(); Assert.assertTrue(factory.getTagDictionary() instanceof POSDictionary); Assert.assertTrue(factory.getPOSContextGenerator() != null); Assert.assertTrue(factory.getSequenceValidator() instanceof DefaultPOSSequenceValidator); ByteArrayOutputStream out = new ByteArrayOutputStream(); posModel.serialize(out); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); POSModel fromSerialized = new POSModel(in); factory = fromSerialized.getFactory(); Assert.assertTrue(factory.getTagDictionary() instanceof POSDictionary); Assert.assertTrue(factory.getPOSContextGenerator() != null); Assert.assertTrue(factory.getSequenceValidator() instanceof DefaultPOSSequenceValidator); }
if (aDictCutOff != Flags.DEFAULT_DICT_CUTOFF) { try { TagDictionary dict = getPosTaggerFactory().getTagDictionary(); if (dict == null) { dict = getPosTaggerFactory().createEmptyTagDictionary(); getPosTaggerFactory().setTagDictionary(dict);
public TagDictionary createTagDictionary(File dictionary) throws IOException { return createTagDictionary(new FileInputStream(dictionary)); }
/** * Create a tag dictionary with the dictionary contained in the dictPath. * * @param dictPath * the string pointing to the tag dictionary */ protected final void createTagDictionary(final String dictPath) { if (!dictPath.equalsIgnoreCase(Flags.DEFAULT_DICT_PATH)) { try { getPosTaggerFactory().setTagDictionary( getPosTaggerFactory().createTagDictionary(new File(dictPath))); } catch (final IOException e) { throw new TerminateToolException(-1, "IO error while loading POS Dictionary: " + e.getMessage(), e); } } }
public POSContextGenerator getPOSContextGenerator() { return getPOSContextGenerator(0); }
@Test(expected = InvalidFormatException.class) public void testCreateWithHierarchy2() throws InvalidFormatException { POSTaggerFactory.create(this.getClass().getCanonicalName(), null, null); } }
public SequenceValidator<String> getSequenceValidator() { return new DefaultPOSSequenceValidator(getTagDictionary()); }
@Override @SuppressWarnings("rawtypes") public Map<String, ArtifactSerializer> createArtifactSerializersMap() { Map<String, ArtifactSerializer> serializers = super.createArtifactSerializersMap(); serializers.put(MORFOLOGIK_POSDICT_SUF, new ByteArraySerializer()); serializers.put(MORFOLOGIK_DICT_INFO_SUF, new ByteArraySerializer()); return serializers; }
@Override protected void init(Dictionary ngramDictionary, TagDictionary posDictionary) { super.init(ngramDictionary, null); this.dict = posDictionary; }
public static POSTaggerFactory create(String subclassName, byte[] featureGeneratorBytes, Map<String, Object> resources, TagDictionary posDictionary) throws InvalidFormatException { POSTaggerFactory theFactory; if (subclassName == null) { // will create the default factory theFactory = new POSTaggerFactory(null, posDictionary); } else { try { theFactory = ExtensionLoader.instantiateExtension( POSTaggerFactory.class, subclassName); } catch (Exception e) { String msg = "Could not instantiate the " + subclassName + ". The initialization throw an exception."; throw new InvalidFormatException(msg, e); } } theFactory.init(featureGeneratorBytes, resources, posDictionary); return theFactory; }
@Test public void testPOSTaggerWithCustomFactory() throws IOException { DummyPOSDictionary posDict = new DummyPOSDictionary( POSDictionary.create(POSDictionaryTest.class .getResourceAsStream("TagDictionaryCaseSensitive.xml"))); POSModel posModel = trainPOSModel(new DummyPOSTaggerFactory(posDict)); POSTaggerFactory factory = posModel.getFactory(); Assert.assertTrue(factory.getTagDictionary() instanceof DummyPOSDictionary); Assert.assertTrue(factory.getPOSContextGenerator() instanceof DummyPOSContextGenerator); Assert.assertTrue(factory.getSequenceValidator() instanceof DummyPOSSequenceValidator); ByteArrayOutputStream out = new ByteArrayOutputStream(); posModel.serialize(out); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); POSModel fromSerialized = new POSModel(in); factory = fromSerialized.getFactory(); Assert.assertTrue(factory.getTagDictionary() instanceof DummyPOSDictionary); Assert.assertTrue(factory.getPOSContextGenerator() instanceof DummyPOSContextGenerator); Assert.assertTrue(factory.getSequenceValidator() instanceof DummyPOSSequenceValidator); }
@Override public TagDictionary createTagDictionary(InputStream in) throws InvalidFormatException, IOException { return super.createTagDictionary(in); }
public POSContextGenerator getPOSContextGenerator() { return getPOSContextGenerator(0); }
@Test(expected = InvalidFormatException.class) public void testCreateWithInvalidName2() throws InvalidFormatException { POSTaggerFactory.create("X", null, null); }
public SequenceValidator<String> getSequenceValidator() { return new DefaultPOSSequenceValidator(getTagDictionary()); }