summaryrefslogtreecommitdiff
path: root/packages/prosody-ui/src/Paragraph.tsx
blob: 72c43a74b4eaf3187bcf0c792a78b90886216dc4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { franc } from "franc-all";
import React, { useCallback, useEffect, useState } from "react";
import ThaiText from "./thai/ThaiText";
import { ColoredText } from "./components/Sentence";
import type { AnalyzeRes, WordData } from "./logic/types";
import { detectScript, langFromScript } from "./logic/utils";
import LatinText from "./latin/LatinText";
import { buildWiktionaryURL, parseWiktionary } from "./logic/wiki";
import type { Result } from "sortug";
import * as Stanza from "./logic/stanza";
import { iso6393To1 } from "./logic/iso6393to1";

export default function Paragraph({
  text,
}: {
  text: string;
  handleWord?: (wd: Result<WordData>) => any;
}) {
  useEffect(() => {
    segmentString();
  }, [text]);
  const [lang, setLang] = useState("");
  const [script, setScript] = useState("");
  const [segs, setSegs] = useState<Stanza.StanzaSegment[]>([]);
  useEffect(() => {
    const res = franc(text);
    console.log();
    console.log({ res, text });
    if (res === "und") detectLanguage();
    else {
      const smol = iso6393To1[res];
      if (!smol) console.log("lang", res);
      else setLang(smol);
    }
  }, [text]);

  const segmentString = useCallback(async () => {
    if (lang) {
      const res = await Stanza.segmenter(text, lang);
      if ("ok" in res) setSegs(res.ok.segments);
      console.log("stanza", res);
    }
  }, [text, lang]);
  const detectLanguage = useCallback(async () => {
    const script = detectScript(text);
    if ("error" in script) console.log("ded");
    else {
      setScript(script.ok);
      const lng = langFromScript(script.ok);
      if ("error" in lng) console.log("ded again!");
      else setLang(lng.ok);
    }
  }, [text]);

  return <div className="segmented-text">{text}</div>;
}