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;
}
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;
}
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);
}
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;
}
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;
}
Aggregations