use of org.corpus_tools.annis.api.CorporaApi in project ANNIS by korpling.
the class Helper method getCorpusConfig.
/**
* Loads the corpus config of a specific corpus.
*
* @param corpus The name of the corpus, for which the config is fetched.
* @return A {@link CorpusConfig} object, which wraps a {@link Properties} object. This Properties
* object stores the corpus configuration as simple key-value pairs.
*/
public static CorpusConfiguration getCorpusConfig(final String corpus, final UI ui) {
if (corpus == null || corpus.isEmpty()) {
Notification.show("no corpus is selected", "please select at least one corpus and execute query again", Notification.Type.WARNING_MESSAGE);
return null;
}
CorpusConfiguration corpusConfig = new CorpusConfiguration();
final CorporaApi api = new CorporaApi(getClient(ui));
try {
corpusConfig = api.corpusConfiguration(corpus);
} catch (final ApiException ex) {
ui.access(() -> ExceptionDialog.show(ex, ERROR_MESSAGE_CORPUS_PROPS_HEADER, ui));
}
return corpusConfig;
}
use of org.corpus_tools.annis.api.CorporaApi in project ANNIS by korpling.
the class Helper method getMetaAnnotationNames.
public static Set<AnnoKey> getMetaAnnotationNames(final String corpus, final UI ui) throws ApiException {
final CorporaApi api = new CorporaApi(getClient(ui));
final SearchApi search = new SearchApi(getClient(ui));
final List<org.corpus_tools.annis.api.model.Annotation> nodeAnnos = api.nodeAnnotations(corpus, false, true).stream().filter(a -> !Objects.equals(a.getKey().getNs(), "annis") && !Objects.equals(a.getKey().getName(), "tok")).collect(Collectors.toList());
final Set<AnnoKey> metaAnnos = new HashSet<>();
// Check for each annotation if its actually a meta-annotation
for (final org.corpus_tools.annis.api.model.Annotation a : nodeAnnos) {
String annotationName = getQName(a.getKey());
if (annotationIsMetadata(corpus, annotationName, search)) {
metaAnnos.add(a.getKey());
}
}
return metaAnnos;
}
use of org.corpus_tools.annis.api.CorporaApi in project ANNIS by korpling.
the class SearchView method evaluateFragment.
private void evaluateFragment(String fragment) {
// do nothing if not changed
if (fragment == null || fragment.isEmpty() || fragment.equals(lastEvaluatedFragment)) {
return;
}
Map<String, String> args = Helper.parseFragment(fragment);
if (args.containsKey("c")) {
String[] originalCorpusNames = args.get("c").split("\\s*,\\s*");
Set<String> corpora = new TreeSet<String>(Arrays.asList(originalCorpusNames));
// Remove all corpora we don't have the access right to
try {
CorporaApi api = new CorporaApi(Helper.getClient(ui));
Set<String> availableCorpora = new HashSet<>(api.listCorpora());
corpora.removeIf(c -> !availableCorpora.contains(c));
} catch (ApiException e) {
ExceptionDialog.show(e, "Could not get corpus list", ui);
}
if (corpora.isEmpty()) {
if (!Helper.getUser(ui.getSecurityContext()).isPresent() && toolbar != null) {
// not logged in, show login window
toolbar.showLoginWindow();
} else {
// already logged in or no login system available, just display a message
new Notification("Linked corpus does not exist", "<div><p>The corpus you wanted to access unfortunally does not (yet) exist" + " in ANNIS.</p>" + "<h2>possible reasons are:</h2>" + "<ul>" + "<li>that it has not been imported yet,</li>" + "<li>you don't have the access rights to see this corpus,</li>" + "<li>or the ANNIS service is not running.</li>" + "</ul>" + "<p>Please ask the responsible person of the site that contained " + "the link to import the corpus.</p></div>", Notification.Type.WARNING_MESSAGE, true).show(Page.getCurrent());
}
} else // end if corpus list returned from service is empty
{
if (args.containsKey("c") && args.size() == 1) {
// special case: we were called from outside and should only select,
// but not query, the selected corpora
Query q = new Query();
q.setCorpora(corpora);
q.setQuery("");
ui.getQueryController().setQuery(q);
ui.getQueryController().corpusSelectionChangedInBackground();
} else if (args.get("cl") != null && args.get("cr") != null) {
// make sure the properties are not overwritten by the background process
getControlPanel().getSearchOptions().setUpdateStateFromConfig(false);
DisplayedResultQuery query = QueryGenerator.displayed().left(Integer.parseInt(args.get("cl"))).right(Integer.parseInt(args.get("cr"))).offset(Integer.parseInt(args.get("s"))).limit(Integer.parseInt(args.get("l"))).segmentation(args.get("seg")).baseText(args.get("bt")).query(args.get("q")).corpora(corpora).build();
if (query.getBaseText() == null && query.getSegmentation() != null) {
// if no explicit visible segmentation was given use the same as the context
query.setBaseText(query.getSegmentation());
}
if (query.getBaseText() != null && query.getBaseText().isEmpty()) {
// empty string means "null"
query.setBaseText(null);
}
String matchSelectionRaw = args.get("m");
if (matchSelectionRaw != null) {
for (String selectedMatchNr : Splitter.on(',').omitEmptyStrings().trimResults().split(matchSelectionRaw)) {
try {
long nr = Long.parseLong(selectedMatchNr);
query.getSelectedMatches().add(nr);
} catch (NumberFormatException ex) {
log.warn("Invalid long provided as selected match", ex);
}
}
}
if (args.get("o") != null) {
try {
query.setOrder(DisplayedResultQuery.parseOrderFromCitationFragment(args.get("0")));
} catch (IllegalArgumentException ex) {
log.warn("Could not parse query fragment argument for order", ex);
}
}
if (args.get("ql") != null) {
try {
query.setQueryLanguage(QueryLanguage.valueOf(args.get("ql").toUpperCase()));
} catch (IllegalArgumentException ex) {
log.warn("Could not parse query fragment argument for the query language", ex);
}
}
// full query with given context
ui.getQueryController().setQuery(query);
ui.getQueryController().executeSearch(true, false);
} else if (args.get("q") != null) {
QueryLanguage ql = QueryLanguage.AQL;
if (args.get("ql") != null) {
try {
ql = QueryLanguage.valueOf(args.get("ql").toUpperCase());
} catch (IllegalArgumentException ex) {
log.warn("Could not parse query fragment argument for the query language", ex);
}
}
// use default context
ui.getQueryController().setQuery(new Query(args.get("q"), ql, corpora));
ui.getQueryController().executeSearch(true, true);
}
}
// end if corpus list from server was non-empty
}
// end if there is a corpus definition
}
use of org.corpus_tools.annis.api.CorporaApi in project ANNIS by korpling.
the class CorpusBrowserPanel method fetchAnnotationsInBackground.
private void fetchAnnotationsInBackground(UI ui) {
CorporaApi api = new CorporaApi(Helper.getClient(ui));
try {
final List<Annotation> nodeAnnos = api.nodeAnnotations(corpus, true, true).stream().filter(a -> !Objects.equals(a.getKey().getNs(), "annis") && !Objects.equals(a.getKey().getName(), "tok")).collect(Collectors.toList());
final List<Annotation> metaAnnos = new LinkedList<>(nodeAnnos);
final Set<AnnoKey> metaAnnoKeys = Helper.getMetaAnnotationNames(corpus, ui);
nodeAnnos.removeIf(anno -> metaAnnoKeys.contains(anno.getKey()));
metaAnnos.removeIf(anno -> !metaAnnoKeys.contains(anno.getKey()));
final List<Component> components = api.components(corpus, "Dominance", null);
final List<Annotation> allEdgeAnnos = new LinkedList<>();
final Map<Component, List<Annotation>> edgeAnnosByComponent = new LinkedHashMap<>();
components.addAll(api.components(corpus, "Pointing", null));
for (Component c : components) {
try {
List<Annotation> annos = api.edgeAnnotations(corpus, c.getType().getValue(), c.getLayer(), c.getName(), true, true);
edgeAnnosByComponent.put(c, annos);
allEdgeAnnos.addAll(annos);
} catch (ApiException ex) {
// Ignore any not found errors
}
}
getUI().access(() -> {
TreeSet<CorpusBrowserEntry> nodeAnnoItems = new TreeSet<>();
TreeSet<CorpusBrowserEntry> edgeAnnoItems = new TreeSet<>();
TreeSet<CorpusBrowserEntry> edgeTypeItems = new TreeSet<>();
TreeSet<CorpusBrowserEntry> metaAnnoItems = new TreeSet<>();
progress.setVisible(false);
accordion.setVisible(true);
boolean stripNodeAnno = canExcludeNamespace(nodeAnnos);
boolean stripEdgeName = canExcludeNamespace(allEdgeAnnos);
boolean stripEdgeAnno = true;
HashSet<String> nodeAnnoNames = new HashSet<>();
HashSet<String> edgeAnnoNames = new HashSet<>();
HashSet<String> edgeNames = new HashSet<>();
boolean hasDominance = false;
boolean hasEmptyDominance = false;
// do some preparations first
for (Annotation a : nodeAnnos) {
// check for ambiguous names
if (!nodeAnnoNames.add(a.getKey().getName())) {
stripNodeAnno = false;
}
}
for (Component c : components) {
// check if collected edge names are unique
if (!edgeNames.add(Helper.getQName(c))) {
stripEdgeName = false;
}
// check if we need to add the general dominance example edge
if (c.getType() == AnnotationComponentType.DOMINANCE) {
hasDominance = true;
if (c.getName() == null || c.getName().isEmpty()) {
hasEmptyDominance = true;
}
}
}
for (List<Annotation> annos : edgeAnnosByComponent.values()) {
for (Annotation a : annos) {
// check for ambiguous names
if (!edgeAnnoNames.add(a.getKey().getName())) {
stripEdgeAnno = false;
}
}
}
// fill the actual containers
for (Annotation a : nodeAnnos) {
String name = stripNodeAnno ? a.getKey().getName() : Helper.getQName(a.getKey());
CorpusBrowserEntry cbe = new CorpusBrowserEntry();
cbe.setName(name);
cbe.setExample(name + "=\"" + a.getVal() + "\"");
cbe.setCorpus(corpus);
nodeAnnoItems.add(cbe);
}
// edge type entry
if (hasDominance && !hasEmptyDominance) {
CorpusBrowserEntry cbe = new CorpusBrowserEntry();
cbe.setName("(dominance)");
cbe.setCorpus(corpus);
cbe.setExample("node & node & #1 > #2");
edgeTypeItems.add(cbe);
}
for (Component c : components) {
CorpusBrowserEntry cbeEdgeType = new CorpusBrowserEntry();
String name = stripEdgeName ? c.getName() : Helper.getQName(c);
if ((name == null || name.isEmpty()) && c.getType() == AnnotationComponentType.DOMINANCE) {
cbeEdgeType.setName("(dominance)");
} else {
cbeEdgeType.setName(name);
}
cbeEdgeType.setCorpus(corpus);
if (c.getType() == AnnotationComponentType.POINTING) {
cbeEdgeType.setExample("node & node & #1 ->" + c.getName() + " #2");
} else if (c.getType() == AnnotationComponentType.DOMINANCE) {
cbeEdgeType.setExample("node & node & #1 >" + c.getName() + " #2");
}
edgeTypeItems.add(cbeEdgeType);
}
// edge annotation entries
for (Map.Entry<Component, List<Annotation>> entry : edgeAnnosByComponent.entrySet()) {
Component c = entry.getKey();
for (Annotation a : entry.getValue()) {
CorpusBrowserEntry cbeEdgeAnno = new CorpusBrowserEntry();
String edgeAnno = stripEdgeAnno ? a.getKey().getName() : Helper.getQName(a.getKey());
cbeEdgeAnno.setName(edgeAnno);
cbeEdgeAnno.setCorpus(corpus);
if (c.getType() == AnnotationComponentType.POINTING) {
cbeEdgeAnno.setExample("node & node & #1 ->" + c.getName() + "[" + a.getKey().getName() + "=\"" + a.getVal() + "\"] #2");
} else if (c.getType() == AnnotationComponentType.DOMINANCE) {
cbeEdgeAnno.setExample("node & node & #1 >[" + a.getKey().getName() + "=\"" + a.getVal() + "\"] #2");
}
edgeAnnoItems.add(cbeEdgeAnno);
}
}
boolean stripMetaName = canExcludeNamespace(metaAnnos);
for (Annotation a : nodeAnnos) {
String name = stripMetaName ? a.getKey().getName() : Helper.getQName(a.getKey());
CorpusBrowserEntry cbe = new CorpusBrowserEntry();
cbe.setName(name);
cbe.setExample(name + "=\"" + a.getVal() + "\"");
cbe.setCorpus(corpus);
nodeAnnoItems.add(cbe);
}
for (Annotation a : metaAnnos) {
String name = stripNodeAnno ? a.getKey().getName() : Helper.getQName(a.getKey());
CorpusBrowserEntry cbe = new CorpusBrowserEntry();
cbe.setName(name);
cbe.setExample(name + "=\"" + a.getVal() + "\"");
cbe.setCorpus(corpus);
metaAnnoItems.add(cbe);
}
lblNoNodeAnno.setVisible(nodeAnnoItems.isEmpty());
tblNodeAnno.setVisible(!nodeAnnoItems.isEmpty());
tblNodeAnno.setItems(new ArrayList<>(nodeAnnoItems));
lblNoEdgeAnno.setVisible(edgeAnnoItems.isEmpty());
tblEdgeAnno.setVisible(!edgeAnnoItems.isEmpty());
tblEdgeAnno.setItems(edgeAnnoItems);
lblNoEdgeTypes.setVisible(edgeTypeItems.isEmpty());
tblEdgeTypes.setVisible(!edgeTypeItems.isEmpty());
tblEdgeTypes.setItems(edgeTypeItems);
lblNoMetaAnno.setVisible(metaAnnoItems.isEmpty());
tblMetaAnno.setVisible(!metaAnnoItems.isEmpty());
tblMetaAnno.setItems(metaAnnoItems);
});
} catch (ApiException e) {
getUI().access(() -> {
ExceptionDialog.show(e, "Error fetching corpus annotations", getUI());
});
}
}
use of org.corpus_tools.annis.api.CorporaApi in project ANNIS by korpling.
the class EmbeddedVisUI method generateVisFromParameters.
private void generateVisFromParameters(final String visName, Map<String, String[]> args) {
// find the matching visualizer
final Optional<VisualizerPlugin> visPlugin = visualizers.stream().filter(vis -> Objects.equal(vis.getShortName(), visName)).findAny();
if (!visPlugin.isPresent()) {
displayMessage("Unknown visualizer \"" + visName + "\"", "This ANNIS instance does not know the given visualizer.");
return;
}
ApiClient client = getClient();
displayLoadingIndicator();
// Create a subgraph query
CorporaApi api = new CorporaApi(client);
Match match = Match.parseFromString(args.get(KEY_MATCH)[0]);
List<String> corpusPath = Helper.getCorpusPath(match.getSaltIDs().get(0));
if (args.containsKey(KEY_FULLTEXT)) {
boolean isUsingRawText = visPlugin.get().isUsingRawText();
String aql = Helper.buildDocumentQuery(corpusPath, null, isUsingRawText);
Background.runWithCallback(() -> api.subgraphForQuery(corpusPath.get(0), aql, QueryLanguage.AQL, isUsingRawText ? AnnotationComponentType.ORDERING : null), new GraphMLLoaderCallback(corpusPath, visPlugin.get(), args));
} else {
SubgraphWithContext subgraphQuery = new SubgraphWithContext();
subgraphQuery.setLeft(Integer.parseInt(args.get(KEY_LEFT)[0]));
subgraphQuery.setRight(Integer.parseInt(args.get(KEY_RIGHT)[0]));
subgraphQuery.setNodeIds(match.getSaltIDs());
if (args.containsKey(KEY_SEGMENTATION)) {
subgraphQuery.setSegmentation(args.get(KEY_SEGMENTATION)[0]);
} else {
subgraphQuery.setSegmentation(null);
}
Background.runWithCallback(() -> api.subgraphForNodes(corpusPath.get(0), subgraphQuery), new GraphMLLoaderCallback(corpusPath, visPlugin.get(), args));
}
}
Aggregations