Search in sources :

Example 11 with QueryResult

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

the class CommentQuery method execute.

@Override
protected QueryResult execute() throws Exception {
    QueryOptions queryOptions = getQueryOptions();
    WildcardTermExtractor termExtractor = new WildcardTermExtractor(commentPhrase, queryOptions.getUnseparableCharacterSequences(), queryOptions.getUserDefinedSeparatingCharacters(), queryOptions.getLocale());
    List<String> termList = termExtractor.getOrderedTerms();
    Indexer indexer = queryOptions.getIndexer();
    QueryResult result = indexer.searchCommentPhrase(queryOptions.getQueryId(), queryOptions.getRelevantSourceDocumentIDs(), termList, queryOptions.getLimit(), queryOptions.getUnseparableCharacterSequences(), queryOptions.getUserDefinedSeparatingCharacters(), queryOptions.getLocale());
    Project repository = queryOptions.getRepository();
    for (QueryResultRow row : result) {
        SourceDocument sd = repository.getSourceDocument(row.getSourceDocumentId());
        row.setPhrase(sd.getContent(row.getRange()));
    }
    return result;
}
Also used : Project(de.catma.project.Project) QueryResult(de.catma.queryengine.result.QueryResult) Indexer(de.catma.indexer.Indexer) QueryResultRow(de.catma.queryengine.result.QueryResultRow) WildcardTermExtractor(de.catma.indexer.WildcardTermExtractor) SourceDocument(de.catma.document.source.SourceDocument)

Example 12 with QueryResult

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

the class AnalyzeView method executeSearch.

private void executeSearch(String searchInput, Consumer<QueryResultPanel> addToLayoutFunction) {
    QueryOptions queryOptions = new QueryOptions(new QueryId(searchInput), currentCorpus.getDocumentIds(), currentCorpus.getCollectionIds(), indexInfoSet.getUnseparableCharacterSequences(), indexInfoSet.getUserDefinedSeparatingCharacters(), indexInfoSet.getLocale(), project);
    QueryJob job = new QueryJob(searchInput, queryOptions);
    showProgress(true);
    ((BackgroundServiceProvider) UI.getCurrent()).submit("Searching...", job, new ExecutionListener<QueryResult>() {

        public void done(QueryResult result) {
            try {
                QueryResultPanel queryResultPanel = new QueryResultPanel(project, result, new QueryId(searchInput.toString()), kwicProviderCache, closingPanel -> handleRemoveQueryResultPanel(closingPanel));
                addToLayoutFunction.accept(queryResultPanel);
                addQueryResultPanelSetting(queryResultPanel.getQueryResultPanelSetting());
            } finally {
                showProgress(false);
            }
        }

        public void error(Throwable t) {
            showProgress(false);
            if (t instanceof QueryException) {
                QueryJob.QueryException qe = (QueryJob.QueryException) t;
                String input = qe.getInput();
                int idx = ((RecognitionException) qe.getCause()).charPositionInLine;
                if ((idx >= 0) && (input.length() > idx)) {
                    char character = input.charAt(idx);
                    String message = MessageFormat.format("<html><p>There is something wrong with your query <b>{0}</b> approximately at positon {1} character <b>{2}</b>.</p> <p>If you are unsure about how to construct a query try the Query Builder!</p></html>", input, idx + 1, character);
                    HTMLNotification.show("Info", message, Type.TRAY_NOTIFICATION);
                } else {
                    String message = MessageFormat.format("<html><p>There is something wrong with your query <b>{0}</b>.</p> <p>If you are unsure about how to construct a query try the Query Builder!</p></html>", input);
                    HTMLNotification.show("Info", message, Type.TRAY_NOTIFICATION);
                }
            } else {
                ((ErrorHandler) UI.getCurrent()).showAndLogError("Error during search!", t);
            }
        }
    });
}
Also used : ThemeResource(com.vaadin.server.ThemeResource) Panel(com.vaadin.ui.Panel) BackgroundServiceProvider(de.catma.backgroundservice.BackgroundServiceProvider) HTMLNotification(de.catma.ui.component.HTMLNotification) LoadingCache(com.google.common.cache.LoadingCache) WordCloudDisplaySettingHandler(de.catma.ui.module.analyze.visualization.vega.WordCloudDisplaySettingHandler) Alignment(com.vaadin.ui.Alignment) UI(com.vaadin.ui.UI) IndexedProject(de.catma.indexer.IndexedProject) KwicProvider(de.catma.indexer.KwicProvider) RecognitionException(org.antlr.runtime.RecognitionException) ErrorHandler(de.catma.ui.module.main.ErrorHandler) Locale(java.util.Locale) SliderPanel(org.vaadin.sliderpanel.SliderPanel) VaadinIcons(com.vaadin.icons.VaadinIcons) QueryId(de.catma.queryengine.QueryId) ProgressBar(com.vaadin.ui.ProgressBar) IconButton(de.catma.ui.component.IconButton) ExecutionListener(de.catma.backgroundservice.ExecutionListener) QueryOptions(de.catma.queryengine.QueryOptions) IndexInfoSet(de.catma.document.source.IndexInfoSet) ClosableTab(de.catma.ui.component.tabbedview.ClosableTab) QueryBuilder(de.catma.ui.module.analyze.querybuilder.QueryBuilder) MarginInfo(com.vaadin.shared.ui.MarginInfo) List(java.util.List) Type(com.vaadin.ui.Notification.Type) Corpus(de.catma.document.corpus.Corpus) RefreshQueryResultPanel(de.catma.ui.module.analyze.queryresultpanel.RefreshQueryResultPanel) Optional(java.util.Optional) SliderPanelBuilder(org.vaadin.sliderpanel.SliderPanelBuilder) QueryJob(de.catma.queryengine.QueryJob) SliderMode(org.vaadin.sliderpanel.client.SliderMode) KwicPanel(de.catma.ui.module.analyze.visualization.kwic.KwicPanel) FormatStyle(java.time.format.FormatStyle) QueryResultPanelSetting(de.catma.ui.module.analyze.queryresultpanel.QueryResultPanelSetting) DoubleTreePanel(de.catma.ui.module.analyze.visualization.doubletree.DoubleTreePanel) QueryResultPanel(de.catma.ui.module.analyze.queryresultpanel.QueryResultPanel) VerticalLayout(com.vaadin.ui.VerticalLayout) LocalDateTime(java.time.LocalDateTime) ComboBox(com.vaadin.ui.ComboBox) WizardContext(de.catma.ui.dialog.wizard.WizardContext) DistributionDisplaySettingHandler(de.catma.ui.module.analyze.visualization.vega.DistributionDisplaySettingHandler) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) EventBus(com.google.common.eventbus.EventBus) MaterialTheme(com.github.appreciated.material.MaterialTheme) Notification(com.vaadin.ui.Notification) VegaPanel(de.catma.ui.module.analyze.visualization.vega.VegaPanel) Label(com.vaadin.ui.Label) ClassResource(com.vaadin.server.ClassResource) SaveCancelListener(de.catma.ui.dialog.SaveCancelListener) QueryException(de.catma.queryengine.QueryJob.QueryException) AnalyzeResourcePanel(de.catma.ui.module.analyze.resourcepanel.AnalyzeResourcePanel) ListDataProvider(com.vaadin.data.provider.ListDataProvider) QueryResult(de.catma.queryengine.result.QueryResult) Iterator(java.util.Iterator) Consumer(java.util.function.Consumer) Button(com.vaadin.ui.Button) HorizontalLayout(com.vaadin.ui.HorizontalLayout) TabCaptionChangeListener(de.catma.ui.component.tabbedview.TabCaptionChangeListener) DateTimeFormatter(java.time.format.DateTimeFormatter) QueryTree(de.catma.queryengine.querybuilder.QueryTree) Collections(java.util.Collections) Component(com.vaadin.ui.Component) QueryResult(de.catma.queryengine.result.QueryResult) QueryException(de.catma.queryengine.QueryJob.QueryException) QueryJob(de.catma.queryengine.QueryJob) RefreshQueryResultPanel(de.catma.ui.module.analyze.queryresultpanel.RefreshQueryResultPanel) QueryResultPanel(de.catma.ui.module.analyze.queryresultpanel.QueryResultPanel) QueryId(de.catma.queryengine.QueryId) BackgroundServiceProvider(de.catma.backgroundservice.BackgroundServiceProvider) QueryOptions(de.catma.queryengine.QueryOptions)

Example 13 with QueryResult

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

the class CSVExportFlatStreamSource method getStream.

@Override
public InputStream getStream() {
    final QueryResult queryResult = queryResultSupplier.get();
    final PipedInputStream in = new PipedInputStream();
    final UI ui = UI.getCurrent();
    final Lock lock = new ReentrantLock();
    final Condition sending = lock.newCondition();
    lock.lock();
    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");
            LoadingCache<String, String> colorCache = CacheBuilder.newBuilder().build(new CacheLoader<String, String>() {

                @Override
                public String load(String tagDefinitionId) throws Exception {
                    return "#" + ColorConverter.toHex(project.getTagManager().getTagLibrary().getTagDefinition(tagDefinitionId).getColor());
                }
            });
            try (CSVPrinter csvPrinter = new CSVPrinter(writer, CSVFormat.EXCEL.withDelimiter(';'))) {
                for (QueryResultRow row : queryResult) {
                    KwicProvider kwicProvider = kwicProviderCache.get(row.getSourceDocumentId());
                    if (row instanceof TagQueryResultRow) {
                        TagQueryResultRow tRow = (TagQueryResultRow) row;
                        List<Range> mergedRanges = Range.mergeRanges(new TreeSet<>((tRow).getRanges()));
                        for (Range range : mergedRanges) {
                            KeywordInSpanContext kwic = kwicProvider.getKwic(range, 5);
                            csvPrinter.printRecord(row.getQueryId().toSerializedString(), row.getSourceDocumentId(), kwicProvider.getSourceDocumentName(), kwicProvider.getDocumentLength(), kwic.getKeyword(), kwic.toString(), range.getStartPoint(), range.getEndPoint(), tRow.getMarkupCollectionId(), kwicProvider.getSourceDocument().getUserMarkupCollectionReference(tRow.getMarkupCollectionId()).toString(), tRow.getTagDefinitionPath(), tRow.getTagDefinitionVersion(), colorCache.get(tRow.getTagDefinitionId()), tRow.getTagInstanceId(), tRow.getPropertyDefinitionId(), tRow.getPropertyName(), tRow.getPropertyValue());
                        }
                    } else {
                        KeywordInSpanContext kwic = kwicProvider.getKwic(row.getRange(), 5);
                        csvPrinter.printRecord(row.getQueryId().toSerializedString(), row.getSourceDocumentId(), kwicProvider.getSourceDocumentName(), kwicProvider.getDocumentLength(), kwic.getKeyword(), kwic.toString(), row.getRange().getStartPoint(), row.getRange().getEndPoint());
                    }
                    csvPrinter.flush();
                    lock.lock();
                    try {
                        sending.signal();
                    } finally {
                        lock.unlock();
                    }
                }
            }
            // 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
    try {
        try {
            sending.await(10, TimeUnit.SECONDS);
        } catch (InterruptedException e1) {
            Logger.getLogger(getClass().getName()).log(Level.WARNING, "Error while waiting on CSV export!", e1);
        }
    } finally {
        lock.unlock();
    }
    return in;
}
Also used : TagQueryResultRow(de.catma.queryengine.result.TagQueryResultRow) QueryResultRow(de.catma.queryengine.result.QueryResultRow) 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) LoadingCache(com.google.common.cache.LoadingCache) List(java.util.List) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Condition(java.util.concurrent.locks.Condition) PipedInputStream(java.io.PipedInputStream) KeywordInSpanContext(de.catma.indexer.KeywordInSpanContext) Range(de.catma.document.Range) ReentrantLock(java.util.concurrent.locks.ReentrantLock) Lock(java.util.concurrent.locks.Lock) OutputStreamWriter(java.io.OutputStreamWriter) CacheLoader(com.google.common.cache.CacheLoader)

Example 14 with QueryResult

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

the class TPGraphProjectIndexer method searchCollocation.

@Override
public QueryResult searchCollocation(QueryId queryId, QueryResult baseResult, QueryResult collocationConditionResult, int spanContextSize, SpanDirection direction) throws IOException {
    int baseResultSize = baseResult.size();
    int collocConditionResultSize = collocationConditionResult.size();
    boolean swapCollocationDirection = baseResultSize > collocConditionResultSize;
    // swap to reduce the amount of span context computation
    if (swapCollocationDirection) {
        QueryResult bufferResult = baseResult;
        baseResult = collocationConditionResult;
        collocationConditionResult = bufferResult;
    }
    Multimap<String, QueryResultRow> collocConditionResultBySourceDocumentId = ArrayListMultimap.create();
    collocationConditionResult.forEach(row -> collocConditionResultBySourceDocumentId.put(row.getSourceDocumentId(), row));
    QueryResultRowArray matchingBaseRows = new QueryResultRowArray();
    QueryResultRowArray matchingCollocConditionRows = new QueryResultRowArray();
    for (QueryResultRow row : baseResult) {
        if (collocConditionResultBySourceDocumentId.containsKey(row.getSourceDocumentId())) {
            SpanContext spanContext = getSpanContextFor(row.getSourceDocumentId(), row.getRange(), spanContextSize, direction);
            boolean baseMatch = matchingBaseRows.contains(row);
            for (QueryResultRow collocConditionRow : collocConditionResultBySourceDocumentId.get(row.getSourceDocumentId())) {
                boolean collocMatch = matchingCollocConditionRows.contains(collocConditionRow);
                if (!baseMatch || !collocMatch) {
                    if (spanContext.hasOverlappingRange(collocConditionRow.getRanges(), direction)) {
                        if (!baseMatch) {
                            matchingBaseRows.add(row);
                            baseMatch = true;
                        }
                        if (!collocMatch) {
                            matchingCollocConditionRows.add(collocConditionRow);
                            collocMatch = true;
                        }
                    }
                }
            }
        }
    }
    // swap back
    if (swapCollocationDirection) {
        QueryResultRowArray bufferResult = matchingBaseRows;
        matchingBaseRows = matchingCollocConditionRows;
        matchingCollocConditionRows = bufferResult;
    }
    return matchingBaseRows;
}
Also used : QueryResult(de.catma.queryengine.result.QueryResult) SpanContext(de.catma.indexer.SpanContext) TagQueryResultRow(de.catma.queryengine.result.TagQueryResultRow) CommentQueryResultRow(de.catma.queryengine.result.CommentQueryResultRow) QueryResultRow(de.catma.queryengine.result.QueryResultRow) 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