Skip to content

Commit

Permalink
Merge pull request #35 from hockyy/feature/32
Browse files Browse the repository at this point in the history
[#32] Add support for kuromoji and set up without external data
  • Loading branch information
hockyy authored Jul 24, 2023
2 parents 874da16 + e8e8b3f commit 35da603
Show file tree
Hide file tree
Showing 33 changed files with 326 additions and 185 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
renderer/public/** filter=lfs diff=lfs merge=lfs -text
26 changes: 20 additions & 6 deletions main/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
} from 'jmdict-simplified-node';
import fs from "fs";
import path from "path";
import {getTokenizer} from "kuromojin";


const isProd: boolean = process.env.NODE_ENV === 'production';
Expand Down Expand Up @@ -165,6 +166,13 @@ if (isProd) {


})

ipcMain.handle('loadDefaultMode', async (event) => {
mecabCommand = 'kuromoji';
return await checkJMDict({
jmdict: path.join(__dirname, 'dict/jmdict.json')
});
})
ipcMain.handle('removeDictCache', (event) => {
removeJMDictCache()
return true;
Expand Down Expand Up @@ -235,13 +243,10 @@ if (isProd) {
return okSetup;
}

ipcMain.handle('getMecabCommand', async (event, mecab, text) => {
return mecabCommand
ipcMain.handle('getTokenizerMode', async () => {
return mecabCommand;
})
ipcMain.handle('validateConfig', async (event, config) => {

// const dicdirRes = checkDicdir(config);
// if (dicdirRes.ok !== 1) return dicdirRes
let jmdictRes;
if (!config.cached) {
jmdictRes = await checkJMDict(config);
Expand All @@ -259,9 +264,18 @@ if (isProd) {
})
let packageJsonPath = path.join(app.getAppPath(), 'package.json');
let packageJson = JSON.parse(fs.readFileSync(packageJsonPath).toString());
ipcMain.handle('getAppVersion', async (event, ...args) => {
ipcMain.handle('getAppVersion', async () => {
return packageJson.version;
});
let tokenizer = null;
getTokenizer({dicPath: path.join(__dirname, 'dict/')}).then(loadedTokenizer => {
tokenizer = loadedTokenizer;
}).catch(e => {
console.log(e)
})
ipcMain.handle('tokenizeUsingKuromoji', async (event, sentence) => {
return tokenizer.tokenizeForSentence(sentence);
});
protocol.registerFileProtocol(scheme, requestHandler); /* eng-disable PROTOCOL_HANDLER_JS_CHECK */
if (isProd) {
await mainWindow.loadURL('app://./home.html');
Expand Down
75 changes: 67 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"private": true,
"name": "miteiru",
"description": "Miteiru",
"version": "1.2.1",
"version": "1.3.0",
"author": "Hocky Yudhiono <[email protected]>",
"main": "app/background.js",
"build": {
Expand All @@ -22,6 +22,8 @@
}
},
"scripts": {
"copy": "rsync -r renderer/public/ app/",
"predev": "npm run copy",
"dev": "nextron",
"build": "nextron build",
"build:linux": "nextron build --linux",
Expand All @@ -40,12 +42,14 @@
"html-react-parser": "^3.0.16",
"iconv-lite": "^0.6.3",
"jmdict-simplified-node": "^1.1.2",
"kuromojin": "^3.0.0",
"patch-package": "^7.0.0",
"react-awesome-button": "^7.0.5",
"react-colorful": "^5.6.1",
"react-dropzone": "^14.2.3",
"react-smooth-collapse": "^2.1.2",
"react-video-seek-slider": "^6.0.4",
"shunou": "^0.0.36",
"shunou": "^1.0.2",
"styled-components": "^6.0.3",
"video.js": "^8.3.0"
},
Expand Down
8 changes: 6 additions & 2 deletions patches/jmdict-simplified-node+1.1.2.patch
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
diff --git a/node_modules/jmdict-simplified-node/index.js b/node_modules/jmdict-simplified-node/index.js
index b50ce2d..233738c 100644
index b50ce2d..d81da04 100644
--- a/node_modules/jmdict-simplified-node/index.js
+++ b/node_modules/jmdict-simplified-node/index.js
@@ -34,43 +34,48 @@ function setup(DBNAME, filename = '', verbose = false) {
@@ -31,46 +31,52 @@ function setup(DBNAME, filename = '', verbose = false) {
// pass
}
if (!filename) {
+ db.close();
throw new Error('database not found but cannot create it if no `filename` given');
}
const raw = JSON.parse(yield fs_1.promises.readFile(filename, 'utf8'));
Expand Down
2 changes: 1 addition & 1 deletion renderer/components/ContainerHome.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export const ContainerHome = (props) => {
return (
<div className={' border border-black w-fit rounded-lg bg-blue-100 p-3'}>
<div className={props.className + ' border border-black w-fit rounded-lg bg-blue-100 p-3'}>
{props.children}
</div>
)
Expand Down
8 changes: 4 additions & 4 deletions renderer/components/DataStructures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export class Line {
this.content = strContent;
}

fillContentFurigana(mecab) {
this.content = getFurigana(this.content as string, mecab);
async fillContentFurigana(tokenizeMiteiru: (string) => Promise<any[]>) {
this.content = await tokenizeMiteiru(this.content as string);
}

async fillContentWithLearningKotoba() {
Expand Down Expand Up @@ -133,11 +133,11 @@ export class SubtitleContainer {
return subtitleContainer
}

async adjustJapanese(mecab) {
async adjustJapanese(tokenizeMiteiru: (string) => Promise<any[]>) {
for (let i = 0; i < this.lines.length; i++) {
if (globalSubtitleId !== this.id) return;
const line = this.lines[i];
await line.fillContentFurigana(mecab)
await line.fillContentFurigana(tokenizeMiteiru)
await line.fillContentWithLearningKotoba();
this.progress = `${((i + 1) * 100 / this.lines.length).toFixed(2)}%`;
}
Expand Down
2 changes: 1 addition & 1 deletion renderer/components/KeyboardHelp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const Key = ({value, extraClass = ""}) => {

export const KeyboardHelp = () => {
return <div
className={"grid grid-cols-2 gap-5 content-start w-auto px-5 py-3 m-2 bg-blue-50 rounded-lg border-2 border-black"}>
className={"grid grid-cols-2 gap-5 content-start w-full md:w-4/5 px-5 py-3 m-2 bg-blue-50 rounded-lg border-2 border-black"}>
{shortcutInformation.map((val, idx) => {
return <div
className={"font-mono font-bold text-black flex flex-row items-center gap-3 font-bold"}
Expand Down
63 changes: 37 additions & 26 deletions renderer/components/MeaningBox.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import React, {useEffect, useState} from "react";
import {ipcRenderer} from "electron";
import {getFurigana} from "shunou";
import {Sentence} from "./Sentence";
import {CJKStyling, defaultMeaningBoxStyling} from "../utils/CJKStyling";
import {joinString} from "../utils/utils";

const initialContentState = {sense: [], kanji: []};

const MeaningBox = ({
meaning,
setMeaning,
mecab,
tokenizeMiteiru,
subtitleStyling = defaultMeaningBoxStyling
}: { meaning: string, setMeaning: any, mecab: string, subtitleStyling?: CJKStyling }) => {
}: { meaning: string, setMeaning: any, tokenizeMiteiru: (value: string) => Promise<any[]>, subtitleStyling?: CJKStyling }) => {
const [meaningContent, setMeaningContent] = useState(initialContentState)
const [otherMeanings, setOtherMeanings] = useState([]);
const [meaningIndex, setMeaningIndex] = useState(0);
Expand All @@ -34,14 +34,25 @@ const MeaningBox = ({
setTags(val)
})
}, [meaning]);
const joinString = (arr, separator = '; ') => {
let total = "";
arr.forEach(val => {
if (total !== '') total += separator
total += val.toString();
})
return total;
}


const [furiganizedData, setFuriganizedData] = useState([]);

useEffect(() => {
const fetchData = async () => {
const data = await Promise.all(meaningContent.kanji.map(async (val) => {
const furiganized = await tokenizeMiteiru(val.text);
return {
key: val.key,
furiganized
};
}));

setFuriganizedData(data);
};
if (meaningContent.kanji.length) fetchData();
}, [meaningContent.kanji]); // Add your dependencies here


if (meaningContent.kanji.length > 0) {
return (<div onClick={() => {
Expand Down Expand Up @@ -69,23 +80,23 @@ const MeaningBox = ({
<div className={"flex flex-wrap gap-2"} style={{
fontFamily: "Arial",
fontSize: "40px",
}}>{meaningContent.kanji.map((val, meanKey) => {
const furiganized = getFurigana(val.text, mecab);
return (
<div key={meanKey}
}}>
{furiganizedData.map(({key, furiganized}) => (
<div key={key}
className={"bg-white rounded-xl p-2 border-2 border-blue-700 w-fit unselectable"}>
{[...furiganized.map((val, idx) => {
return (<Sentence key={idx}
origin={val.origin}
setMeaning={setMeaning}
separation={val.separation}
extraClass={"meaning-kanji text-md"}
subtitleStyling={subtitleStyling}/>)
})]}
</div>);
})}</div>
{[...furiganized.map((val, idx) => (
<Sentence key={idx}
origin={val.origin}
setMeaning={setMeaning}
separation={val.separation}
extraClass={"unselectable meaning-kanji text-md"}
subtitleStyling={subtitleStyling}/>
))]}
</div>
))}
</div>
{meaningIndex + 1 < otherMeanings.length &&
< button className={"bg-blue-800 p-3 rounded-md m-4"} onClick={(e) => {
<button className={"bg-blue-800 p-3 rounded-md m-4"} onClick={(e) => {
e.stopPropagation()
setMeaningIndex((old) => {
setMeaningContent(otherMeanings[old + 1])
Expand Down
Loading

0 comments on commit 35da603

Please sign in to comment.