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

New search functionality #265

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 88 additions & 4 deletions src/components/Details/TranscriptSearch/TranscriptSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { strings } from "../../../assets/LocalizedStrings";
import { fontSizes } from "../../../styles/fonts";
import { screenWidths } from "../../../styles/mediaBreakpoints";
import cleanText from "../../../utils/cleanText";
import { varietyRoles } from "../../../stories/model-mocks/role";
alonr619 marked this conversation as resolved.
Show resolved Hide resolved

const Container = styled.div({
display: "flex",
Expand Down Expand Up @@ -100,14 +101,97 @@ const TranscriptSearch: FC<TranscriptSearchProps> = ({
if (!searchedTerm.trim()) {
return sentences.data;
}
const cleanedQuery = cleanText(searchedTerm);

let newTerm = searchedTerm;

// get beginning restricter
const beginIndStart = newTerm.indexOf('^"');
let beginRestriction = "";
if (beginIndStart !== -1) {
const beginIndFinal = newTerm.substring(beginIndStart + 2).indexOf('"') + beginIndStart + 2;
beginRestriction = newTerm.substring(beginIndStart + 2, beginIndFinal);
newTerm = newTerm.substring(0, beginIndStart) + newTerm.substring(beginIndFinal + 1);
}

// get ending restricter
const endIndStart = newTerm.indexOf('$"');
let endRestriction = "";
if (endIndStart !== -1) {
const endIndFinal = newTerm.substring(endIndStart + 2).indexOf('"') + endIndStart + 2;
endRestriction = newTerm.substring(endIndStart + 2, endIndFinal);
newTerm = newTerm.substring(0, endIndStart) + newTerm.substring(endIndFinal + 1);
}

// get all indices of double quotes
const regex = /"/gi,
indices = [],
required = [],
banned = [];
let result;
while ((result = regex.exec(newTerm))) {
indices.push(result.index);
}

// find everything inside these double quotes and remove them
for (let i = Math.floor(indices.length / 2) - 1; i >= 0; i--) {
if (newTerm.substring(indices[i * 2] - 1, indices[i * 2]) === "!") {
banned.push(newTerm.substring(indices[i * 2] + 1, indices[i * 2 + 1]));
} else {
required.push(newTerm.substring(indices[i * 2] + 1, indices[i * 2 + 1]));
}
newTerm =
newTerm.substring(0, indices[i * 2]) +
newTerm.substring(indices[i * 2 + 1] + 1, newTerm.length);
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you pull everything from line 104-145ish into a utility function that just lives a neighboring folder? e.g. util/, util/, etc? Cleaner and more testable to keep the logic outside of views, as each function can be assessed on its sole purpose.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do this once I fix John's bugs, thanks for the suggestion!

let ans = [];
console.log("1");
alonr619 marked this conversation as resolved.
Show resolved Hide resolved
const cleanedQuery = cleanText(newTerm);
console.log("2");
const tokenizedQuery = removeStopwords(cleanedQuery.split(" "));
console.log("3");
if (!cleanedQuery || tokenizedQuery.length === 0) {
// empty query or no valid tokens to search
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems this comment needs to be updated?

return [];
ans = [...sentences.data];
console.log("4");
} else {
const stemmedQuery = tokenizedQuery.map((token) => stem(token.toLowerCase()));
// the list before filtering out entries according to double quote rules
ans = sentences.data.filter((_, i) => stemmedQuery.some((q) => stemmedSentences[i].has(q)));
console.log("5");
}
console.log("6");
// filter out everything in prevAns that doesn't follow the double quote rules
for (let i = ans.length - 1; i >= 0; i--) {
console.log("7");
if (
beginRestriction !== "" &&
ans[i].text.substring(0, beginRestriction.length) !== beginRestriction
) {
ans.splice(i, 1);
continue;
}
if (
endRestriction !== "" &&
ans[i].text.substring(ans[i].text.length - endRestriction.length) !== endRestriction
) {
ans.splice(i, 1);
continue;
}
for (let j = 0; j < required.length; j++) {
if (!ans[i].text.includes(required[j])) {
ans.splice(i, 1);
continue;
}
}
for (let j = 0; j < banned.length; j++) {
if (ans[i].text.includes(banned[j])) {
ans.splice(i, 1);
continue;
}
}
}
const stemmedQuery = tokenizedQuery.map((token) => stem(token.toLowerCase()));
return sentences.data.filter((_, i) => stemmedQuery.some((q) => stemmedSentences[i].has(q)));
return ans;
}
return [];
}, [sentences.data, stemmedSentences, searchedTerm]);
Expand Down