| import { useState } from "react"; | |
| import { api, getErrorMessage } from "../api"; | |
| /** | |
| * Shared hook for loading corpus text from the engine and parsing it into documents. | |
| * Used by both TrainingPanel and Word2VecPanel. | |
| */ | |
| export function useCorpusLoader() { | |
| const [corpusText, setCorpusText] = useState(""); | |
| const [loading, setLoading] = useState(false); | |
| const [error, setError] = useState(""); | |
| function parseCorpus(): string[] { | |
| return corpusText | |
| .split(/\n{2,}/) | |
| .map((t) => t.trim()) | |
| .filter((t) => t.length > 20); | |
| } | |
| async function loadFromEngine() { | |
| setLoading(true); | |
| setError(""); | |
| try { | |
| const res = await api.getCorpusTexts(); | |
| if (res.documents.length === 0) { | |
| setError("No documents loaded in the engine. Load a dataset first."); | |
| return; | |
| } | |
| setCorpusText( | |
| res.documents.map((d: { doc_id: string; text: string }) => d.text).join("\n\n") | |
| ); | |
| } catch (e) { | |
| setError(getErrorMessage(e)); | |
| } finally { | |
| setLoading(false); | |
| } | |
| } | |
| return { | |
| corpusText, | |
| setCorpusText, | |
| loading, | |
| error, | |
| setError, | |
| parseCorpus, | |
| loadFromEngine, | |
| }; | |
| } | |