Merge branch 'main' into advanced-doclist

This commit is contained in:
Richard Roberson 2025-02-12 02:39:18 -07:00
commit b7f64a28b1
4 changed files with 113 additions and 83 deletions

View file

@ -1,31 +1,23 @@
import { NextRequest, NextResponse } from 'next/server';
import nlp from 'compromise'; import nlp from 'compromise';
// Text preprocessing function to clean and normalize text const MAX_BLOCK_LENGTH = 300;
export const preprocessSentenceForAudio = (text: string): string => {
const preprocessSentenceForAudio = (text: string): string => {
return text return text
// Replace URLs with descriptive text including domain
.replace(/\S*(?:https?:\/\/|www\.)([^\/\s]+)(?:\/\S*)?/gi, '- (link to $1) -') .replace(/\S*(?:https?:\/\/|www\.)([^\/\s]+)(?:\/\S*)?/gi, '- (link to $1) -')
// Remove special characters except basic punctuation
//.replace(/[^\w\s.,!?;:'"()-]/g, ' ')
// Fix hyphenated words at line breaks (word- word -> wordword)
.replace(/(\w+)-\s+(\w+)/g, '$1$2') .replace(/(\w+)-\s+(\w+)/g, '$1$2')
// Replace multiple spaces with single space
.replace(/\s+/g, ' ') .replace(/\s+/g, ' ')
// Trim whitespace
.trim(); .trim();
}; };
const MAX_BLOCK_LENGTH = 300; // Maximum characters per block const splitIntoSentences = (text: string): string[] => {
export const splitIntoSentences = (text: string): string[] => {
// Split text into paragraphs first
const paragraphs = text.split(/\n+/); const paragraphs = text.split(/\n+/);
const blocks: string[] = []; const blocks: string[] = [];
for (const paragraph of paragraphs) { for (const paragraph of paragraphs) {
if (!paragraph.trim()) continue; if (!paragraph.trim()) continue;
// Preprocess each paragraph
const cleanedText = preprocessSentenceForAudio(paragraph); const cleanedText = preprocessSentenceForAudio(paragraph);
const doc = nlp(cleanedText); const doc = nlp(cleanedText);
const rawSentences = doc.sentences().out('array') as string[]; const rawSentences = doc.sentences().out('array') as string[];
@ -35,23 +27,42 @@ export const splitIntoSentences = (text: string): string[] => {
for (const sentence of rawSentences) { for (const sentence of rawSentences) {
const trimmedSentence = sentence.trim(); const trimmedSentence = sentence.trim();
// If adding this sentence would exceed the limit, start a new block
if (currentBlock && (currentBlock.length + trimmedSentence.length + 1) > MAX_BLOCK_LENGTH) { if (currentBlock && (currentBlock.length + trimmedSentence.length + 1) > MAX_BLOCK_LENGTH) {
blocks.push(currentBlock.trim()); blocks.push(currentBlock.trim());
currentBlock = trimmedSentence; currentBlock = trimmedSentence;
} else { } else {
// Add to current block with a space if not empty
currentBlock = currentBlock currentBlock = currentBlock
? `${currentBlock} ${trimmedSentence}` ? `${currentBlock} ${trimmedSentence}`
: trimmedSentence; : trimmedSentence;
} }
} }
// Add the last block if not empty
if (currentBlock) { if (currentBlock) {
blocks.push(currentBlock.trim()); blocks.push(currentBlock.trim());
} }
} }
return blocks; return blocks;
}; };
export async function POST(req: NextRequest) {
try {
const { text } = await req.json();
if (!text) {
return NextResponse.json({ error: 'No text provided' }, { status: 400 });
}
if (text.length <= MAX_BLOCK_LENGTH) {
// Single sentence preprocessing
const cleanedText = preprocessSentenceForAudio(text);
return NextResponse.json({ sentences: [cleanedText] });
}
// Full text splitting into sentences
const sentences = splitIntoSentences(text);
return NextResponse.json({ sentences });
} catch (error) {
console.error('Error processing text:', error);
return NextResponse.json({ error: 'Failed to process text' }, { status: 500 });
}
}

View file

@ -33,6 +33,8 @@ import {
handleTextClick, handleTextClick,
} from '@/utils/pdf'; } from '@/utils/pdf';
import type { PDFDocumentProxy } from 'pdfjs-dist';
/** /**
* Interface defining all available methods and properties in the PDF context * Interface defining all available methods and properties in the PDF context
*/ */
@ -43,11 +45,12 @@ interface PDFContextType {
currDocPages: number | undefined; currDocPages: number | undefined;
currDocPage: number; currDocPage: number;
currDocText: string | undefined; currDocText: string | undefined;
pdfDocument: PDFDocumentProxy | undefined;
setCurrentDocument: (id: string) => Promise<void>; setCurrentDocument: (id: string) => Promise<void>;
clearCurrDoc: () => void; clearCurrDoc: () => void;
// PDF functionality // PDF functionality
onDocumentLoadSuccess: ({ numPages }: { numPages: number }) => void; onDocumentLoadSuccess: (pdf: PDFDocumentProxy) => void;
highlightPattern: (text: string, pattern: string, containerRef: React.RefObject<HTMLDivElement>) => void; highlightPattern: (text: string, pattern: string, containerRef: React.RefObject<HTMLDivElement>) => void;
clearHighlights: () => void; clearHighlights: () => void;
handleTextClick: ( handleTextClick: (
@ -75,15 +78,17 @@ export function PDFProvider({ children }: { children: ReactNode }) {
const [currDocURL, setCurrDocURL] = useState<string>(); const [currDocURL, setCurrDocURL] = useState<string>();
const [currDocName, setCurrDocName] = useState<string>(); const [currDocName, setCurrDocName] = useState<string>();
const [currDocText, setCurrDocText] = useState<string>(); const [currDocText, setCurrDocText] = useState<string>();
const [pdfDocument, setPdfDocument] = useState<PDFDocumentProxy>();
/** /**
* Handles successful document load * Handles successful document load
* *
* @param {Object} param0 - Object containing numPages * @param {Object} param0 - Object containing numPages
*/ */
const onDocumentLoadSuccess = useCallback(({ numPages }: { numPages: number }) => { const onDocumentLoadSuccess = useCallback((pdf: PDFDocumentProxy) => {
console.log('Document loaded:', numPages); console.log('Document loaded:', pdf.numPages);
setCurrDocPages(numPages); setCurrDocPages(pdf.numPages);
setPdfDocument(pdf);
}, [setCurrDocPages]); }, [setCurrDocPages]);
/** /**
@ -91,15 +96,15 @@ export function PDFProvider({ children }: { children: ReactNode }) {
*/ */
const loadCurrDocText = useCallback(async () => { const loadCurrDocText = useCallback(async () => {
try { try {
if (!currDocURL) return; if (!pdfDocument) return;
const text = await extractTextFromPDF(currDocURL, currDocPage); const text = await extractTextFromPDF(pdfDocument, currDocPage);
setCurrDocText(text); setCurrDocText(text);
setTTSText(text); setTTSText(text);
} catch (error) { } catch (error) {
console.error('Error loading PDF text:', error); console.error('Error loading PDF text:', error);
} }
}, [currDocURL, currDocPage, setTTSText]); }, [pdfDocument, currDocPage, setTTSText]);
/** /**
* Updates the current document text when the page changes * Updates the current document text when the page changes
@ -136,6 +141,7 @@ export function PDFProvider({ children }: { children: ReactNode }) {
setCurrDocURL(undefined); setCurrDocURL(undefined);
setCurrDocText(undefined); setCurrDocText(undefined);
setCurrDocPages(undefined); setCurrDocPages(undefined);
setPdfDocument(undefined);
stop(); stop();
}, [setCurrDocPages, stop]); }, [setCurrDocPages, stop]);
@ -153,6 +159,7 @@ export function PDFProvider({ children }: { children: ReactNode }) {
highlightPattern, highlightPattern,
clearHighlights, clearHighlights,
handleTextClick, handleTextClick,
pdfDocument,
}), }),
[ [
onDocumentLoadSuccess, onDocumentLoadSuccess,
@ -163,6 +170,7 @@ export function PDFProvider({ children }: { children: ReactNode }) {
currDocPage, currDocPage,
currDocText, currDocText,
clearCurrDoc, clearCurrDoc,
pdfDocument,
] ]
); );

View file

@ -29,7 +29,6 @@ import toast from 'react-hot-toast';
import { useParams } from 'next/navigation'; import { useParams } from 'next/navigation';
import { useConfig } from '@/contexts/ConfigContext'; import { useConfig } from '@/contexts/ConfigContext';
import { splitIntoSentences, preprocessSentenceForAudio } from '@/utils/nlp';
import { audioBufferToURL } from '@/utils/audio'; import { audioBufferToURL } from '@/utils/audio';
import { useAudioCache } from '@/hooks/audio/useAudioCache'; import { useAudioCache } from '@/hooks/audio/useAudioCache';
import { useVoiceManagement } from '@/hooks/audio/useVoiceManagement'; import { useVoiceManagement } from '@/hooks/audio/useVoiceManagement';
@ -152,51 +151,71 @@ export function TTSProvider({ children }: { children: React.ReactNode }) {
*/ */
const setText = useCallback((text: string) => { const setText = useCallback((text: string) => {
console.log('Setting page text:', text); console.log('Setting page text:', text);
const newSentences = splitIntoSentences(text);
// If skipBlank is enabled and there's no text
if (isPlaying && skipBlank && newSentences.length === 0) {
if (isEPUB && locationChangeHandlerRef.current) {
// For EPUB, use the location handler to move to next section
locationChangeHandlerRef.current('next');
toast.success('Skipping blank section', {
id: `epub-section-skip`,
iconTheme: {
primary: 'var(--accent)',
secondary: 'var(--background)',
},
style: {
background: 'var(--background)',
color: 'var(--accent)',
},
duration: 1000,
position: 'top-center',
});
return;
} else if (currDocPageNumber < currDocPages!) {
// For PDF, increment the page
incrementPage();
toast.success(`Skipping blank page ${currDocPageNumber}`, {
id: `page-${currDocPageNumber}`,
iconTheme: {
primary: 'var(--accent)',
secondary: 'var(--background)',
},
style: {
background: 'var(--background)',
color: 'var(--accent)',
},
duration: 1000,
position: 'top-center',
});
return;
}
}
setSentences(newSentences); fetch('/api/nlp', {
setNextPageLoading(false); method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ text }),
})
.then(response => {
if (!response.ok) {
throw new Error('Failed to process text');
}
return response.json();
})
.then(({ sentences: newSentences }) => {
// If skipBlank is enabled and there's no text
if (isPlaying && skipBlank && newSentences.length === 0) {
if (isEPUB && locationChangeHandlerRef.current) {
locationChangeHandlerRef.current('next');
toast.success('Skipping blank section', {
id: `epub-section-skip`,
iconTheme: {
primary: 'var(--accent)',
secondary: 'var(--background)',
},
style: {
background: 'var(--background)',
color: 'var(--accent)',
},
duration: 1000,
position: 'top-center',
});
return;
} else if (currDocPageNumber < currDocPages!) {
incrementPage();
toast.success(`Skipping blank page ${currDocPageNumber}`, {
id: `page-${currDocPageNumber}`,
iconTheme: {
primary: 'var(--accent)',
secondary: 'var(--background)',
},
style: {
background: 'var(--background)',
color: 'var(--accent)',
},
duration: 1000,
position: 'top-center',
});
return;
}
}
setSentences(newSentences);
setNextPageLoading(false);
})
.catch(error => {
console.error('Error processing text:', error);
toast.error('Failed to process text', {
style: {
background: 'var(--background)',
color: 'var(--accent)',
},
duration: 3000,
});
});
}, [isPlaying, skipBlank, currDocPageNumber, currDocPages, incrementPage, isEPUB]); }, [isPlaying, skipBlank, currDocPageNumber, currDocPages, incrementPage, isEPUB]);
/** /**
@ -437,9 +456,8 @@ export function TTSProvider({ children }: { children: React.ReactNode }) {
// Only set processing state if not preloading // Only set processing state if not preloading
if (!preload) setIsProcessing(true); if (!preload) setIsProcessing(true);
const cleanedSentence = preprocessSentenceForAudio(sentence); // No need to preprocess again since setText already did it
const audioBuffer = await getAudio(cleanedSentence); const audioBuffer = await getAudio(sentence);
return audioBufferToURL(audioBuffer!); return audioBufferToURL(audioBuffer!);
}, [isProcessing, audioContext, getAudio]); }, [isProcessing, audioContext, getAudio]);

View file

@ -2,9 +2,11 @@ import { pdfjs } from 'react-pdf';
import nlp from 'compromise'; import nlp from 'compromise';
import stringSimilarity from 'string-similarity'; import stringSimilarity from 'string-similarity';
import type { TextItem } from 'pdfjs-dist/types/src/display/api'; import type { TextItem } from 'pdfjs-dist/types/src/display/api';
import type { PDFDocumentProxy } from 'pdfjs-dist';
// Set worker from public directory // Set worker from public directory and compatibility mode
pdfjs.GlobalWorkerOptions.workerSrc = '/pdf.worker.mjs'; pdfjs.GlobalWorkerOptions.workerSrc = '/pdf.worker.mjs';
pdfjs.GlobalWorkerOptions.workerPort = null;
interface TextMatch { interface TextMatch {
elements: HTMLElement[]; elements: HTMLElement[];
@ -24,17 +26,8 @@ export function convertPDFDataToURL(pdfData: Blob): Promise<string> {
} }
// Text Processing functions // Text Processing functions
export async function extractTextFromPDF(pdfURL: string, pageNumber: number): Promise<string> { export async function extractTextFromPDF(pdf: PDFDocumentProxy, pageNumber: number): Promise<string> {
try { try {
const base64Data = pdfURL.split(',')[1];
const binaryData = atob(base64Data);
const bytes = new Uint8Array(binaryData.length);
for (let i = 0; i < binaryData.length; i++) {
bytes[i] = binaryData.charCodeAt(i);
}
const loadingTask = pdfjs.getDocument({ data: bytes });
const pdf = await loadingTask.promise;
const page = await pdf.getPage(pageNumber); const page = await pdf.getPage(pageNumber);
const textContent = await page.getTextContent(); const textContent = await page.getTextContent();