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

Parse without examples #112

Merged
merged 6 commits into from
Nov 14, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions frontend/src/components/Questions/QuestionCategories.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
export const QUESTION_CATEGORIES = [
"Strings",
"Data Structures",
"Algorithms",
"Algorithm",
"Data Structure",
"Array",
"String",
"Hash Table",
"Math",
"Dynamic Programming",
"Math",
"Sorting",
"Greedy",
"Depth-First Search",
Expand All @@ -15,19 +15,21 @@ export const QUESTION_CATEGORIES = [
"Tree",
"Matrix",
"Two Pointers",
"Binary Tree",
"Bit Manipulation",
"Binary Tree",
"Heap (Priority Queue)",
"Stack",
"Prefix Sum",
"Graph",
"Simulation",
"Design",
"Counting",
"Backtracking",
"Sliding Window",
"Backtracking",
"Union Find",
"Linked List",
"Ordered Set",
"Enumeration",
"Ordered Set",
"Monotonic Stack",
"Trie",
"Recursion",
Expand All @@ -36,17 +38,20 @@ export const QUESTION_CATEGORIES = [
"Bitmask",
"Queue",
"Binary Search Tree",
"Segment Tree",
"Memoization",
"Geometry",
"Topological Sort",
"Binary Indexed Tree",
"Topological Sort",
"Game Theory",
"Hash Function",
"Shortest Path",
"Combinatorics",
"Shortest Path",
"Interactive",
"String Matching",
"Data Stream",
"Rolling Hash",
"Brainteaser",
"Randomized",
"Monotonic Queue",
"Merge Sort",
Expand All @@ -56,6 +61,15 @@ export const QUESTION_CATEGORIES = [
"Probability and Statistics",
"Quickselect",
"Bucket Sort",
"Suffix Array",
"Minimum Spanning Tree",
"Counting Sort",
"Shell",
"Line Sweep",
"Reservoir Sampling",
"Strongly Connected Component",
"Eulerian Circuit",
"Radix Sort",
"Rejection Sampling",
"Biconnected Component"
]
19 changes: 13 additions & 6 deletions frontend/src/components/Questions/QuestionForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import DialogContent from "@mui/material/DialogContent";
import DialogActions from "@mui/material/DialogActions";
import Snackbar from "@mui/material/Snackbar";
import MuiAlert from "@mui/material/Alert";
import {parseHtmlDescriptionWithoutExamples} from "../../utils/utils"

interface QuestionFormProps {
question?: Question;
Expand Down Expand Up @@ -102,13 +103,16 @@ const QuestionForm: React.FC<QuestionFormProps> = ({
const words1 = description1.split(" ");
const words2 = description2.split(" ");


const commonWords = words1.filter((word) => words2.includes(word));

const similarity = commonWords.length / words1.length;
const similarityRatio = commonWords.length / words1.length;
const sharedWordCount = commonWords.length;

return similarity;
return { similarityRatio, sharedWordCount };
}


const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
// Check for empty fields
Expand All @@ -127,12 +131,14 @@ const QuestionForm: React.FC<QuestionFormProps> = ({

// Check for similar questions
const similarityThreshold = 0.6; // Adjust as needed
const sharedWordCountThreshold = 20;
const similarQuestions = questions.filter((existingQuestion) => {
const similarity = calculateSimilarity(
const { similarityRatio, sharedWordCount } = calculateSimilarity(
existingQuestion.description,
question.description
);
return similarity >= similarityThreshold;
return (similarityRatio >= similarityThreshold || sharedWordCount >= sharedWordCountThreshold)
&& existingQuestion.id !== question.id;
});

if (similarQuestions.length > 0) {
Expand All @@ -144,6 +150,7 @@ const QuestionForm: React.FC<QuestionFormProps> = ({
}
};


const handleCloseDialog = () => {
setShowSimilarQuestionsDialog(false);
};
Expand Down Expand Up @@ -373,8 +380,8 @@ const QuestionForm: React.FC<QuestionFormProps> = ({
{similarQuestions.map((similarQuestion) => (
<div key={similarQuestion.id}>
<Typography variant="h6">{similarQuestion.title}</Typography>
<Typography variant="body2">
{similarQuestion.description}
<Typography variant="body2" component="span">
{parseHtmlDescriptionWithoutExamples(similarQuestion.description)}
</Typography>
</div>
))}
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/Questions/QuestionsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
Typography,
} from "@mui/material";
import { useData } from "../../data/data.context";
import {parseHtmlDescription} from "../../utils/utils";
import {parseHtmlDescriptionWithoutExamples} from "../../utils/utils";

interface Question {
id: string;
Expand Down Expand Up @@ -203,7 +203,7 @@ const InterviewQuestionsTable: React.FC = () => {
<b>Difficulty:</b> {selectedQuestion.difficulty}
</Typography>
<Typography component={'span'} variant="body2" style={{ padding: "5px" }}>
<b>Description</b>: {parseHtmlDescription(selectedQuestion.description)}
<b>Description</b>: {parseHtmlDescriptionWithoutExamples(selectedQuestion.description)}
</Typography>
</DialogContent>
<DialogActions>
Expand Down
24 changes: 21 additions & 3 deletions frontend/src/utils/utils.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
import {decode} from "html-entities";
import htmr from "htmr";
import {ReactNode} from "react";
import DOMPurify from "dompurify";

export function parseHtmlDescription(description: string): ReactNode {
// Decode escaped HTML characters and add text wrap to pre tags in the question description
try {
let decodedDescription = decode(description)
.replace(/<pre>/g, "<pre style=\"white-space: pre-wrap;\">");
decodedDescription = DOMPurify.sanitize(decodedDescription);
return htmr(decodedDescription);
}
} catch (e) {
console.log(e)
return description;
}
}

export function parseHtmlDescriptionWithoutExamples(description: string): ReactNode {
// Decode escaped HTML characters and add text wrap to pre tags in the question description
try {
let decodedDescription = decode(description)
.replace(/<pre>/g, "<pre style=\"white-space: pre-wrap;\">");
const exampleIndex = decodedDescription?.indexOf("Example");

const newDescription = exampleIndex !== -1 ? decodedDescription?.substring(0, exampleIndex) : decodedDescription;
return htmr(newDescription);
} catch(e) {
console.log(e)
return description;
}
}
Loading