Skip to content

Commit

Permalink
Merge pull request #1569 from inception-project/feature/1549-Filter-e…
Browse files Browse the repository at this point in the history
…ntity-candidates-by-description

#1549 - Filter entity candidates by description
  • Loading branch information
reckart authored Dec 18, 2019
2 parents a06c1f6 + 9bf550a commit 263480e
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@
import static de.tudarmstadt.ukp.clarin.webanno.support.lambda.LambdaBehavior.visibleWhen;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static org.apache.commons.lang3.StringUtils.containsIgnoreCase;
import static org.apache.commons.lang3.StringUtils.isNotBlank;
import static org.apache.commons.lang3.StringUtils.substringAfter;
import static org.apache.commons.lang3.StringUtils.substringBefore;
import static org.apache.wicket.event.Broadcast.BUBBLE;
import static org.apache.wicket.markup.head.JavaScriptHeaderItem.forReference;

import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

import org.apache.commons.lang3.StringUtils;
import org.apache.uima.cas.CAS;
Expand Down Expand Up @@ -160,6 +164,29 @@ private List<KBHandle> getCandidates(IModel<AnnotatorState> aStateModel,
if (aInput == null) {
return emptyList();
}

String input = aInput;

// Extract filter on the description
final String descriptionFilter;
if (input.contains("::")) {
descriptionFilter = substringAfter(input , "::");
input = substringBefore(input , "::");
}
else {
descriptionFilter = null;
}

// Extract exact match filter on the query
boolean labelFilter = false;
String trimmedInput = input.trim();
if (trimmedInput.length() > 2 && trimmedInput.startsWith("\"")
&& trimmedInput.endsWith("\"")) {
input = StringUtils.substring(trimmedInput, 1, -1);
labelFilter = true;
}

final String finalInput = input;

List<KBHandle> choices;
try {
Expand All @@ -185,8 +212,8 @@ private List<KBHandle> getCandidates(IModel<AnnotatorState> aStateModel,
: -1;

choices = clService.getLinkingInstancesInKBScope(traits.getRepositoryId(),
traits.getScope(), traits.getAllowedValueType(), aInput, mention, mentionBegin,
cas, feat.getProject());
traits.getScope(), traits.getAllowedValueType(), finalInput, mention,
mentionBegin, cas, feat.getProject());
}
catch (Exception e) {
choices = asList(new KBHandle("http://ERROR", "ERROR", e.getMessage(), "en"));
Expand All @@ -196,6 +223,19 @@ private List<KBHandle> getCandidates(IModel<AnnotatorState> aStateModel,
.find(IPartialPageRequestHandler.class)
.ifPresent(target -> target.addChildren(getPage(), IFeedback.class));
}

if (labelFilter) {
choices = choices.stream()
.filter(kb -> containsIgnoreCase(kb.getUiLabel(), finalInput))
.collect(Collectors.toList());
}

if (isNotBlank(descriptionFilter)) {
choices = choices.stream()
.filter(kb -> containsIgnoreCase(kb.getDescription(), descriptionFilter))
.collect(Collectors.toList());
}

return choices;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,14 @@

Concept features are shown as an auto-complete field. Typing into the field triggers a query against
the knowledge base and displays candidates in a dropdown list.

.Filtering by description
The query entered into the field only matches against the label of the knowledge base items, not
against their description. However, you can filter the candidates by their description. E.g. if you
wish to find all knowledge base items with `Obama` in the label and `president` in the description,
then you can write `Obama :: president`. A case-insensitive matching is being used.

.Substring matching
Depending on the knowledge base and full-text mode being used, there may be fuzzy matching. To
filter the candidate list down to those candidates which contain a particular substring, put
double quotes around the query, e.g. `"County Carlow"`. A case-insensitive matching is being used.

0 comments on commit 263480e

Please sign in to comment.