-
Notifications
You must be signed in to change notification settings - Fork 2
/
initData.cjs
378 lines (345 loc) · 10.5 KB
/
initData.cjs
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/* eslint-disable no-undef */
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require('fs')
const path = require('path')
const os = require('os')
const dayjs = require('dayjs')
const weekOfYear = require('dayjs/plugin/weekOfYear')
const {prompts, generateFile, chalk} = require('@umijs/utils')
const Segment = require('novel-segment')
require("dayjs/locale/zh-cn");
// config dayjs
dayjs.locale('zh-cn');
dayjs.extend(weekOfYear)
const nodejieba = new Segment()
nodejieba.useDefault();
const emojiText = [
'[',
']',
'微笑',
'发呆',
'撇嘴',
'色',
'得意',
'流泪',
'害羞',
'闭嘴',
'睡',
'大哭',
'尴尬',
'发怒',
'调皮',
'呲牙',
'惊讶',
'难过',
'抓狂',
'吐',
'偷笑',
'愉快',
'白',
'傲慢',
'困',
'惊恐',
'憨笑',
'悠闲',
'咒骂',
'疑问',
'嘘',
'晕',
'衰',
'骷髅',
'敲打',
'再见',
'擦汗',
'抠鼻',
'鼓掌',
'坏笑',
'右哼哼',
'鄙视',
'委屈',
'快哭了',
'阴险',
'亲亲',
'可怜',
'可怜',
'笑脸',
'生病',
'脸红',
'破涕为笑',
'恐惧',
'失望',
'无语',
'嘿哈',
'捂脸',
'奸笑',
'机智',
'皱眉',
'耶',
'吃瓜',
'加油',
'汗',
'天啊',
'Emm',
'社会社会',
'旺柴',
'好的',
'打脸',
'哇',
'翻白眼',
'666',
'让我看看',
'叹气',
'苦涩',
'裂开',
'嘴唇',
'爱心',
'心碎',
'拥抱',
'强',
'弱',
'握手',
'胜利',
'抱拳',
'勾引',
'拳头',
'OK',
'合十',
'啤酒',
'咖啡',
'蛋糕',
'玫',
'凋谢',
'菜刀',
'炸弹',
'便便',
'月亮',
'太阳',
'庆祝',
'礼物',
'红包',
'發',
'福',
'烟花',
'爆竹',
'猪头',
'跳跳',
'发抖',
'转圈',
]
// 生成词汇出现量的排行
function getWordRank(messages) {
// const homedir = os.userInfo().homedir
// const targetDictPath = path.join(homedir, 'weixin-group-annual-report')
// if (!fs.existsSync(targetDictPath)) {
// fs.mkdirSync(targetDictPath)
// }
// const defaultDicts = [
// 'DEFAULT_USER_DICT',
// 'DEFAULT_DICT',
// 'DEFAULT_HMM_DICT',
// 'DEFAULT_IDF_DICT',
// 'DEFAULT_STOP_WORD_DICT',
// ]
// defaultDicts.forEach(dictPath => {
// const from = nodejieba[dictPath].split('/')
// console.log(nodejieba[dictPath])
// const fileName = from[from.length - 1]
// const to = path.join(targetDictPath, fileName)
// // nodejieba不识别中文路径,需要复制到临时路径
// fs.copyFileSync(nodejieba[dictPath], to)
// })
// const defaultDict = path.resolve(targetDictPath, 'jieba.dict.utf8')
// const hmmDict = path.resolve(targetDictPath, 'hmm_model.utf8')
// const userDict = path.resolve(targetDictPath, 'user.dict.utf8')
// const idfDict = path.resolve(targetDictPath, 'idf.utf8')
const stopWordDict = path.join(__dirname, 'stopwordstemp')
if (!fs.existsSync(stopWordDict)) {
fs.mkdirSync(stopWordDict)
}
const stopWordDictPath = path.resolve(stopWordDict, 'stopwords.utf8')
const customStopWordsDir = path.join(__dirname, 'stopwords')
fs.writeFileSync(stopWordDictPath, '')
fs.readdirSync(customStopWordsDir).forEach(fileName => {
if(fileName.endsWith('.txt')) {
let fileContent = fs.readFileSync(path.join(customStopWordsDir, fileName), 'utf-8')
fs.appendFileSync(stopWordDictPath, fileContent)
}
})
// const displaynames = [...new Set(messages.map(item => item.displayname))]; //昵称自动加入用户自定义词典
// const customWords = ['外包', '张三', '李四']; //自定义分词词典
// let userDictStr = '';
// [...displaynames, ...customWords].forEach(name => {
// if (name) {
// userDictStr += `\n${name}`
// }
// })
// fs.writeFileSync(userDict, userDictStr.slice(1))
try {
nodejieba.loadStopwordDict(stopWordDictPath);
} catch (e) {
console.log("加载StopWord出错")
}
function isChina(s) {
const reg = new RegExp("[\\u4E00-\\u9FFF]+", "g");
return reg.test(s);
}
// const stopWords = fs.readFileSync(stopWordDictPath, 'utf-8').split('\n')
const wordMap = {};
for (const message of messages) {
if (message.type === 1 || message.type === 49 && message.sub_type === 57) {
let {text = ''} = message
// 删除表情包
for (const emoji of emojiText) {
text = text.replace(new RegExp(`[${emoji}]`, 'gm'), '')
}
const txtArr = nodejieba.doSegment(text, {
stripStopword: true,
simple: true
});
for (const wordWeight of txtArr) {
const {w} = wordWeight
if(!wordWeight||wordWeight.length === 1 || !isChina(wordWeight)) { // 排除不包含汉字的词汇,排除一个字,排除停用词
continue;
}
if (!wordMap[wordWeight]) {
wordMap[wordWeight] = 1
} else {
wordMap[wordWeight] = wordMap[wordWeight] + 1
}
}
}
}
const wordCountList = Object.keys(wordMap).map(word => ({
word,
count:wordMap[word]
})).sort((a, b) => b.count - a.count)
return wordCountList
}
async function main() {
let parentDir = path.resolve('../')
let dirs = parentDir.split(path.sep)
let groupName = dirs[dirs.length - 1]
let htmlFilePath = path.join(parentDir, groupName + '.html')
const fileExits = fs.existsSync(htmlFilePath)
if (!fileExits) {
let customDir = await prompts([
{
type: 'text',
name: 'dir',
message: '输入导出的群聊html聊天记录目录',
validate(v){
parentDir = path.resolve(v)
dirs = parentDir.split(path.sep)
groupName = dirs[dirs.length - 1]
htmlFilePath = path.join(parentDir, groupName + '.html')
if(!fs.existsSync(htmlFilePath)) {
return '请输入正确的聊天记录导出目录,或者将本项目文件夹放在导出的聊天记录{群聊名称}.html同级目录下'
} else {
return true
}
}
}
])
// parentDir = path.resolve(customDir.dir)
// dirs = parentDir.split(path.sep)
// groupName = dirs[dirs.length - 1]
// htmlFilePath = path.join(parentDir, groupName + '.html')
}
const htmlFile = fs.readFileSync(htmlFilePath, 'utf-8')
const startStr = 'const chatMessages ='
const startIndex = htmlFile.indexOf(startStr)
const endIndex = htmlFile.indexOf(`</script>`)
let messagesStr = htmlFile.slice(startIndex + startStr.length, endIndex)
const allMessages = eval(messagesStr);
const currentYear = dayjs().format('YYYY')
const yearValue = Number(currentYear)
const prevYears = [yearValue]
for (let i = 1; i < 5; i++) {
prevYears.push(yearValue - i)
}
let value = await prompts([
{
type: 'select',
name: 'range',
message: '选择要生成报告的时间段',
choices: [
{title: '上周', value: 'lastweek'},
{title: '本周', value: 'week'},
{title: '上个月', value: 'lastmonth'},
{title: '本月', value: 'month'},
{title: '全部', value: 'all'},
...prevYears.map(title => ({title:`${title}年`, value: `${title}`}))
],
}
])
console.log(chalk.green('正在过滤聊天记录...'))
let messages;
let startTime
let endTime
let rangeStr = ''
if(value.range === 'week') {
startTime = dayjs().startOf('week')
endTime = dayjs()
rangeStr = `${startTime.format('YYYY')}年第${startTime.week()}周`
} else if(value.range === 'lastweek') {
startTime = dayjs().subtract(1,'week').startOf('week')
endTime = dayjs().subtract(1,'week').endOf('week')
rangeStr = `${startTime.format('YYYY')}年第${startTime.week()}周`
}else if(value.range === 'lastmonth') {
startTime = dayjs().subtract(1,'month').startOf('month')
endTime = dayjs().subtract(1,'month').endOf('month')
rangeStr = `${startTime.format('YYYY')}年${startTime.format('M')}月`
}else if(value.range === 'month') {
startTime = dayjs().startOf('month')
endTime = dayjs()
rangeStr = `${startTime.format('YYYY')}年${startTime.format('M')}月`
}
if(startTime && endTime) {
messages = allMessages.filter(item => {
if (item.timestamp) {
let messageTime = dayjs(item.timestamp * 1000)
if (messageTime.isAfter(startTime) && messageTime.isBefore(endTime)) {
return true
}
}
return false
})
} else if(value.range === 'all') {
messages = allMessages
rangeStr = '全部'
} else {
rangeStr = `${value.range}年`
messages = allMessages.filter(item => {
if (item.timestamp) {
if (dayjs(item.timestamp * 1000).format('YYYY') === value.range) {
return true
}
}
return false
})
}
console.log(chalk.green('正在生成词云...'))
const wordCountList = getWordRank(messages).slice(0, 100)
const templates = [
'./index.html',
'./src/App.tsx',
'./src/messages.ts',
'./vite.config.ts',
]
await Promise.all(templates.map(tpl => {
return generateFile({
path: tpl + '.tpl',
target: tpl,
baseDir: __dirname,
data: {
groupName,
rangeStr,
publicPath: JSON.stringify(parentDir),
wordCountList: JSON.stringify(wordCountList),
messages: JSON.stringify(messages)
}
})
}))
console.log(chalk.green('生成模板完成,执行 npm run dev 运行项目'))
}
main()