Merge branch 'dev'
Before Width: | Height: | Size: 247 KiB After Width: | Height: | Size: 155 KiB |
Before Width: | Height: | Size: 76 KiB After Width: | Height: | Size: 43 KiB |
Before Width: | Height: | Size: 170 KiB After Width: | Height: | Size: 184 KiB |
Before Width: | Height: | Size: 90 KiB After Width: | Height: | Size: 110 KiB |
@ -0,0 +1,54 @@
|
||||
export const DICT_PINYIN = 'pinyin'
|
||||
export const DICT_KANA = 'kana'
|
||||
|
||||
export class PronunciationConverter {
|
||||
constructor () {
|
||||
this.pronunciationMap = new Map()
|
||||
}
|
||||
|
||||
async loadDict (dictName) {
|
||||
let promise
|
||||
switch (dictName) {
|
||||
case DICT_PINYIN:
|
||||
promise = import('./dictPinyin')
|
||||
break
|
||||
case DICT_KANA:
|
||||
promise = import('./dictKana')
|
||||
break
|
||||
default:
|
||||
return
|
||||
}
|
||||
|
||||
let dictTxt = (await promise).default
|
||||
let pronunciationMap = new Map()
|
||||
for (let item of dictTxt.split('\n')) {
|
||||
if (item.length === 0) {
|
||||
continue
|
||||
}
|
||||
pronunciationMap.set(item.substring(0, 1), item.substring(1))
|
||||
}
|
||||
this.pronunciationMap = pronunciationMap
|
||||
}
|
||||
|
||||
getPronunciation (text) {
|
||||
let res = []
|
||||
let lastHasPronunciation = null
|
||||
for (let char of text) {
|
||||
let pronunciation = this.pronunciationMap.get(char)
|
||||
if (pronunciation === undefined) {
|
||||
if (lastHasPronunciation !== null && lastHasPronunciation) {
|
||||
res.push(' ')
|
||||
}
|
||||
lastHasPronunciation = false
|
||||
res.push(char)
|
||||
} else {
|
||||
if (lastHasPronunciation !== null) {
|
||||
res.push(' ')
|
||||
}
|
||||
lastHasPronunciation = true
|
||||
res.push(pronunciation)
|
||||
}
|
||||
}
|
||||
return res.join('')
|
||||
}
|
||||
}
|
Before Width: | Height: | Size: 113 KiB After Width: | Height: | Size: 117 KiB |
Before Width: | Height: | Size: 309 KiB After Width: | Height: | Size: 224 KiB |
Before Width: | Height: | Size: 159 KiB After Width: | Height: | Size: 173 KiB |