Skip to content

Commit

Permalink
#3822 - Order of matches found in knowledge base search is not correct
Browse files Browse the repository at this point in the history
- If the best match term differs from the KBHandle primary UI label, display it also in the dropdown
  • Loading branch information
reckart committed Feb 21, 2023
1 parent 03cc990 commit 24a515a
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@
.item-title {
font-weight: bolder;
}

.item-alt-title {
font-size: 85%;
line-height: normal;
padding-left: 10px;
}

.item-title .badge {
font-size: 8px;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static de.tudarmstadt.ukp.inception.conceptlinking.model.CandidateEntity.KEY_MENTION_CONTEXT;
import static de.tudarmstadt.ukp.inception.conceptlinking.model.CandidateEntity.KEY_MENTION_NC;
import static de.tudarmstadt.ukp.inception.conceptlinking.model.CandidateEntity.KEY_QUERY;
import static de.tudarmstadt.ukp.inception.conceptlinking.model.CandidateEntity.KEY_QUERY_BEST_MATCH_TERM_NC;
import static de.tudarmstadt.ukp.inception.conceptlinking.model.CandidateEntity.KEY_QUERY_NC;
import static org.apache.commons.lang3.StringUtils.join;

Expand Down Expand Up @@ -53,28 +54,32 @@ public void apply(CandidateEntity aCandidate)
aCandidate.getHandle().getMatchTerms().forEach(p -> update(aCandidate, p.getKey()));
}

private void update(CandidateEntity aCandidate, String label)
private void update(CandidateEntity aCandidate, String aTerm)
{
String labelNC = label.toLowerCase(aCandidate.getLocale());
String termNC = aTerm.toLowerCase(aCandidate.getLocale());

aCandidate.get(KEY_MENTION_NC) //
.map(mention -> lev.apply(labelNC, mention)) //
.map(mention -> lev.apply(termNC, mention)) //
.ifPresent(score -> aCandidate.mergeMin(KEY_LEVENSHTEIN_MENTION_NC, score));

aCandidate.get(KEY_QUERY_NC) //
.map(query -> lev.apply(labelNC, query)) //
.ifPresent(score -> aCandidate.mergeMin(KEY_LEVENSHTEIN_QUERY_NC, score));
.map(query -> lev.apply(termNC, query)) //
.ifPresent(score -> {
if (aCandidate.mergeMin(KEY_LEVENSHTEIN_QUERY_NC, score)) {
aCandidate.put(KEY_QUERY_BEST_MATCH_TERM_NC, aTerm);
}
});

aCandidate.get(KEY_MENTION) //
.map(mention -> lev.apply(label, mention)) //
.map(mention -> lev.apply(aTerm, mention)) //
.ifPresent(score -> aCandidate.mergeMin(KEY_LEVENSHTEIN_MENTION, score));

aCandidate.get(KEY_QUERY) //
.map(query -> lev.apply(label, query)) //
.map(query -> lev.apply(aTerm, query)) //
.ifPresent(score -> aCandidate.mergeMin(KEY_LEVENSHTEIN_QUERY, score));

aCandidate.get(KEY_MENTION_CONTEXT) //
.map(context -> lev.apply(label, join(context, ' '))) //
.map(context -> lev.apply(aTerm, join(context, ' '))) //
.ifPresent(score -> aCandidate.mergeMin(KEY_LEVENSHTEIN_MENTION_CONTEXT, score));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
package de.tudarmstadt.ukp.inception.conceptlinking.model;

import static java.lang.Integer.MAX_VALUE;
import static java.util.Collections.emptySet;
import static java.util.Collections.unmodifiableMap;

Expand All @@ -43,6 +44,13 @@ public class CandidateEntity
public static final Key<String> KEY_QUERY = new Key<>("query");
public static final Key<String> KEY_QUERY_NC = new Key<>("queryNC");

/**
* The term which had the best match with query or mention. This should be displayed to the user
* in addition to the handles pref-label if it does differ from the pref-label.
*/
public static final Key<String> KEY_QUERY_BEST_MATCH_TERM_NC = new Key<>(
"queryBestMatchTermNC");

/**
* Whether the query entered by the user is completely in lower case.
*/
Expand All @@ -68,11 +76,10 @@ public class CandidateEntity
* the default value to ensure that candidates are ranked last on this feature if it could not
* be calculated.
*/
public static final Key<Integer> KEY_LEVENSHTEIN_MENTION = new Key<>("levMention",
Integer.MAX_VALUE);
public static final Key<Integer> KEY_LEVENSHTEIN_MENTION = new Key<>("levMention", MAX_VALUE);

public static final Key<Integer> KEY_LEVENSHTEIN_MENTION_NC = new Key<>("levMentionNC",
Integer.MAX_VALUE);
MAX_VALUE);

/**
* Edit distance between mention + context and candidate entity label
Expand All @@ -82,7 +89,7 @@ public class CandidateEntity
* be calculated.
*/
public static final Key<Integer> KEY_LEVENSHTEIN_MENTION_CONTEXT = new Key<>("levContext",
Integer.MAX_VALUE);
MAX_VALUE);

/**
* Edit distance between typed string and candidate entity label
Expand All @@ -91,11 +98,9 @@ public class CandidateEntity
* the default value to ensure that candidates are ranked last on this feature if it could not
* be calculated.
*/
public static final Key<Integer> KEY_LEVENSHTEIN_QUERY = new Key<>("levQuery",
Integer.MAX_VALUE);
public static final Key<Integer> KEY_LEVENSHTEIN_QUERY = new Key<>("levQuery", MAX_VALUE);

public static final Key<Integer> KEY_LEVENSHTEIN_QUERY_NC = new Key<>("levQueryNC",
Integer.MAX_VALUE);
public static final Key<Integer> KEY_LEVENSHTEIN_QUERY_NC = new Key<>("levQueryNC", MAX_VALUE);

/**
* set of directly related entities as IRI Strings
Expand Down Expand Up @@ -220,10 +225,11 @@ public <T> T put(Key<T> aKey, T aValue)
}
}

public int mergeMin(Key<Integer> aKey, int aValue)
public boolean mergeMin(Key<Integer> aKey, int aValue)
{
return (int) features.merge(aKey.name, aValue,
var newValue = (int) features.merge(aKey.name, aValue,
(o, n) -> o == null ? n : Math.min((int) o, (int) n));
return newValue == aValue;
}

public Map<String, Object> getFeatures()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static de.tudarmstadt.ukp.inception.conceptlinking.model.CandidateEntity.KEY_MENTION_CONTEXT;
import static de.tudarmstadt.ukp.inception.conceptlinking.model.CandidateEntity.KEY_MENTION_NC;
import static de.tudarmstadt.ukp.inception.conceptlinking.model.CandidateEntity.KEY_QUERY;
import static de.tudarmstadt.ukp.inception.conceptlinking.model.CandidateEntity.KEY_QUERY_BEST_MATCH_TERM_NC;
import static de.tudarmstadt.ukp.inception.conceptlinking.model.CandidateEntity.KEY_QUERY_NC;
import static java.lang.System.currentTimeMillis;
import static java.util.Arrays.asList;
Expand Down Expand Up @@ -453,6 +454,9 @@ public List<KBHandle> rankCandidates(String aQuery, String aMention, Set<KBHandl
.map(candidate -> {
KBHandle handle = candidate.getHandle();
handle.setDebugInfo(String.valueOf(candidate.getFeatures()));
candidate.get(KEY_QUERY_BEST_MATCH_TERM_NC)
.filter(t -> !t.equalsIgnoreCase(handle.getUiLabel()))
.ifPresent(handle::setQueryBestMatchTerm);
return handle;
}) //
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class KBHandle
private static final long serialVersionUID = -4284462837460396185L;
private String identifier;
private String name;
private String queryBestMatchTerm;
private Set<Pair<String, String>> matchTerms;
private String description;
private KnowledgeBase kb;
Expand Down Expand Up @@ -209,6 +210,16 @@ public String getDebugInfo()
return debugInfo;
}

public void setQueryBestMatchTerm(String aTerm)
{
queryBestMatchTerm = aTerm;
}

public String getQueryBestMatchTerm()
{
return queryBestMatchTerm;
}

public int getRank()
{
return rank;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,12 @@ public String getText()
sb.append(" # if (data.rank) { if (data.rank != '0') { #");
sb.append(" <span class=\"item-rank\">[${ data.rank }]</span>");
sb.append(" # } } #");
sb.append(" ${ data.uiLabel }");
sb.append(" # if (data.queryBestMatchTerm) { #");
sb.append(" ${ data.queryBestMatchTerm }");
sb.append(" <div class='item-alt-title'>${ data.uiLabel }</div>");
sb.append(" # } else { #");
sb.append(" ${ data.uiLabel }");
sb.append(" # } #");
sb.append("</span>");
sb.append("<div class=\"item-identifier\">");
sb.append(" ${ data.identifier }");
Expand All @@ -64,6 +69,7 @@ public List<String> getTextProperties()
properties.add("identifier");
properties.add("description");
properties.add("rank");
properties.add("queryBestMatchTerm");
if (DEVELOPMENT.equals(Application.get().getConfigurationType())) {
properties.add("debugInfo");
}
Expand Down

0 comments on commit 24a515a

Please sign in to comment.