import { useState } from "react"; import { api, getErrorMessage } from "../api"; import type { W2VQueryResult, W2VSimilarWord, CompareResponse } from "../types"; import { scoreColor } from "../utils/colors"; import ScoreBar from "./ScoreBar"; import StatusMessage from "./StatusMessage"; import DocumentViewer from "./DocumentViewer"; export default function Word2VecTools() { const [error, setError] = useState(""); // Similar words const [simWord, setSimWord] = useState(""); const [simTopK, setSimTopK] = useState(10); const [simResults, setSimResults] = useState([]); const [simLoading, setSimLoading] = useState(false); // Compare const [compTextA, setCompTextA] = useState(""); const [compTextB, setCompTextB] = useState(""); const [compResult, setCompResult] = useState(null); const [compLoading, setCompLoading] = useState(false); // Search const [queryText, setQueryText] = useState(""); const [queryTopK, setQueryTopK] = useState(5); const [queryResults, setQueryResults] = useState([]); const [queryLoading, setQueryLoading] = useState(false); async function handleSimilarWords() { setSimLoading(true); setError(""); try { const res = await api.w2vSimilarWords({ word: simWord, top_k: simTopK }); setSimResults(res.similar); } catch (err) { setError(getErrorMessage(err)); } finally { setSimLoading(false); } } async function handleCompare() { setCompLoading(true); setError(""); try { const res = await api.w2vCompare({ text_a: compTextA, text_b: compTextB }); setCompResult(res); } catch (err) { setError(getErrorMessage(err)); } finally { setCompLoading(false); } } async function handleQuery() { setQueryLoading(true); setError(""); try { const res = await api.w2vQuery({ text: queryText, top_k: queryTopK }); setQueryResults(res.results); } catch (err) { setError(getErrorMessage(err)); } finally { setQueryLoading(false); } } return (
{error && }
{/* Similar Words */}

Similar Words

Find words that appear in similar contexts using Word2Vec static embeddings.

setSimWord(e.target.value)} onKeyDown={e => e.key === "Enter" && handleSimilarWords()} placeholder="e.g. pizza" />
setSimTopK(+e.target.value)} min={1} max={50} style={{ width: 60 }} />
{simResults.length > 0 && ( {simResults.map((r, i) => ( ))}
WordSimilarity
{r.word}
)}
{/* Compare Texts */}

Compare Texts

Sentence similarity via averaged word vectors.

setCompTextA(e.target.value)} placeholder="pizza gives me homework" />
setCompTextB(e.target.value)} placeholder="school gives me homework" />
{compResult && (
{compResult.similarity.toFixed(4)}
Word2Vec Cosine Similarity
)}
{/* Semantic Search — full width */}

Semantic Search

Search your corpus using averaged Word2Vec vectors.

setQueryText(e.target.value)} onKeyDown={e => e.key === "Enter" && handleQuery()} placeholder="a place where children learn" />
setQueryTopK(+e.target.value)} min={1} max={20} style={{ width: 60 }} />
{queryResults.length > 0 && (
{queryResults.map((r, i) => (
#{r.rank} {r.doc_id}
{r.text}
))}
)}
); }