From 40641faf7aec2b03793726602098393f0b9dbd2e Mon Sep 17 00:00:00 2001 From: adueck Date: Thu, 19 Jan 2023 17:24:11 +0500 Subject: [PATCH] rough try of text conversion thing --- website/src/App.tsx | 6 +- website/src/lib/scriptToPhonetics.ts | 115 ++++++++++++++++++++++ website/src/screens/ScriptToPhonetics.tsx | 62 ++++++++++++ 3 files changed, 182 insertions(+), 1 deletion(-) create mode 100644 website/src/lib/scriptToPhonetics.ts create mode 100644 website/src/screens/ScriptToPhonetics.tsx diff --git a/website/src/App.tsx b/website/src/App.tsx index 2a0bc3f..e5df5e9 100644 --- a/website/src/App.tsx +++ b/website/src/App.tsx @@ -81,6 +81,7 @@ import { searchAllInflections } from "./lib/search-all-inflections"; import { addToWordlist, } from "./lib/wordlist-database"; +import ScriptToPhonetics from "./screens/ScriptToPhonetics"; // to allow Moustrap key combos even when input fields are in focus Mousetrap.prototype.stopCallback = function () { @@ -97,7 +98,7 @@ if (prod) { const possibleLandingPages = [ "/", "/about", "/settings", "/word", "/account", "/new-entries", "/share-target", "/phrase-builder", - "/privacy", + "/privacy", "/script-to-phonetics" ]; const editorOnlyPages = [ "/edit", "/review-tasks", @@ -656,6 +657,9 @@ class App extends Component { loadUser={this.handleLoadUser} /> + + + {this.state.user?.level === "editor" && wordToPhonetics(w, entries); + return words.map(f).join(" "); +} + + +function wordToPhonetics(p: string, entries: T.DictionaryEntry[]): string { + if (!isPashtoScript(p)) { + return p; + } + const results = dictionary.exactPashtoSearch(p); + const entryFs = results.map(entry => removeFVarients(entry.f)); + const inflectionsR = searchAllInflectionsCore(entries, p); + // TODO: also add directional prefix stuff + const inflections = inflectionsR.map(result => result.forms) + .flatMap(form => form.flatMap(x => x.matches.map(x => x.ps.f))); + const possibilities = [...new Set([...entryFs, ...inflections])]; + if (possibilities.length === 0) { + return p; + } + return possibilities.join("/"); +} + +export function searchAllInflectionsCore(allDocs: T.DictionaryEntry[], searchValue: string): InflectionSearchResult[] { + const preSearchFun = (ps: T.PsString) => ps.p.slice(0, 2) === searchValue.slice(0, 2); + const searchFun = (ps: T.PsString) => ps.p === searchValue; + // console.time(timerLabel); + return allDocs.reduce((all: InflectionSearchResult[], entry) => { + const type = isNounAdjOrVerb(entry); + if (entry.c && type === "verb") { + try { + const complement = (entry.l && entry.c.includes("comp.")) ? dictionary.findOneByTs(entry.l) : undefined; + const verbInfo = getVerbInfo(entry, complement); + const initialResults = searchPile(verbInfo as any, preSearchFun); + if (!initialResults.length) return all; + const conjugation = conjugateVerb( + entry, + complement, + ); + const forms = searchPile( + conjugation as any, + searchFun, + ); + if (forms.length) { + return [...all, { entry, forms }]; + } + return all; + } catch (e) { + console.error(e); + console.error("error inflecting", entry.p); + return all; + } + } + if (entry.c && type === "nounAdj") { + const inflections = inflectWord(entry); + if (!inflections) return all; + const forms = searchPile(inflections as any, searchFun); + if (forms.length) { + return [...all, { entry, forms }]; + } + } + return all; + }, []); +} + +function splitWords(p: string): string[] { + function isP(c: string): boolean { + return !!c.match(/[\u0621-\u065f\u0670-\u06d3\u06d5]/); + } + const words: string[] = []; + let current = ""; + let onP: boolean = true; + const chars = p.split(""); + for (let char of chars) { + const p = isP(char); + if (p) { + if (onP) { + current += char; + } else { + words.push(current); + current = char; + onP = true; + } + } else { + if (onP) { + words.push(current); + current = char; + onP = false; + } else { + current += char; + } + } + } + words.push(current); + return words.map(standardizePashto); +} \ No newline at end of file diff --git a/website/src/screens/ScriptToPhonetics.tsx b/website/src/screens/ScriptToPhonetics.tsx new file mode 100644 index 0000000..edd8032 --- /dev/null +++ b/website/src/screens/ScriptToPhonetics.tsx @@ -0,0 +1,62 @@ +/** + * Copyright (c) 2021 lingdocs.com + * + * This source code is licensed under the GPL3 license found in the + * LICENSE file in the root directory of this source tree. + * + */ + +import { FormEvent, useState } from "react"; +import { Helmet } from "react-helmet"; +import { scriptToPhonetics } from "../lib/scriptToPhonetics"; + +const preStyle: React.CSSProperties = { + overflowX: "auto", + whiteSpace: "pre-wrap", + wordWrap: "break-word", + lineHeight: "1.5", +}; + +const ScriptToPhonetics = () => { + const [text, setText] = useState(""); + const [result, setResult] = useState(""); + function handleConversion(e: FormEvent) { + e.preventDefault(); + setResult("Converting... Please wait..."); + setTimeout(() => { + setResult(scriptToPhonetics(text)); + }, 50); + } + + return
+ + + + Script to Phonetics - LingDocs Pashto Dictionary + +

Script to Phonetics

+
+
+ +