Skip to content

Commit

Permalink
Merge pull request #37 from project-yuki/b0.11
Browse files Browse the repository at this point in the history
B0.11
  • Loading branch information
tinyAdapter committed Nov 19, 2019
2 parents 66ccebe + 61d27f0 commit 6aaae09
Show file tree
Hide file tree
Showing 26 changed files with 771 additions and 187 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ thumbs.db
config/games.json
config/gui.json
config/texts.json
lib/dict/lingoes
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
git clone https://github.com/project-yuki/YUKI.git
cd yuki
yarn
yarn rebuild
yarn dev

## 许可证
Expand Down
8 changes: 7 additions & 1 deletion config/config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
{
"dictionaries": {
"lingoes": {
"enable": true,
"path": "C:\\YUKI\\libraries\\dict\\lingoes\\njcd.db"
}
},
"language": "zh",
"librariesRepoUrl": "https://github.com/project-yuki/libraries/raw/master/_pack/",
"localeChangers": {
Expand All @@ -25,7 +31,7 @@
},
"mecab": {
"enable": true,
"path": "E:\\yuki\\libraries\\pos\\mecab-ipadic"
"path": "C:\\YUKI\\libraries\\pos\\mecab-ipadic"
},
"onlineApis": [
{
Expand Down
1 change: 0 additions & 1 deletion docs/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ After installing Node.js and yarn, run the following commands in any folder:
git clone https://github.com/project-yuki/YUKI.git
cd yuki
yarn
yarn rebuild
yarn dev

## License
Expand Down
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
"test": "npm run unit && npm run e2e",
"unit:main": "cross-env NODE_ENV=none karma start test/unit/karma.main.conf.js",
"unit:renderer": "cross-env NODE_ENV=none karma start test/unit/karma.renderer.conf.js",
"rebuild": "electron-rebuild",
"postinstall": "",
"rebuild": "electron-builder install-app-deps",
"postinstall": "npm run rebuild",
"tsc": "tsc --noEmit"
},
"build": {
Expand Down Expand Up @@ -59,6 +59,7 @@
"request": "^2.88.0",
"request-progress": "^3.0.0",
"request-promise-native": "^1.0.5",
"sqlite3": "^4.1.0",
"tasklist": "^3.1.1",
"textractor-wrapper": "^2.0.1",
"vue": "^2.6.10",
Expand All @@ -70,6 +71,7 @@
"vuetify-dialog": "^1.0.0-alpha.5",
"vuex": "^3.0.1",
"wanakana": "^4.0.2",
"xml2js": "^0.4.22",
"xterm": "^4.2.0-vscode1",
"xterm-addon-fit": "^0.3.0"
},
Expand All @@ -82,7 +84,9 @@
"@types/lodash": "^4.14.121",
"@types/mocha": "^5.2.5",
"@types/request-promise-native": "^1.0.15",
"@types/sqlite3": "^3.1.5",
"@types/vue-color": "^2.4.1",
"@types/xml2js": "^0.4.5",
"ajv": "^6.5.0",
"babel-core": "^6.26.3",
"babel-loader": "^7.1.4",
Expand All @@ -104,7 +108,6 @@
"electron": "^4.0.3",
"electron-builder": "^20.39.0",
"electron-debug": "^1.5.0",
"electron-rebuild": "^1.8.6",
"fibers": "^4.0.1",
"file-loader": "^1.1.11",
"hard-source-webpack-plugin": "^0.13.1",
Expand Down
4 changes: 3 additions & 1 deletion src/common/IpcTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ enum IpcTypes {
REQUEST_ABORT_DOWNLOAD = 'request-abort-download',
REQUEST_DOWNLOAD_LIBRARY = 'request-download-library',
HAS_NEW_DEBUG_MESSAGE = 'has-new-debug-message',
GAME_ABORTED = 'game-aborted'
GAME_ABORTED = 'game-aborted',
REQUEST_DICT = 'request-dict',
HAS_DICT = 'has-dict'
}

export default IpcTypes
2 changes: 1 addition & 1 deletion src/main/Hooker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default class Hooker {

private buildApplication () {
applicationBuilder = new ApplicationBuilder<yuki.TextOutputObject>()
//applicationBuilder.use(new TextMergerMiddleware())
// applicationBuilder.use(new TextMergerMiddleware())
applicationBuilder.use(
new TextMergerMiddleware(
ConfigManager.getInstance().get<yuki.Config.Texts>('texts').merger
Expand Down
4 changes: 4 additions & 0 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import * as path from 'path'
import ConfigManager from './config/ConfigManager'
import DownloaderFactory from './DownloaderFactory'
import setupIpc from './setup/Ipc'
import DictManager from './translate/DictManager'
const debug = require('debug')('yuki:app')

// check & make ./config folder
Expand Down Expand Up @@ -122,6 +123,9 @@ function createWindow () {

app.on('ready', () => {
DownloaderFactory.init()
DictManager.init(
ConfigManager.getInstance().get<yuki.Config.Default>('default').dictionaries
)
createWindow()
})

Expand Down
10 changes: 10 additions & 0 deletions src/main/setup/Ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ConfigManager from '../config/ConfigManager'
import DownloaderFactory from '../DownloaderFactory'
import Game from '../Game'
import Hooker from '../Hooker'
import DictManager from '../translate/DictManager'
import TranslationManager from '../translate/TranslationManager'
import TranslatorWindow from '../TranslatorWindow'

Expand Down Expand Up @@ -186,6 +187,15 @@ export default function (mainWindow: Electron.BrowserWindow) {
}
)

ipcMain.on(
IpcTypes.REQUEST_DICT,
(event: Electron.Event, message: { dict: string, word: string }) => {
DictManager.find(message, (result) => {
event.sender.send(IpcTypes.HAS_DICT, result)
})
}
)

ipcMain.on(
IpcTypes.HAS_DOWNLOAD_PROGRESS,
(packName: string, state: RequestProgress.ProgressState) => {
Expand Down
15 changes: 15 additions & 0 deletions src/main/translate/DictManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import ConfigManager from '../config/ConfigManager'
import LingoesDict from './LingoesDict'

export default class DictManager {
public static init (config: yuki.Config.Dictionaries) {
this.lingoes = new LingoesDict(config.lingoes)
}

public static find (options: yuki.DictOptions, callback: (result: yuki.DictResult) => void) {
if (options.dict !== 'lingoes') callback({ found: false })

this.lingoes.find(options.word, callback)
}
private static lingoes: LingoesDict
}
156 changes: 156 additions & 0 deletions src/main/translate/LingoesDict.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import * as sqlite3 from 'sqlite3'
import * as xml2js from 'xml2js'
const debug = require('debug')('yuki:dict:lingoes')

export default class LingoesDict {

public static contentToObject (
content: string,
callback: (pattern?: yuki.LingoesPattern) => void
) {
try {
xml2js.parseString(content, (err, result: any) => {
if (err) callback()

const refinedResult: yuki.LingoesPattern = {}
let parseResult: any = { ...result }
if (!parseResult.C) {
callback()
return
}
parseResult = parseResult.C
if (!parseResult.F) {
callback()
return
}
parseResult = parseResult.F
if (!parseResult[0]) {
callback()
return
}
parseResult = parseResult[0]
if (parseResult.H) {// parse kana
let kanaResult = parseResult.H
if (kanaResult[0]) {
kanaResult = kanaResult[0]
if (kanaResult.M) {
kanaResult = kanaResult.M
}
refinedResult.kana = kanaResult
}
}

if (!parseResult.I) {
callback(refinedResult)
return
}
parseResult = parseResult.I
if (!parseResult[0]) {
callback(refinedResult)
return
}
parseResult = parseResult[0]
if (!parseResult.N) {
callback(refinedResult)
return
}
parseResult = parseResult.N
if (parseResult[0]) {// parse parts of speech and explanations
refinedResult.definitions = []
for (let definitionResult of parseResult) {
const oneDefinition: any = {}
if (!definitionResult.P) {
oneDefinition.partOfSpeech = undefined
} else {
oneDefinition.partOfSpeech = definitionResult.P[0].U[0]
}
if (!definitionResult.Q) {
oneDefinition.explanations = undefined
} else {
oneDefinition.explanations = []
definitionResult = definitionResult.Q
let oneExplanation = {
content: '',
example: {
sentence: '',
content: ''
}
}
for (const iStr in definitionResult) {
const i = Number.parseInt(iStr, 10)
if (typeof definitionResult[i] === 'string') {
// appendix of the last explanation
oneExplanation.content += definitionResult[i]
} else if (typeof definitionResult[i] === 'object') {
// end of the last explanation, with potential example
oneExplanation.content += definitionResult[i]._
oneExplanation.example.sentence = definitionResult[i].T[0].W[0]
oneExplanation.example.content = definitionResult[i].T[0].X[0]
oneDefinition.explanations.push(oneExplanation)
oneExplanation = {
content: '',
example: {
sentence: '',
content: ''
}
}
} else {
// Error, stop parsing
break
}
}
if (typeof definitionResult[definitionResult.length - 1] === 'string') {
oneDefinition.explanations.push(oneExplanation)
}
}
refinedResult.definitions.push(oneDefinition)
}
}

callback(refinedResult)
})
} catch (e) {
debug('parse XML error !> %s', e)
}
}

private config: yuki.Config.LibraryItem
private db: sqlite3.Database | undefined

constructor (config: yuki.Config.LibraryItem) {
this.config = config
if (this.config.enable) {
this.db = new sqlite3.Database(this.config.path)
debug('loaded db file from path: %s', this.config.path)
}
}

public find (word: string, callback: (result: yuki.DictResult) => void) {
if (!this.db) return

this.db.all('SELECT content from entry where word = ?', word, (err, rows) => {
if (err) {
callback({ found: false })
return
}
if ((!rows) || rows.length === 0) {
callback({ found: false })
return
}

LingoesDict.contentToObject(rows[0].content, (pattern) => {
debug('raw content object from sqlite: %s -> %o', word, pattern)

callback({
found: true,
word,
content: pattern
})
})
})
}

public close () {
if (this.db) this.db.close()
}
}
1 change: 1 addition & 0 deletions src/main/translate/TranslationManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default class TranslationManager {
public initializeApis (
apis: yuki.Config.Default['onlineApis']
): TranslationManager {
this.apis = []
for (const api of apis) {
try {
if (api.external && api.jsFile) {
Expand Down
Loading

0 comments on commit 6aaae09

Please sign in to comment.