// /scripts/utils
function mineJieba(str) {
class WordTree {
root = null;
stop = {
你: 1,
我: 1,
它: 1,
};
constructor() {
this.root = new NodeAs(null);
}
// 将unicode转成utf-8的三字节
toBytes(word) {
let result = [];
for (let i = 0; i < word.length; i++) {
let code = word.charCodeAt(i);
if (code < 0x80) {
result.push(code);
} else {
result = result.concat(this.toUTF8(code));
}
}
return result;
}
toUTF8(code) {
let byte1 = 0xe0 | ((code >> 12) & 0x0f),
byte2 = 0x80 | ((code >> 6) & 0x3f),
byte3 = 0x80 | (code & 0x3f);
return [byte1, byte2, byte3];
}
toUTF16(b1, b2, b3) {
let byte1 = (b1 << 4) | ((b2 >> 2) & 0x0f),
byte2 = ((b2 & 0x03) << 6) | (b3 & 0x3f),
utf16 = ((byte1 & 0x00ff) << 8) | byte2;
return utf16;
}
// 添加每个词到trie
add(word) {
let node = this.root,
bytes = this.toBytes(word),
len = bytes.length;
for (let i = 0; i < len; i++) {
let c = bytes[i];
if (!(c in node.childs)) {
node.childs[c] = new NodeAs(c);
}
node = node.childs[c];
}
node.asWord();
}
// 按字节在trie树中搜索
search(bytes) {
let node = this.root,
len = bytes.length,
result = [],
word = [],
j = 0;
for (let i = 0; i < len; i++) {
let c = bytes[i],
childs = node.childs;
if (!(c in childs)) {
return result;
}
if (c < 0x80) {
word.push(String.fromCharCode(c));
} else {
j++;
if (j % 3 == 0) {
let b1 = bytes[i - 2],
b2 = bytes[i - 1],
b3 = c;
word.push(String.fromCharCode(this.toUTF16(b1, b2, b3)));
}
}
if (word.join("") in this.stop) {
return result;
}
let cnode = childs[c];
if (cnode.isWord()) {
cnode.addCount();
result.push(word.join(""));
}
node = cnode;
}
return result;
}
// 分词
splitWords(words) {
let bytes = this.toBytes(words),
start = 0,
end = bytes.length,
result = [];
while (start != end) {
let word = [];
for (let i = start; i <= end; i++) {
let b = bytes[i];
word.push(b);
let finds = this.search(word);
if (finds != false && finds.length > 0) {
result = result.concat(finds);
}
}
start++;
}
return result;
}
// 初始化
init(dict) {
for (let i = 0; i < dict.length; i++) {
this.add(dict[i]);
}
}
}
class NodeAs {
childs = null;
_byte = null;
_isWord = null;
_count = null;
constructor() {
this.childs = {};
this._byte = this._byte || null;
this._isWord = false;
this._count = 0;
}
isWord() {
return this._isWord && this._count == 0;
}
asWord() {
this._isWord = true;
}
addCount() {
this._count++;
}
getCount() {
return this._count;
}
}
let mjb = new WordTree();
return mjb;
}
// 定义数据
const typeInfo = reactive({
text: "物理层负责最后将信息编码成电流脉冲或其它信号用于网上传输。它由计算机和网络介质之间的实际界面组成|可定义电气信号、符号、线的状态和时钟要求、数据编码和数据传输用的连接器。",
index: 0,
result: "",
});
// 开始打字
function startTyping() {
if (typeInfo.index < typeInfo.text.length) {
typeInfo.result = typeInfo.text.substring(0, typeInfo.index++) + "_";
setTimeout(startTyping, 100);
} else {
typeInfo.result = typeInfo.text;
}
}
// #ifdef APP-PLUS
// 获取缓存
async function getCache($event, from = "app") {
let cacheSize = await proxy.$apis.utils.cache.get();
console.log(`应用缓存大小(${from}):, ${cacheSize.data}`); // 应用缓存大小(app):, 1.15KB 应用缓存大小(app):, 0B
}
// 清除缓存
function clearCache(type = "app") {
if (type == "app") {
proxy.$apis.utils.cache.del();
} else {
proxy.$apis.utils.cache.clear();
}
getCache(type);
}
// #endif