Skip to content

Commit

Permalink
generate from paragraph
Browse files Browse the repository at this point in the history
  • Loading branch information
krmanik committed Feb 11, 2024
1 parent 94dc6a3 commit b5c35cf
Show file tree
Hide file tree
Showing 4 changed files with 461 additions and 618 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"docusaurus-plugin-code-preview": "^0.0.1",
"genanki-js": "^2.0.0",
"hanzi-writer": "^3.3.0",
"jieba-wasm": "^0.0.2",
"path-browserify": "^1.0.1",
"primereact": "^10.5.0",
"prism-react-renderer": "^1.3.5",
Expand Down
86 changes: 72 additions & 14 deletions src/pages/create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import pinzhu from "../dict/pinyinzhuyin";

import pinyin from "chinese-to-pinyin";

import init, { cut } from "jieba-wasm/pkg/web/jieba_rs_wasm.js";

export default function CreateDeck(): JSX.Element {
const [words, setWords] = useState<
{
Expand Down Expand Up @@ -204,16 +206,39 @@ export default function CreateDeck(): JSX.Element {
let Zhuyin = [];
let Syllable = [];
let Definitions = [];
let doNotAdd = [];

for (let res of result) {
if (res.simplified == result[0].simplified) {
Pinyin.push(decodeHtmlEntities(res.pinyin));
Zhuyin.push(decodeHtmlEntities(res.zhuyin));
Syllable.push(res.syllable);
Definitions.push(res.definitions);
if (word.trim() !== result[0].simplified) {
doNotAdd.push(word.trim());
} else {
Pinyin.push(decodeHtmlEntities(res.pinyin));
Zhuyin.push(decodeHtmlEntities(res.zhuyin));
Syllable.push(res.syllable);
Definitions.push(res.definitions);
}
}
}

if (doNotAdd.includes(word.trim())) {
const url = `https://translate.googleapis.com/translate_a/single?client=gtx&sl=zh-CN&tl=en-US&dt=t&q=${word.trim()}`;
const response = await fetch(url);
const data = await response.json();

result[0].simplified = word.trim();
result[0].traditional = Chinese.s2t(word.trim());

let pin = pinyin(word.trim(), { toneToNumber: true });
pin = pin.replace(/0/g, "5");
let pizh = await pinzhu.pinyinAndZhuyin(pin, "", "");

Pinyin = [decodeHtmlEntities(pizh[1])];
Zhuyin = [decodeHtmlEntities(pizh[2])];
Syllable = [pin];
Definitions.push(data[0][0][0]);
}

setWords([
...words,
{
Expand All @@ -233,6 +258,9 @@ export default function CreateDeck(): JSX.Element {
useEffect(() => {
DICT.loadDict();
setupSql();
init(
"https://cdn.jsdelivr.net/npm/jieba-wasm@latest/pkg/web/jieba_rs_wasm_bg.wasm"
);
}, []);

const setupSql = async () => {
Expand Down Expand Up @@ -343,34 +371,64 @@ export default function CreateDeck(): JSX.Element {
setSelectWord(null);
}

function filterChineseWords(array) {
const chineseRegex = /[\u4e00-\u9fa5]/;
const chineseWords = array.filter((word) => chineseRegex.test(word));
return chineseWords;
}

async function generateFromParagraph(e) {
let text = texAreaValue;
let _words = [];

while (text.trim() != "") {
let res = DICT.search(text);
let cutWords = cut(text, true);
cutWords = filterChineseWords(cutWords);

for (let word of cutWords) {
let res = DICT.search(word);
let result = await DICT.makeHtml(res, true);
let pattern = new RegExp(result[0].simplified, "g");
text = text.replace(pattern, "");

let Pinyin = [];
let Zhuyin = [];
let Syllable = [];
let Definitions = [];
let doNotAdd = [];

for (let res of result) {
if (res.simplified == result[0].simplified) {
Pinyin.push(decodeHtmlEntities(res.pinyin));
Zhuyin.push(decodeHtmlEntities(res.zhuyin));
Syllable.push(res.syllable);
Definitions.push(res.definitions);
if (word.trim() !== result[0].simplified) {
doNotAdd.push(word.trim());
} else {
Pinyin.push(decodeHtmlEntities(res.pinyin));
Zhuyin.push(decodeHtmlEntities(res.zhuyin));
Syllable.push(res.syllable);
Definitions.push(res.definitions);
}
}
}

if (words.some((w) => w.Simplified === result[0].simplified)) {
continue;
}

if (doNotAdd.includes(word.trim())) {
const url = `https://translate.googleapis.com/translate_a/single?client=gtx&sl=zh-CN&tl=en-US&dt=t&q=${word.trim()}`;
const response = await fetch(url);
const data = await response.json();

result[0].simplified = word.trim();
result[0].traditional = Chinese.s2t(word.trim());

let pin = pinyin(word.trim(), { toneToNumber: true });
pin = pin.replace(/0/g, "5");
let pizh = await pinzhu.pinyinAndZhuyin(pin, "", "");

Pinyin = [decodeHtmlEntities(pizh[1])];
Zhuyin = [decodeHtmlEntities(pizh[2])];
Syllable = [pin];
Definitions.push(data[0][0][0]);
}

_words.push({
Simplified: result[0].simplified,
Traditional: result[0].traditional,
Expand All @@ -395,7 +453,7 @@ export default function CreateDeck(): JSX.Element {

let ri = 0;
for (let card in tabContent) {
req.push( [ ri, "any", [ ri ] ] );
req.push([ri, "any", [ri]]);
ri++;

let hideSimp = true;
Expand Down Expand Up @@ -519,7 +577,7 @@ for (var _hide of hideList) {
id: modelId.toString(),
flds: flds,
css: CONSTANTS.DECK_CSS,
req: [req],
req: req,
tmpls: tmpls,
});

Expand Down
6 changes: 0 additions & 6 deletions src/pages/index.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,6 @@ section {
padding: 10px;
}

ul,
ol {
margin: unset;
padding: unset;
}

.select_type {
margin: 18px 0px 18px 0px !important;
}
Expand Down
Loading

0 comments on commit b5c35cf

Please sign in to comment.