Skip to content

Commit

Permalink
feat(#950): using record search_keywords for highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
frascuchon committed Mar 8, 2022
1 parent 6899272 commit 829dc89
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 18 deletions.
23 changes: 11 additions & 12 deletions frontend/components/token-classifier/results/EntityHighlight.vue
Expand Up @@ -19,12 +19,7 @@
<span
@mouseenter="showTooltip = true"
@mouseleave="showTooltip = false"
:class="[
'highlight',
span.origin,
// isText ? '' : 'highlight--block',
annotationEnabled ? 'editable' : null,
]"
:class="['highlight', span.origin, annotationEnabled ? 'editable' : null]"
><span
v-for="(token, i) in span.tokens"
:key="i"
Expand All @@ -37,7 +32,7 @@
@click="openTagSelector"
@dblclick="removeEntity"
v-html="
`${$highlightSearch(dataset.query.text, token.text)}${
`${$highlightKeywords(token.text, record.search_keywords)}${
token.hasSpaceAfter && i + 1 !== span.tokens.length ? ' ' : ''
}`
"
Expand All @@ -53,6 +48,7 @@
<lazy-text-span-tooltip v-if="showTooltip" :span="span" />
</span>
</template>

<script>
import "assets/icons/cross";
Expand All @@ -69,6 +65,10 @@ export default {
type: Object,
required: true,
},
record: {
type: Object,
required: true,
}
},
data: () => {
return {
Expand All @@ -79,9 +79,6 @@ export default {
};
},
computed: {
// isText() {
// return this.text.replace(/\s/g, "").length;
// },
annotationEnabled() {
return this.dataset.viewSettings.viewMode === "annotate";
},
Expand Down Expand Up @@ -110,16 +107,17 @@ export default {
},
};
</script>

<style lang="scss" scoped>
.whitespace {
margin-right: 3.5px;
}
.highlight {
@include font-size(0);
line-height: 1em;
position: relative;
cursor: default;
// display: inline-flex;
cursor: default; // display: inline-flex;
border-radius: 2px;
padding: 0;
&.editable {
Expand All @@ -146,6 +144,7 @@ export default {
z-index: 5;
}
}
.remove-button {
opacity: 0;
z-index: -1;
Expand Down
3 changes: 2 additions & 1 deletion frontend/components/token-classifier/results/TextSpan.vue
Expand Up @@ -22,6 +22,7 @@
:class="['color_' + tag_color, { zindex3: showEntitiesSelector }]"
:span="token"
:dataset="dataset"
:record="record"
@openTagSelector="openTagSelector"
@removeEntity="removeEntity"
/><span
Expand All @@ -32,7 +33,7 @@
v-for="(t, i) in token.tokens"
:key="i"
v-html="
`${$highlightSearch(dataset.query.text, t.text)}${
`${$highlightKeywords(t.text, record.search_keywords)}${
token.hasSpaceAfter ? ' ' : ''
}`
"
Expand Down
Expand Up @@ -22,6 +22,7 @@
:class="['color_' + tag_color]"
:span="token"
:dataset="dataset"
:record="record"
/><template v-else v-for="t in token.tokens">{{ t.text }}</template
><template>{{ token.hasSpaceAfter ? " " : "" }}</template>
</span>
Expand Down
2 changes: 2 additions & 0 deletions frontend/models/Common.js
Expand Up @@ -34,6 +34,7 @@ class BaseRecord {
status,
selected,
event_timestamp,
search_keywords,
}) {
this.id = id;
this.metadata = metadata;
Expand All @@ -43,6 +44,7 @@ class BaseRecord {
this.status = status;
this.selected = selected || false;
this.event_timestamp = event_timestamp;
this.search_keywords = search_keywords || [];
}

recordTitle() {
Expand Down
34 changes: 29 additions & 5 deletions frontend/plugins/highlight-search.js
Expand Up @@ -16,24 +16,48 @@
*/

export default (context, inject) => {
const highlightSearch = function (query, text) {
const escapedText = text
const escapeText = function (text) {
return text
.toString()
.replace(/&/g, "&amp;")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/"/g, "&quot;")
.replace(/'/g, "&#039;");
};

const regexFromTerm = function (term) {
let q = term.replace(/[-[\]{}()*+?.,\\/^$|#\s]/g, "");
return new RegExp(q, "gi");
};

const highlightSearch = function (query, text) {
const escapedText = escapeText(text);
if (!query) {
return escapedText;
return text;
}
let q = query.replace(/[-[\]{}()*+?.,\\/^$|#\s]/g, "");

return escapedText
.toString()
.replace(
new RegExp(q, "gi"),
regexFromTerm(query),
(match) => `<span class="highlight-text">${match}</span>`
);
};

const highlightKeywords = function (text, keywords) {
let escapedText = escapeText(text).toString();

(keywords || []).forEach((keyword) => {
escapedText = escapedText.replace(
regexFromTerm(keyword),
(match) => `<span class="highlight-text">${match}</span>`
);
});

return escapedText;
};

inject("highlightSearch", highlightSearch);
inject("highlightKeywords", highlightKeywords);
};

0 comments on commit 829dc89

Please sign in to comment.