private static POSTaggerFactory create(Dictionary ngram, TagDictionary pos) { return new POSTaggerFactory(ngram, pos); } }
@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); } }
@SuppressWarnings("unchecked") public Event[] updateContext(Sequence sequence, AbstractModel model) { Sequence<POSSample> pss = sequence; POSTagger tagger = new POSTaggerME(new POSModel("x-unspecified", model, null, new POSTaggerFactory())); String[] sentence = pss.getSource().getSentence(); Object[] ac = pss.getSource().getAddictionalContext(); String[] tags = tagger.tag(pss.getSource().getSentence()); Event[] events = new Event[sentence.length]; POSSampleEventStream.generateEvents(sentence, tags, ac, pcg) .toArray(events); return events; }
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; }
private void crossEval(TrainingParameters params, double expectedScore) throws IOException { try (ObjectStream<POSSample> samples = createPOSSampleStream()) { POSTaggerCrossValidator cv = new POSTaggerCrossValidator("eng", params, new POSTaggerFactory()); cv.evaluate(samples, 5); Assert.assertEquals(expectedScore, cv.getWordAccuracy(), 0.0001d); } }
private POSModel train(File trainFile, String lang, TrainingParameters params) throws IOException { ObjectStream<POSSample> samples = new ConllXPOSSampleStream(new MarkableFileInputStreamFactory(trainFile), StandardCharsets.UTF_8); return POSTaggerME.train(lang, samples, params, new POSTaggerFactory()); }
private static POSTaggerFactory create(Dictionary ngram, TagDictionary pos) { return new POSTaggerFactory(ngram, pos); } }
private static POSTaggerFactory create(Dictionary ngram, TagDictionary pos) { return new POSTaggerFactory(ngram, pos); } }
mlParams.getParameters("tagger"), new POSTaggerFactory());
parseSamples), mlParams.getParameters("tagger"), new POSTaggerFactory());
/** * Trains a POSModel from the annotated test data. * * @return {@link POSModel} */ public static POSModel trainPOSModel(ModelType type) throws IOException { TrainingParameters params = new TrainingParameters(); params.put(TrainingParameters.ALGORITHM_PARAM, type.toString()); params.put(TrainingParameters.ITERATIONS_PARAM, 100); params.put(TrainingParameters.CUTOFF_PARAM, 5); return POSTaggerME.train("eng", createSampleStream(), params, new POSTaggerFactory()); }
@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); }
@Test(expected = InsufficientTrainingDataException.class) public void insufficientTestData() throws IOException { InputStreamFactory in = new ResourceAsStreamFactory(POSTaggerMETest.class, "/opennlp/tools/postag/AnnotatedSentencesInsufficient.txt"); ObjectStream<POSSample> stream = new WordTagSampleStream( new PlainTextByLineStream(in, StandardCharsets.UTF_8)); TrainingParameters params = new TrainingParameters(); params.put(TrainingParameters.ALGORITHM_PARAM, ModelType.MAXENT.name()); params.put(TrainingParameters.ITERATIONS_PARAM, 100); params.put(TrainingParameters.CUTOFF_PARAM, 5); POSTaggerME.train("eng", stream, params, new POSTaggerFactory()); }
private void createPOSFactory(final TrainingParameters params) { final String featureSet = Flags.getFeatureSet(params); if (featureSet.equalsIgnoreCase("Opennlp")) { this.posTaggerFactory = new POSTaggerFactory(); } else { this.posTaggerFactory = new BaselineFactory(); } }
@Nullable private POSModel train(List<POSSample> aPosSamples, TrainingParameters aParameters) throws RecommendationException { if (aPosSamples.isEmpty()) { return null; } try (POSSampleStream stream = new POSSampleStream(aPosSamples)) { POSTaggerFactory taggerFactory = new POSTaggerFactory(); return POSTaggerME.train("unknown", stream, aParameters, taggerFactory); } catch (IOException e) { throw new RecommendationException("Error training model", e); } } }
@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); } }
@SuppressWarnings("unchecked") public Event[] updateContext(Sequence sequence, AbstractModel model) { Sequence<POSSample> pss = sequence; POSTagger tagger = new POSTaggerME(new POSModel("x-unspecified", model, null, new POSTaggerFactory())); String[] sentence = pss.getSource().getSentence(); Object[] ac = pss.getSource().getAddictionalContext(); String[] tags = tagger.tag(pss.getSource().getSentence()); Event[] events = new Event[sentence.length]; POSSampleEventStream.generateEvents(sentence, tags, ac, pcg) .toArray(events); return events; }
@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); } }
@SuppressWarnings("unchecked") public Event[] updateContext(Sequence sequence, AbstractModel model) { Sequence<POSSample> pss = sequence; POSTagger tagger = new POSTaggerME(new POSModel("x-unspecified", model, null, new POSTaggerFactory())); String[] sentence = pss.getSource().getSentence(); Object[] ac = pss.getSource().getAddictionalContext(); String[] tags = tagger.tag(pss.getSource().getSentence()); Event[] events = new Event[sentence.length]; POSSampleEventStream.generateEvents(sentence, tags, ac, pcg) .toArray(events); return events; }
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; }