-
Notifications
You must be signed in to change notification settings - Fork 28
New search functionality #265
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"; | ||
|
||
const Container = styled.div({ | ||
display: "flex", | ||
|
@@ -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); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]); | ||
|
Uh oh!
There was an error while loading. Please reload this page.