1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import _cryptoJs from 'crypto-js';
export default class utils {
// 格式化为json字符串
static formatJson(jsonString: string) {
try {
return JSON.stringify(JSON.parse(jsonString), null, 2); // 对JSON字符串进行格式化处理
} catch (error) {
return 'Invalid JSON'
}
}
// 格式化为json
static formatJsonObj(jsonString: string) {
try {
return JSON.parse(jsonString) // 对JSON字符串进行格式化处理
} catch (error) {
return JSON.parse('{"error": "Invalid JSON"}')
}
}
// 从文本中提取 JSON
static extractJSON(text: string) {
// 正则表达式匹配 JSON 格式的字符串
const jsonRegex = /\[\s*\{\s*"序号"\s*:\s*\d+.*?\}\s*\]/s;
const matches = text.match(jsonRegex);
return matches ? matches[0] : null;
}
// 生成年月日时分秒毫秒字符串
static genDateTimeStr() {
const now = new Date();
const year = now.getFullYear();
const month = (now.getMonth() + 1).toString().padStart(2, '0');
const day = now.getDate().toString().padStart(2, '0');
const hours = now.getHours().toString().padStart(2, '0');
const minutes = now.getMinutes().toString().padStart(2, '0');
const seconds = now.getSeconds().toString().padStart(2, '0');
const milliseconds = now.getMilliseconds().toString().padStart(3, '0');
const formattedDateTime = `${year}${month}${day}${hours}${minutes}${seconds}${milliseconds}`;
// console.log(formattedDateTime); // 输出类似:20221231120530123
return formattedDateTime
}
// 对一句话再次拆分文本
static splitDetailText(sentences: string[]) {
// console.log(sentences)
let result_sentences: string[] = [];
let currentSentence = '';
for (let i = 0; i < sentences.length; i++) {
const str = sentences[i];
currentSentence += str + ',';
if (i < sentences.length - 1 && (currentSentence + sentences[i + 1]).length <= 20) {
continue;
}
if (i === sentences.length - 2 && sentences[i + 1].length <= 5) {
continue;
}
if (currentSentence.length < 10) {
continue;
}
result_sentences.push(currentSentence.endsWith(",") ? currentSentence.slice(0, -1) : currentSentence);
currentSentence = '';
}
// console.log('result_sentences=', result_sentences);
return result_sentences;
}
static splitMoreText (sentences: string[]) {
// console.log(sentences)
let result_sentences: string[] = [];
for (let i = 0; i < sentences.length; i++) {
const str = sentences[i];
let tempSentences = str.split(/[!|?|。|!|?|,|,]/);
let newList = utils.splitDetailText(tempSentences);
result_sentences = result_sentences.concat(newList);
}
// console.log('result_sentences=', result_sentences);
return result_sentences;
};
// 拆分文本
static splitText(str: string, type: string = 'default') {
str = str.replaceAll('“', '').replaceAll('”', '')
// 使用正则表达式拆分文本
let sentences = str.split(/[!|?|。|!|?]/)
// 过滤掉长度为 0 的句子
sentences = sentences.filter((s) => s.length > 0)
// 过滤掉只包含标点符号的句子
sentences = sentences.filter((s) => !utils.containsOnlyPunctuation(s))
if (type == 'default') {
return sentences
} else {
let detailSplit = utils.splitMoreText(sentences)
return detailSplit
}
}
// 拆分英文文本
static splitTextEn(str: string) {
str = str.replaceAll('"','').replaceAll('"','')
// 使用正则表达式拆分文本
let sentences = str.split(/[!|?|.]/);
// 过滤掉长度为 0 的句子
sentences = sentences.filter(s => s.length > 0);
// console.log(sentences)
return sentences
}
// 过滤掉中文字符
static filterChineseAndPunctuation(inputString: string) {
return inputString.replace(/[\u4E00-\u9FA5\u3000-\u303F\uff00-\uffef]/g, '') // 过滤中文字符
.replace(/[^\w\s]|_/g, '') // 过滤标点符号
.replace(/\s+/g, ' '); // 连续多个空格替换为一个空格
}
// 检查该字符串是否只包含中文标点符号和英文标点符号
static containsOnlyPunctuation(str: string) {
// 使用正则表达式匹配是否只包含标点符号(包括中文标点)
return /^[!-\/:-@\[-`{-~\p{P}\p{S}\s]*$/u.test(str);
}
// 加密
static aesEncrypt(word: string) {
var key = _cryptoJs.enc.Utf8.parse('e6ef616dc57343248f6b3e98a07e1dde');
var srcs = _cryptoJs.enc.Utf8.parse(word);
var encrypted = _cryptoJs.AES.encrypt(srcs, key, {
mode: _cryptoJs.mode.ECB,
padding: _cryptoJs.pad.Pkcs7
});
return encrypted.toString();
}
}