private void addJCas(List<JCas> jCases, User user, SourceDocument sourceDocument) { try { // Check if there is an annotation document entry in the database. If there is none, // create one. AnnotationDocument annotationDocument = repository.createOrGetAnnotationDocument(sourceDocument, user); // Read the CAS JCas jcas = repository.readAnnotationCas(annotationDocument); // Update the annotation document CAS repository.upgradeCas(jcas.getCas(), annotationDocument); // After creating an new CAS or upgrading the CAS, we need to save it repository.writeAnnotationCas( jcas.getCas().getJCas(), annotationDocument.getDocument(), user); jCases.add(jcas); } catch (UIMAException e) { error(e.getMessage() + " : " + ExceptionUtils.getRootCauseMessage(e)); } catch (IOException e) { error(e.getMessage()); } }
public static void main(String[] arguments) { final CommandLineParser parser = new PosixParser(); final Options opts = new Options(); CommandLine cmd = null; Pipeline.addLogHelpAndInputOptions(opts); Pipeline.addTikaOptions(opts); Pipeline.addOutputOptions(opts); Pipeline.addJdbcResourceOptions( opts, DEFAULT_JDBC_DRIVER, DEFAULT_DB_PROVIDER, DEFAULT_DATABASE); // entity annotator options setup opts.addOption("Q", "query-file", true, "file with SQL SELECT queries"); OptionBuilder.withLongOpt("query"); OptionBuilder.withArgName("SELECT"); OptionBuilder.hasArgs(); OptionBuilder.withDescription("one or more SQL SELECT queries"); opts.addOption(OptionBuilder.create('q')); opts.addOption( "m", "entity-map", true, "name of the entity map file [" + DEFAULT_MAPPING_FILE + "]"); opts.addOption( "n", "namespace", true, "namespace of the entity annotations [" + DEFAULT_NAMESPACE + "]"); try { cmd = parser.parse(opts, arguments); } catch (final ParseException e) { System.err.println(e.getLocalizedMessage()); System.exit(1); // == exit == } final Logger l = Pipeline.loggingSetup(cmd, opts, "txtfnnl entities [options] <directory|files...>\n"); // output options XmiWriter.Builder writer = Pipeline.configureWriter(cmd, XmiWriter.configure(Pipeline.ensureOutputDirectory(cmd))); // DB resource ExternalResourceDescription jdbcResource = null; try { jdbcResource = Pipeline.getJdbcConnectionResource( cmd, l, DEFAULT_JDBC_DRIVER, DEFAULT_DB_PROVIDER, DEFAULT_DATABASE); } catch (final ClassNotFoundException e) { System.err.println("JDBC resoruce setup failed:"); System.err.println(e.toString()); System.exit(1); // == EXIT == } catch (ResourceInitializationException e) { System.err.println("JDBC resoruce setup failed:"); System.err.println(e.toString()); System.exit(1); // == EXIT == } /* BEGIN entity annotator */ final String queryFileName = cmd.getOptionValue('Q'); final String entityMapPath = cmd.getOptionValue('m', DEFAULT_MAPPING_FILE); final String namespace = cmd.getOptionValue('n', DEFAULT_NAMESPACE); String[] queries = cmd.getOptionValues('q'); File entityMap; // m if (queryFileName != null) { final File queryFile = new File(queryFileName); if (!queryFile.isFile() || !queryFile.canRead()) { System.err.print("cannot read query file "); System.err.println(queryFile); System.exit(1); // == EXIT == } String[] fileQueries = null; try { fileQueries = IOUtils.read(new FileInputStream(queryFile), Pipeline.inputEncoding(cmd)).split("\n"); } catch (final Exception e) { System.err.print("cannot read query file "); System.err.print(queryFile); System.err.print(":"); System.err.println(e.getLocalizedMessage()); System.exit(1); // == EXIT == } if (queries == null || queries.length == 0) { queries = fileQueries; } else { final String[] tmp = new String[queries.length + fileQueries.length]; System.arraycopy(queries, 0, tmp, 0, queries.length); System.arraycopy(fileQueries, 0, tmp, queries.length, fileQueries.length); queries = tmp; } } entityMap = new File(entityMapPath); if (!entityMap.isFile() || !entityMap.canRead()) { System.err.print("cannot read entity map file "); System.err.println(entityMapPath); System.exit(1); // == EXIT == } if (queries == null || queries.length == 0) { queries = DEFAULT_SQL_QUERIES; } /* END entity annotator */ try { final Pipeline pipeline = new Pipeline(2); // tika and known entity annotator KnownEntityAnnotator.Builder builder = KnownEntityAnnotator.configure(namespace, queries, entityMap, jdbcResource); pipeline.setReader(cmd); pipeline.configureTika(cmd); pipeline.set(1, Pipeline.multiviewEngine(builder.create())); pipeline.setConsumer(Pipeline.textEngine(writer.create())); pipeline.run(); pipeline.destroy(); } catch (final UIMAException e) { l.severe(e.toString()); System.err.println(e.getLocalizedMessage()); System.exit(1); // == EXIT == } catch (final IOException e) { l.severe(e.toString()); System.err.println(e.getLocalizedMessage()); System.exit(1); // == EXIT == } System.exit(0); }