Search in sources :

Example 1 with QueryResult

use of de.catma.queryengine.result.QueryResult in project catma by forTEXT.

the class CSVExportGroupedStreamSource method getStream.

@Override
public InputStream getStream() {
    final QueryResult queryResult = queryResultSupplier.get();
    final Set<String> documentIds = new TreeSet<String>();
    final Multimap<String, String> collectionIdByDocumentId = ArrayListMultimap.create();
    final Table<String, String, Integer> groupings = HashBasedTable.create();
    for (QueryResultRow row : queryResult) {
        String group = row.getPhrase();
        if (groupByTagSupplier.get()) {
            if (row instanceof TagQueryResultRow) {
                group = ((TagQueryResultRow) row).getTagDefinitionPath();
            } else {
                group = TagQueryResultRowItem.getNoTagAvailableKey();
            }
        }
        groupings.put(group, "Total", getValue(groupings, group, "Total") + 1);
        groupings.put(group, row.getSourceDocumentId(), getValue(groupings, group, row.getSourceDocumentId()) + 1);
        documentIds.add(row.getSourceDocumentId());
        if (row instanceof TagQueryResultRow) {
            collectionIdByDocumentId.put(row.getSourceDocumentId(), ((TagQueryResultRow) row).getMarkupCollectionId());
            groupings.put(group, ((TagQueryResultRow) row).getMarkupCollectionId(), getValue(groupings, group, ((TagQueryResultRow) row).getMarkupCollectionId()) + 1);
        }
    }
    final PipedInputStream in = new PipedInputStream();
    final UI ui = UI.getCurrent();
    backgroundServiceProvider.submit("csv-export", new DefaultProgressCallable<Void>() {

        @Override
        public Void call() throws Exception {
            PipedOutputStream out = new PipedOutputStream(in);
            OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
            ArrayList<String> header = new ArrayList<>();
            header.add("Group");
            header.add("Total");
            for (String documentId : documentIds) {
                KwicProvider kwicProvider = kwicProviderCache.get(documentId);
                header.add(kwicProvider.getSourceDocumentName() + " (" + documentId + ")");
                for (String collectionId : new TreeSet<String>(collectionIdByDocumentId.get(documentId))) {
                    header.add(kwicProvider.getSourceDocument().getUserMarkupCollectionReference(collectionId).toString() + " (" + collectionId + ")");
                }
            }
            try (CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.EXCEL.withDelimiter(';').withHeader(header.toArray(new String[] {})))) {
                for (String group : new TreeSet<String>(groupings.rowKeySet())) {
                    csvPrinter.print(group);
                    csvPrinter.print(groupings.get(group, "Total"));
                    for (String documentId : documentIds) {
                        csvPrinter.print(groupings.get(group, documentId));
                        for (String collectionId : new TreeSet<String>(collectionIdByDocumentId.get(documentId))) {
                            csvPrinter.print(groupings.get(group, collectionId));
                        }
                    }
                    csvPrinter.println();
                }
                csvPrinter.flush();
            }
            // intended
            return null;
        }
    }, new ExecutionListener<Void>() {

        @Override
        public void done(Void result) {
        // noop
        }

        @Override
        public void error(Throwable t) {
            ((ErrorHandler) ui).showAndLogError("Error export data to CSV!", t);
        }
    });
    // waiting on the background thread to send data to the pipe
    int tries = 100;
    try {
        while (!(in.available() > 0) && tries > 0) {
            Thread.sleep(10);
            tries--;
        }
    } catch (IOException | InterruptedException e) {
        Logger.getLogger(getClass().getName()).log(Level.WARNING, "Error while waiting on CSV export!", e);
    }
    return in;
}
Also used : TagQueryResultRow(de.catma.queryengine.result.TagQueryResultRow) QueryResultRow(de.catma.queryengine.result.QueryResultRow) ArrayList(java.util.ArrayList) PipedOutputStream(java.io.PipedOutputStream) CSVPrinter(org.apache.commons.csv.CSVPrinter) QueryResult(de.catma.queryengine.result.QueryResult) UI(com.vaadin.ui.UI) TagQueryResultRow(de.catma.queryengine.result.TagQueryResultRow) TreeSet(java.util.TreeSet) KwicProvider(de.catma.indexer.KwicProvider) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException) IOException(java.io.IOException) OutputStreamWriter(java.io.OutputStreamWriter)

Example 2 with QueryResult

use of de.catma.queryengine.result.QueryResult in project catma by forTEXT.

the class Phrase method execute.

@Override
protected QueryResult execute() throws Exception {
    QueryOptions options = getQueryOptions();
    TermExtractor termExtractor = new TermExtractor(phrase, options.getUnseparableCharacterSequences(), options.getUserDefinedSeparatingCharacters(), options.getLocale());
    List<String> termList = termExtractor.getTermsInOrder();
    QueryResult queryResult = options.getIndexer().searchPhrase(options.getQueryId(), options.getRelevantSourceDocumentIDs(), phrase, termList, options.getLimit());
    return queryResult;
}
Also used : QueryResult(de.catma.queryengine.result.QueryResult) TermExtractor(de.catma.indexer.TermExtractor)

Example 3 with QueryResult

use of de.catma.queryengine.result.QueryResult in project catma by forTEXT.

the class CollocQuery method execute.

@Override
protected QueryResult execute() throws Exception {
    QueryResult baseResult = query1.execute();
    QueryResult collocCondition = query2.execute();
    Indexer indexer = getQueryOptions().getIndexer();
    return indexer.searchCollocation(getQueryOptions().getQueryId(), baseResult, collocCondition, spanContextSize, direction);
}
Also used : QueryResult(de.catma.queryengine.result.QueryResult) Indexer(de.catma.indexer.Indexer)

Example 4 with QueryResult

use of de.catma.queryengine.result.QueryResult in project catma by forTEXT.

the class SimilQuery method searchTermsBySimilarity.

/**
 * Searches a list of terms that a similar to the given phrase by a certain degree
 *
 * @param phrase the phrase to compare with
 * @param similPercent the degree of similarity
 * @return the list of similar terms
 * @throws Exception
 * @see org.catma.queryengine.Simil
 */
private QueryResult searchTermsBySimilarity() throws Exception {
    QueryResultRowArray result = new QueryResultRowArray();
    Simil simil = new Simil(phrase);
    FreqQuery freqQuery = new FreqQuery(CompareOperator.GREATERTHAN.toString(), "0");
    freqQuery.setQueryOptions(getQueryOptions());
    QueryResult allTokens = freqQuery.getResult();
    Set<GroupedQueryResult> allTypes = allTokens.asGroupedSet();
    for (GroupedQueryResult groupedQueryResult : allTypes) {
        String type = groupedQueryResult.getGroup().toString();
        if (simil.getSimilarityInPercentFor(type) >= similPercent) {
            for (QueryResultRow row : groupedQueryResult) {
                result.add(row);
            }
        }
    }
    return result;
}
Also used : GroupedQueryResult(de.catma.queryengine.result.GroupedQueryResult) GroupedQueryResult(de.catma.queryengine.result.GroupedQueryResult) QueryResult(de.catma.queryengine.result.QueryResult) QueryResultRow(de.catma.queryengine.result.QueryResultRow) QueryResultRowArray(de.catma.queryengine.result.QueryResultRowArray)

Example 5 with QueryResult

use of de.catma.queryengine.result.QueryResult in project catma by forTEXT.

the class TagDiffQuery method execute.

@Override
protected QueryResult execute() throws Exception {
    QueryOptions queryOptions = getQueryOptions();
    Project repository = queryOptions.getRepository();
    Indexer indexer = queryOptions.getIndexer();
    List<String> relevantUserMarkupCollIDs = queryOptions.getRelevantUserMarkupCollIDs();
    if (relevantUserMarkupCollIDs.isEmpty() && !queryOptions.getRelevantSourceDocumentIDs().isEmpty()) {
        relevantUserMarkupCollIDs = new ArrayList<String>();
        for (String sourceDocumentId : queryOptions.getRelevantSourceDocumentIDs()) {
            for (AnnotationCollectionReference umcRef : repository.getSourceDocument(sourceDocumentId).getUserMarkupCollectionRefs()) {
                relevantUserMarkupCollIDs.add(umcRef.getId());
            }
        }
        if (relevantUserMarkupCollIDs.isEmpty()) {
            return new QueryResultRowArray();
        }
    }
    QueryResult result = indexer.searchTagDiff(queryOptions.getQueryId(), relevantUserMarkupCollIDs, propertyName, tagPhrase);
    for (QueryResultRow row : result) {
        SourceDocument sd = repository.getSourceDocument(row.getSourceDocumentId());
        TagQueryResultRow tRow = (TagQueryResultRow) row;
        if (tRow.getRanges().size() > 1) {
            StringBuilder builder = new StringBuilder();
            String conc = "";
            for (Range range : tRow.getRanges()) {
                builder.append(conc);
                builder.append(sd.getContent(range));
                conc = "[...]";
            }
            row.setPhrase(builder.toString());
        } else {
            row.setPhrase(sd.getContent(row.getRange()));
        }
    }
    return result;
}
Also used : QueryResultRow(de.catma.queryengine.result.QueryResultRow) TagQueryResultRow(de.catma.queryengine.result.TagQueryResultRow) SourceDocument(de.catma.document.source.SourceDocument) AnnotationCollectionReference(de.catma.document.annotation.AnnotationCollectionReference) Range(de.catma.document.Range) Project(de.catma.project.Project) QueryResult(de.catma.queryengine.result.QueryResult) Indexer(de.catma.indexer.Indexer) TagQueryResultRow(de.catma.queryengine.result.TagQueryResultRow) QueryResultRowArray(de.catma.queryengine.result.QueryResultRowArray)

Aggregations

QueryResult (de.catma.queryengine.result.QueryResult)14 QueryResultRow (de.catma.queryengine.result.QueryResultRow)10 Indexer (de.catma.indexer.Indexer)6 QueryResultRowArray (de.catma.queryengine.result.QueryResultRowArray)6 TagQueryResultRow (de.catma.queryengine.result.TagQueryResultRow)6 SourceDocument (de.catma.document.source.SourceDocument)5 Project (de.catma.project.Project)5 Range (de.catma.document.Range)4 UI (com.vaadin.ui.UI)3 KwicProvider (de.catma.indexer.KwicProvider)3 LoadingCache (com.google.common.cache.LoadingCache)2 AnnotationCollectionReference (de.catma.document.annotation.AnnotationCollectionReference)2 List (java.util.List)2 RecognitionException (org.antlr.runtime.RecognitionException)2 MaterialTheme (com.github.appreciated.material.MaterialTheme)1 CacheLoader (com.google.common.cache.CacheLoader)1 EventBus (com.google.common.eventbus.EventBus)1 ListDataProvider (com.vaadin.data.provider.ListDataProvider)1 VaadinIcons (com.vaadin.icons.VaadinIcons)1 ClassResource (com.vaadin.server.ClassResource)1