Skip to content

feat: enhance question similarity detection with error handling and improved parsing #1

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

Open
wants to merge 1 commit 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
56 changes: 39 additions & 17 deletions src/utils/isSimilarQuestion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,55 @@ export function isSimilarQuestion(
newQuestion: string,
previousQuestions: string
): boolean {
// Debug logging
console.log("Previous questions received:", previousQuestions);
console.log("New question:", newQuestion);

// If no previous questions, it can't be similar
if (!previousQuestions) {
if (!previousQuestions || typeof previousQuestions !== "string") {
Copy link
Owner Author

Choose a reason for hiding this comment

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

this is a comment to test the script GitHubPRCommentsFetcher

Copy link
Owner Author

Choose a reason for hiding this comment

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

ok got it, thanks

console.log("No previous questions or invalid type, returning false");
return false;
}

// Split the previous questions string into an array
const prevQuestions = previousQuestions.split(",");
try {
// Split the previous questions string into an array, handling both comma and newline separators
const prevQuestions = previousQuestions
.split(/[,\n]/)
.map((q) => q.trim())
.filter((q) => q.length > 0)
// Take only the last 10 questions to prevent performance issues
.slice(-10);

// If no valid previous questions after splitting, return false
if (prevQuestions.length === 0) {
console.log("No valid questions after splitting, returning false");
return false;
}

// Extract key concepts from the new question
const newConcepts = extractConcepts(newQuestion);
// Extract key concepts from the new question
const newConcepts = extractConcepts(newQuestion);

// Compare with each previous question
for (const prevQuestion of prevQuestions) {
const prevConcepts = extractConcepts(prevQuestion);
// Compare with each previous question
for (const prevQuestion of prevQuestions) {
const prevConcepts = extractConcepts(prevQuestion);

// Calculate concept overlap
const similarity = calculateConceptSimilarity(newConcepts, prevConcepts);
// Calculate concept overlap
const similarity = calculateConceptSimilarity(newConcepts, prevConcepts);

if (similarity > SIMILARITY_THRESHOLD) {
console.log(`Question similarity: ${similarity}`);
console.log("New question:", newQuestion);
console.log("Similar to:", prevQuestion);
return true;
if (similarity > SIMILARITY_THRESHOLD) {
console.log(`Question similarity: ${similarity}`);
console.log("New question:", newQuestion);
console.log("Similar to:", prevQuestion);
return true;
}
}
}

return false;
return false;
} catch (error) {
console.error("Error in isSimilarQuestion:", error);
// If there's an error, return false to allow the question
return false;
}
}

function extractConcepts(question: string): string[] {
Expand Down