wb-table/js/index.js

378 lines
9.0 KiB
JavaScript
Raw Normal View History

2022-03-11 12:13:08 +08:00
/**
*
* @author yutent<yutent.io@gmail.com>
* @date 2022/03/11 12:03:59
*/
import '//unpkg.yutent.top/anot/dist/anot.js'
2022-03-11 15:47:27 +08:00
import '//unpkg.yutent.top/@bytedo/wcui/dist/layer/index.js'
2022-03-11 12:13:08 +08:00
import '//unpkg.yutent.top/@bytedo/wcui/dist/form/input.js'
import '//unpkg.yutent.top/@bytedo/wcui/dist/form/button.js'
2022-03-14 00:16:51 +08:00
import '//unpkg.yutent.top/@bytedo/wcui/dist/form/link.js'
2022-03-11 12:13:08 +08:00
import '//unpkg.yutent.top/@bytedo/wcui/dist/form/radio.js'
2022-03-14 00:16:51 +08:00
import '//unpkg.yutent.top/@bytedo/wcui/dist/form/checkbox.js'
2022-03-11 12:13:08 +08:00
import '//unpkg.yutent.top/@bytedo/wcui/dist/form/switch.js'
import fetch from '//unpkg.yutent.top/@bytedo/fetch/dist/index.js'
2022-03-21 16:46:49 +08:00
import { Enum, saveFile, SString, createCode } from './lib/core.js'
2022-03-16 01:43:17 +08:00
import FIXED_86F from './lib/86_fixed.js'
const VER_86 = '86'
const VER_86F = '86f'
2022-03-11 15:47:27 +08:00
2022-03-11 18:58:43 +08:00
const WB_CODE_NAME = { 1: '一级简码', 2: '二级简码', 3: '三级简码', 4: '四级简码' }
2022-03-16 01:43:17 +08:00
const WB_TABLE_2312 = new Enum()
const WB_TABLE_GBK = new Enum()
const WB_TABLE_86F = new Enum(FIXED_86F)
2022-03-13 01:23:31 +08:00
const WB_WORDS = new Enum()
const WB_DY = new Enum()
2022-03-15 20:00:33 +08:00
const WB_EMOJI = new Enum()
2022-03-21 16:46:49 +08:00
const WB_NET = new Enum()
const WB_CODE = new Enum()
2022-03-11 12:13:08 +08:00
Anot({
$id: 'app',
state: {
2022-03-16 01:43:17 +08:00
gb2312: 0,
gbk: 0,
2022-03-11 15:47:27 +08:00
words: 0,
2022-03-13 01:23:31 +08:00
dy: 0,
2022-03-15 20:00:33 +08:00
emoji: 0,
2022-03-21 16:46:49 +08:00
nethot: 0,
code: 0,
2022-03-11 15:47:27 +08:00
result: '',
filter: {
2022-03-11 18:58:43 +08:00
text: '',
2022-03-16 01:43:17 +08:00
version: VER_86
2022-03-14 00:16:51 +08:00
},
dlOpt: {
2022-03-14 19:03:17 +08:00
pos: 'front',
2022-03-16 01:43:17 +08:00
version: VER_86,
2022-03-14 19:03:17 +08:00
reverse: true,
2022-03-21 16:46:49 +08:00
pinyin: false,
2022-03-16 01:43:17 +08:00
tables: ['2312', 'words', 'dy']
2022-03-14 19:03:17 +08:00
},
2022-03-21 16:46:49 +08:00
total: 0,
2022-03-14 19:03:17 +08:00
preview: ''
2022-03-11 12:13:08 +08:00
},
2022-03-21 16:46:49 +08:00
watch: {
'dlOpt.tables'() {
this.calculate()
}
},
2022-03-11 12:13:08 +08:00
mounted() {
2022-03-14 00:16:51 +08:00
Promise.all([
2022-03-16 01:43:17 +08:00
fetch('./data/gb2312.txt').then(r => r.text()),
fetch('./data/gbk.txt').then(r => r.text()),
2022-03-14 00:16:51 +08:00
fetch('./data/words.txt').then(r => r.text()),
2022-03-15 20:00:33 +08:00
fetch('./data/dy.txt').then(r => r.text()),
2022-03-21 16:46:49 +08:00
fetch('./data/emoji.txt').then(r => r.text()),
fetch('./data/nethot.txt').then(r => r.text()),
fetch('./data/code.txt').then(r => r.text())
]).then(([gb2312, gbk, words, dy, emoji, nethot, code]) => {
2022-03-14 00:16:51 +08:00
//
2022-03-16 01:43:17 +08:00
gb2312.split('\n').forEach(it => {
it = it.split(' ')
let k = it.shift()
if (k) {
WB_TABLE_2312.add(k, it)
}
})
gbk.split('\n').forEach(it => {
2022-03-14 00:16:51 +08:00
it = it.split(' ')
let k = it.shift()
2022-03-13 01:23:31 +08:00
2022-03-14 00:16:51 +08:00
if (k) {
2022-03-16 01:43:17 +08:00
WB_TABLE_GBK.add(k, it)
2022-03-14 00:16:51 +08:00
}
})
2022-03-21 16:46:49 +08:00
WB_TABLE_GBK.concat(WB_TABLE_2312)
2022-03-20 21:30:49 +08:00
//
2022-03-21 16:46:49 +08:00
words.split('\n').forEach(it => {
2022-03-14 00:16:51 +08:00
it = it.split(' ')
2022-03-13 01:23:31 +08:00
2022-03-14 00:16:51 +08:00
let k = it.shift()
2022-03-13 01:23:31 +08:00
2022-03-14 00:16:51 +08:00
if (k) {
2022-03-21 16:46:49 +08:00
WB_WORDS.add(k, createCode(WB_TABLE_GBK, k))
2022-03-14 00:16:51 +08:00
}
2022-03-13 01:23:31 +08:00
})
2022-03-14 00:16:51 +08:00
dy.split('\n').forEach(it => {
it = it.split(' ')
let k = it.shift()
if (k) {
WB_DY.add(k, it)
}
2022-03-13 01:23:31 +08:00
})
2022-03-14 00:16:51 +08:00
2022-03-15 20:00:33 +08:00
emoji.split('\n').forEach(it => {
it = it.split(' ')
let k = it.shift()
if (k) {
WB_EMOJI.add(k, it)
}
})
2022-03-21 16:46:49 +08:00
nethot.split('\n').forEach(it => {
it = it.split(' ')
let k = it.shift()
if (k) {
WB_NET.add(k, createCode(WB_TABLE_GBK, k))
}
})
code.split('\n').forEach(it => {
it = it.split(' ')
let k = it.shift()
if (k) {
WB_CODE.add(k, createCode(WB_TABLE_GBK, k))
}
})
2022-03-20 21:30:49 +08:00
2022-03-16 01:43:17 +08:00
this.gb2312 = WB_TABLE_2312.length
this.gbk = WB_TABLE_GBK.length
2022-03-14 00:16:51 +08:00
this.words = WB_WORDS.length
this.dy = WB_DY.length
2022-03-15 20:00:33 +08:00
this.emoji = WB_EMOJI.length
2022-03-21 16:46:49 +08:00
this.nethot = WB_NET.length
this.code = WB_CODE.length
window.WB_WORDS = WB_WORDS
window.WB_NET = WB_NET
window.WB_CODE = WB_CODE
this.calculate()
2022-03-14 00:16:51 +08:00
})
2022-03-11 15:47:27 +08:00
},
methods: {
search() {
2022-03-15 20:00:33 +08:00
var { text, version } = this.filter
2022-03-11 15:56:24 +08:00
var reverse = false
2022-03-16 01:43:17 +08:00
var res, resf
2022-03-11 15:47:27 +08:00
2022-03-11 18:58:43 +08:00
text = text.trim().toLowerCase()
2022-03-11 15:47:27 +08:00
2022-03-11 18:58:43 +08:00
if (!text) {
2022-03-14 19:03:17 +08:00
this.result = ''
2022-03-11 18:58:43 +08:00
return
}
reverse = /^[a-z]{1,4}$/.test(text)
2022-03-11 15:47:27 +08:00
2022-03-11 16:23:29 +08:00
if (!reverse) {
2022-03-11 18:58:43 +08:00
text = text.replace(/[\sa-z]/g, '')
2022-03-11 16:23:29 +08:00
}
2022-03-21 16:46:49 +08:00
text = new SString(text)
2022-03-11 18:58:43 +08:00
if (reverse || text.length === 1) {
2022-03-21 16:46:49 +08:00
console.log(text, text + '')
2022-03-20 21:30:49 +08:00
res = [WB_TABLE_GBK.get(text)]
2022-03-16 01:43:17 +08:00
if (version === VER_86F) {
resf = [WB_TABLE_86F.get(text)]
2022-03-11 18:58:43 +08:00
}
2022-03-11 16:48:07 +08:00
} else {
2022-03-21 16:46:49 +08:00
res = text.split().map(t => WB_TABLE_GBK.get(t))
2022-03-16 01:43:17 +08:00
if (version === VER_86F) {
2022-03-21 16:46:49 +08:00
resf = text.split().map(t => WB_TABLE_86F.get(t))
2022-03-11 18:58:43 +08:00
}
2022-03-11 16:23:29 +08:00
}
2022-03-11 15:47:27 +08:00
2022-03-11 16:23:29 +08:00
if (reverse) {
2022-03-11 18:58:43 +08:00
text = text.toUpperCase()
// 反查时, 直接替换结果
2022-03-16 01:43:17 +08:00
if (resf && resf[0]) {
res = resf
2022-03-11 18:58:43 +08:00
}
2022-03-11 16:23:29 +08:00
if (res[0]) {
2022-03-11 18:58:43 +08:00
res = `${text} 】👉\t${res[0]
.map(
t =>
2022-03-21 16:46:49 +08:00
`${t}(<b>${(resf && resf[0] ? WB_TABLE_86F.get(t) : WB_TABLE_GBK.get(t))
2022-03-11 18:58:43 +08:00
.join('、')
2022-03-21 16:46:49 +08:00
.toUpperCase()}</b>)`
2022-03-11 18:58:43 +08:00
)
.join('\t\t')}`
2022-03-11 15:56:24 +08:00
} else {
2022-03-11 18:58:43 +08:00
res = `${text} 】👉\t无结果, 请检查你的输入是否正确, 如果确认无误, 可以反馈缺失字库。`
2022-03-11 15:56:24 +08:00
}
2022-03-11 15:47:27 +08:00
} else {
2022-03-16 01:43:17 +08:00
if (resf) {
resf.forEach((it, i) => {
2022-03-11 18:58:43 +08:00
if (it) {
res[i] = it
}
})
}
2022-03-11 16:23:29 +08:00
res = res
.map((it, i) => {
if (it) {
2022-03-21 16:46:49 +08:00
return `${text.at(i)} 】👉\t${it
.map(t => `${WB_CODE_NAME[t.length]}: <b>${t.toUpperCase()}</b>`)
2022-03-11 16:23:29 +08:00
.join('\t\t')}`
} else {
2022-03-21 16:46:49 +08:00
return `${text.at(
i
)} 👉\t无结果, 请检查你的输入是否正确, 如果确认无误, 可以反馈缺失字库`
2022-03-11 16:23:29 +08:00
}
})
.join('\n')
2022-03-21 16:46:49 +08:00
// 词库查询
{
let extra =
WB_WORDS.get(text) ||
WB_DY.get(text) ||
WB_EMOJI.get(text) ||
WB_NET.get(text) ||
WB_CODE.get(text)
if (extra) {
let t = extra.shift()
res += `\n\n${'-'.repeat(6)} 词库查询结果 ${'-'.repeat(32)}\n${text} 】👉\t${
WB_CODE_NAME[t.length]
}: <b>${t.toUpperCase()}</b>`
}
}
2022-03-11 15:47:27 +08:00
}
2022-03-11 16:23:29 +08:00
2022-03-21 16:46:49 +08:00
this.result = `${'-'.repeat(6)} 字库查询结果 ${'-'.repeat(32)}\n${res}`
},
calculate() {
var num = this.gb2312
if (this.dlOpt.tables.includes('gbk')) {
num = this.gbk
}
num += this.words
num += this.dy
if (this.dlOpt.tables.includes('emoji')) {
num += this.emoji
}
if (this.dlOpt.tables.includes('nethot')) {
num += this.nethot
}
if (this.dlOpt.tables.includes('code')) {
num += this.code
}
this.total = num
2022-03-14 19:03:17 +08:00
},
fileChange(ev) {
var reader = new FileReader()
var file = ev.target.files[0]
var all = new Set()
var unknow = new Set()
ev.target.value = ''
reader.onload = () => {
let arr = reader.result
.trim()
.split('\n')
.map(_ => _.trim())
for (let it of arr) {
2022-03-15 20:00:33 +08:00
it = it.replace(/[\w\s\t]+/g, '')
2022-03-14 19:03:17 +08:00
all.add(it)
2022-03-20 21:30:49 +08:00
if (!WB_TABLE_GBK.get(it) && !WB_WORDS.get(it) && !WB_DY.get(it)) {
2022-03-14 19:03:17 +08:00
unknow.add(it)
}
}
all = Array.from(all)
unknow = Array.from(unknow)
this.preview =
2022-03-15 20:00:33 +08:00
`${file.name}\n本次上传, 含有 ${arr.length} 个词条(有效词条 ${all.length} 个)。\n` +
2022-03-14 19:03:17 +08:00
`其中字库中已经存在 ${all.length - unknow.length}个, 未存在词条 ${
unknow.length
} , 如下:\n\n${unknow.join('\t')}`
2022-03-20 21:30:49 +08:00
// window.unknow = unknow
// console.log(unknow)
2022-03-14 19:03:17 +08:00
// navigator.clipboard.writeText(Array.from(all).join('\n'))
}
reader.readAsText(file)
},
openDownloadPanel() {
this.$refs.dl.show()
},
closeDownloadPanel() {
this.$refs.dl.close()
},
download() {
2022-03-21 16:46:49 +08:00
var opt = { ...this.dlOpt }
var temp = new Enum()
// 生成反查字库
if (opt.reverse) {
if (opt.tables.includes('gbk')) {
temp.concat(WB_TABLE_GBK)
let bin = new Blob([WB_TABLE_GBK.toString()], { type: 'text/plain' })
saveFile(bin, 'wb_table_gbk_reverse.txt')
} else {
temp.concat(WB_TABLE_2312)
let bin = new Blob([WB_TABLE_2312.toString()], { type: 'text/plain' })
saveFile(bin, 'wb_table_gb2312_reverse.txt')
}
}
// 默认词库
temp.concat(WB_WORDS)
// 异形字库
temp.concat(WB_DY)
// emoji表情
if (opt.tables.includes('emoji')) {
temp.concat(WB_EMOJI)
}
// 网络热词
if (opt.tables.includes('nethot')) {
temp.concat(WB_NET)
}
// 计算机术语
if (opt.tables.includes('code')) {
temp.concat(WB_CODE)
}
// 暂未支持
// if (opt.tables.includes('personal')) {
// temp.concat(WB_PERSONAL)
// }
let bin = new Blob([temp.toString(opt.pos === 'front')], { type: 'text/plain' })
saveFile(bin, `wb_table_${opt.pos}.txt`)
2022-03-11 15:47:27 +08:00
}
2022-03-11 12:13:08 +08:00
}
})