-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from project-yuki/b0.11
B0.11
- Loading branch information
Showing
26 changed files
with
771 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ thumbs.db | |
config/games.json | ||
config/gui.json | ||
config/texts.json | ||
lib/dict/lingoes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,7 +70,6 @@ | |
git clone https://github.com/project-yuki/YUKI.git | ||
cd yuki | ||
yarn | ||
yarn rebuild | ||
yarn dev | ||
|
||
## 许可证 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.