Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#950): using record search_keywords for highlighting #1235

Merged
merged 5 commits into from Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 9 additions & 10 deletions frontend/components/text-classifier/results/RecordExplain.vue
Expand Up @@ -33,19 +33,18 @@
<script>
export default {
props: {
queryText: {
type: String,
default: undefined,
record: {
type: Object,
required: true,
},
explain: {
type: Array,
},
predicted: {
type: String,
default: undefined,
},
}
},
computed: {
predicted() {
return this.record.predicted;
},
explainFormatted() {
// TODO ALLOW FOR MULTI LABEL
return this.explain.map((token) => {
Expand All @@ -58,8 +57,8 @@ export default {
percent = Math.round(Math.log10(percent) ** p * s);
}
return {
text: this.queryText
? this.$highlightSearch(this.queryText, token.token)
text: this.record.search_keywords
? this.$highlightKeyword(token.token, this.record.search_keywords)
: token.token,
percent: percent.toString(),
grad,
Expand Down
28 changes: 11 additions & 17 deletions frontend/components/text-classifier/results/RecordInputs.vue
Expand Up @@ -26,11 +26,10 @@
<span class="record__key">{{ index }}:</span>
<LazyRecordExplain
v-if="explanation"
:predicted="predicted"
:query-text="queryText"
:record="record"
:explain="explanation[index]"
/>
<LazyRecordString v-else :query-text="queryText" :text="text" />
<LazyRecordString v-else :record="record" :text="text" />
</span>
</span>
</div>
Expand All @@ -47,27 +46,22 @@
<script>
export default {
props: {
data: {
record: {
type: Object,
required: true,
},
queryText: {
type: String,
},
predicted: {
type: String,
default: undefined,
},
explanation: {
type: Object,
default: () => undefined,
},
required: true
}
},
data: () => ({
showFullRecord: false,
scrollHeight: undefined,
}),
computed: {
data() {
return this.record.inputs;
},
explanation() {
return this.record.explanation;
},
visibleRecordHeight() {
return this.$mq === "lg" ? 468 : 174;
},
Expand Down
19 changes: 13 additions & 6 deletions frontend/components/text-classifier/results/RecordString.vue
Expand Up @@ -21,7 +21,7 @@
<div v-for="item in text" :key="item.index">
<span
class="record__content"
v-html="$highlightSearch(queryText, item)"
v-html="$highlightKeywords(item, keywords)"
>
</span>
</div>
Expand All @@ -30,22 +30,28 @@
<span
v-else
class="record__content"
v-html="$highlightSearch(queryText, text)"
v-html="$highlightKeywords(text, keywords)"
>
</span>
</div>
</template>

<script>
export default {
props: {
record: {
type: Object,
required: true,
},
text: {
type: [String, Array],
required: true,
},
queryText: {
type: String,
default: undefined,
},
},
computed: {
keywords() {
return this.record.search_keywords
}
},
methods: {
isList(record) {
Expand All @@ -54,6 +60,7 @@ export default {
},
};
</script>

<style lang="scss" scoped>
.record {
&__content {
Expand Down
Expand Up @@ -20,12 +20,7 @@
<!-- annotation labels and prediction status -->
<div class="record--left">
<!-- record text -->
<RecordInputs
:predicted="record.predicted"
:data="record.inputs"
:explanation="record.explanation"
:query-text="dataset.query.text"
/>
<RecordInputs :record="record" />
<ClassifierAnnotationArea
v-if="annotationEnabled"
:dataset="dataset"
Expand Down
20 changes: 11 additions & 9 deletions frontend/components/text2text/results/RecordStringText2Text.vue
Expand Up @@ -20,7 +20,7 @@
ref="list"
:class="showFullRecord ? 'record__expanded' : 'record__collapsed'"
>
<span class="record__content" v-html="$highlightSearch(queryText, text)">
<span class="record__content" v-html="$highlightKeywords(text, keywords)">
</span>
<a
href="#"
Expand All @@ -34,20 +34,22 @@
<script>
export default {
props: {
text: {
type: [String, Array],
required: true,
},
queryText: {
type: String,
default: undefined,
},
record: {
type: Object,
required: true
}
},
data: () => ({
showFullRecord: false,
scrollHeight: undefined,
}),
computed: {
text() {
return this.record.text;
},
keywords() {
return this.record.search_keywords;
},
visibleRecordHeight() {
return this.$mq === "lg" ? 570 : 260;
},
Expand Down
5 changes: 1 addition & 4 deletions frontend/components/text2text/results/RecordText2Text.vue
Expand Up @@ -18,10 +18,7 @@
<template>
<div class="record">
<div class="record--left record__item">
<record-string-text-2-text
:query-text="dataset.query.text"
:text="record.text"
/>
<record-string-text-2-text :record="record" />
<div>
<text-2-text-list
ref="list"
Expand Down
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);
};
Expand Up @@ -3,7 +3,7 @@
exports[`RecordTextClassification renders properly 1`] = `
<div class="record">
<div class="record--left">
<recordinputs data="[object Object]" query-text="mock test"></recordinputs>
<recordinputs record="[object Object]"></recordinputs>
<classifierexplorationarea dataset="[object Object]" record="[object Object]"></classifierexplorationarea>
<!---->
</div>
Expand Down