User:Polygnotus/Scripts/AI Source Verification.js: Difference between revisions

Content deleted Content added
No edit summary
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 202:
font-size: 13px;
line-height: 1.5;
max-height: 200px480px;
overflow-y: auto;
white-space: pre-wrap;
Line 480:
}
 
extractClaimText(refElement) {
 
// Find the text node containing the reference
 
let currentNode = refElement.parentElement.previousSibling;
extractClaimText(refElement) {
let claim = '';
// Get the paragraph or container element
const container = refElement.closest('p, li, td, div, section');
// Look backwards to find the start of the sentence
whileif (currentNode!container) {
return '';
if (currentNode.nodeType === Node.TEXT_NODE) {
}
const text = currentNode.textContent;
claim = text + claim;
// Get all text content from the container
let fullText = '';
// Stop at sentence boundaries
const walker = document.createTreeWalker(
const sentenceEnd = text.match(/[.!?]\s*$/);
container,
if (sentenceEnd) {
break;NodeFilter.SHOW_TEXT,
}null,
false
} else if (currentNode.nodeType === Node.ELEMENT_NODE) {
);
// Include text from elements but stop at certain boundaries
if (currentNode.tagName && ['BR', 'P', 'DIV', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6'].includes(currentNode.tagName)) {
let textNode;
break;
const textNodes = [];
}
while (textNode = walker.nextNode()) {
claim = currentNode.textContent + claim;
}textNodes.push({
node: textNode,
currentNode = currentNode.previousSibling;
}text: textNode.textContent,
offset: fullText.length
});
// Clean up the claim text
fullText claim += claimtextNode.trim()textContent;
}
// If we didn't find a sentence boundary, try to extract from current element
// Find the position of the reference in the full text
if (!claim || claim.length < 10) {
const parentElementrefText = refElement.closest('p,textContent; li,// td,This div');should be like "[1]"
let refPosition = -1;
if (parentElement) {
const fullText = parentElement.textContent;
// Look for the reference pattern in the text
const refIndex = fullText.indexOf('[');
const refPattern = new RegExp('\\[\\s*' + refText.replace(/[\[\]]/g, '') + '\\s*\\]');
if (refIndex > 0) {
const claimrefMatch = fullText.substring(0, refIndex).trimmatch(refPattern);
if (refMatch) {
// Find the last sentence before the reference
refPosition = refMatch.index;
const sentences = claim.split(/[.!?]/);
} else {
if (sentences.length > 1) {
// Fallback: find any reference pattern before our element
claim = sentences[sentences.length - 2] + '.';
const allRefs = }fullText.match(/\[\d+\]/g);
if (allRefs) }{
// Find the last }reference before the end
}let lastRefIndex = -1;
let searchPos = 0;
returnfor claim.trim(const ref of allRefs); {
const index = fullText.indexOf(ref, searchPos);
}
if (index !== -1) {
lastRefIndex = index;
searchPos = index + ref.length;
}
}
refPosition = lastRefIndex;
}
}
if (refPosition === -1) {
refPosition = fullText.length;
}
// Extract text before the reference
const textBeforeRef = fullText.substring(0, refPosition).trim();
// Find the last complete sentence
const sentences = textBeforeRef.split(/([.!?]+\s+)/);
if (sentences.length === 1) {
// No sentence breaks found, return the entire text
return textBeforeRef;
}
// Reconstruct the last sentence
let lastSentence = '';
let foundSentenceEnd = false;
// Work backwards through the sentence fragments
for (let i = sentences.length - 1; i >= 0; i--) {
const fragment = sentences[i];
if (fragment.match(/^[.!?]+\s*$/)) {
// This is punctuation + whitespace
if (!foundSentenceEnd) {
foundSentenceEnd = true;
continue;
} else {
// We've found the end of the previous sentence
break;
}
} else {
// This is text content
lastSentence = fragment + lastSentence;
if (foundSentenceEnd) {
// We have a complete sentence
break;
}
}
}
// If we didn't find a proper sentence boundary, fall back to a more aggressive approach
if (!lastSentence.trim() || lastSentence.trim().length < 10) {
// Look for other potential sentence boundaries
const alternativeBreaks = textBeforeRef.split(/([.!?:;]\s+|^\s*[A-Z])/);
if (alternativeBreaks.length > 1) {
lastSentence = alternativeBreaks[alternativeBreaks.length - 1];
} else {
// As a last resort, take a reasonable chunk from the end
const words = textBeforeRef.split(/\s+/);
if (words.length > 15) {
lastSentence = words.slice(-15).join(' ');
} else {
lastSentence = textBeforeRef;
}
}
}
// Clean up the result
lastSentence = lastSentence.trim();
// Ensure it ends with proper punctuation for context
if (lastSentence && !lastSentence.match(/[.!?]$/)) {
// Check if the original text continues with punctuation
const nextChar = fullText.charAt(refPosition - lastSentence.length - 1);
if (nextChar.match(/[.!?]/)) {
lastSentence = nextChar + ' ' + lastSentence;
}
}
// Remove any remaining reference brackets that might have been included
lastSentence = lastSentence.replace(/\[\d+\]/g, '').trim();
return lastSentence;
}
 
 
 
 
 
 
 
extractReferenceUrl(refElement) {