-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathword-cloud.js
55 lines (50 loc) · 1.18 KB
/
word-cloud.js
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
const boa = require('@pipcook/boa')
const path = require('path')
const os = require('os')
const Segment = require('segment')
const { WordCloud } = boa.import('wordcloud')
const font_path = path.join(__dirname, 'FZLTHJW.TTF')
const segment = new Segment()
segment.useDefault()
function getWord(text, filterWord) {
try {
text = text.replace(/\[CQ:.*?\]/gim, '')
if (!text) return ''
let wordList = segment.doSegment(text, {
stripPunctuation: true
})
wordList = wordList.map(item => item.w)
if (typeof filterWord === 'function') {
wordList = filterWord(wordList)
}
return wordList.join(',')
} catch (e) {
console.error('[word-cloud]', e)
return ''
}
}
function getImage(wordList) {
const wc = new WordCloud(
boa.kwargs({
font_path,
width: 1000,
height: 1000,
margin: 2,
max_font_size: 300,
background_color: 'black',
collocations: false
})
)
wc.generate(wordList.join(','))
const filename = path.join(
os.tmpdir(),
`go-cqhttp-node-hot-${Date.now()}.png`
)
wc.to_file(filename)
// console.log(filename)
return `file://${filename}`
}
module.exports = {
getWord,
getImage
}