2018-08-13 08:54:56 +00:00
|
|
|
import 藍 from '../../ai';
|
|
|
|
import IModule from '../../module';
|
|
|
|
import config from '../../config';
|
|
|
|
import MessageLike from '../../message-like';
|
|
|
|
import serifs from '../../serifs';
|
2018-08-13 09:17:38 +00:00
|
|
|
const MeCab = require('mecab-async');
|
2018-08-13 08:54:56 +00:00
|
|
|
|
|
|
|
export default class KeywordModule implements IModule {
|
|
|
|
public name = 'keyword';
|
|
|
|
|
|
|
|
private ai: 藍;
|
2018-08-13 09:17:38 +00:00
|
|
|
private tokenizer: any;
|
2018-08-13 08:54:56 +00:00
|
|
|
|
|
|
|
public install = (ai: 藍) => {
|
|
|
|
this.ai = ai;
|
|
|
|
|
2018-08-13 09:17:38 +00:00
|
|
|
this.tokenizer = new MeCab();
|
|
|
|
this.tokenizer.command = config.mecab;
|
|
|
|
|
|
|
|
setTimeout(this.say, 1000 * 60 * 60);
|
2018-08-13 08:54:56 +00:00
|
|
|
}
|
|
|
|
|
2018-08-13 09:17:38 +00:00
|
|
|
private say = async (msg?: MessageLike) => {
|
2018-08-13 08:54:56 +00:00
|
|
|
const tl = await this.ai.api('notes/local-timeline');
|
|
|
|
|
|
|
|
const interestedNotes = tl.filter(note => note.userId !== this.ai.account.id && note.text != null);
|
|
|
|
|
2018-08-13 09:17:38 +00:00
|
|
|
let keywords: string[][] = [];
|
2018-08-13 08:54:56 +00:00
|
|
|
|
2018-08-13 09:17:38 +00:00
|
|
|
await Promise.all(interestedNotes.map(note => new Promise((res, rej) => {
|
|
|
|
this.tokenizer.parse(note.text, (err, tokens) => {
|
|
|
|
const keywordsInThisNote = tokens.filter(token => token[2] == '固有名詞');
|
|
|
|
keywords = keywords.concat(keywordsInThisNote);
|
|
|
|
res();
|
|
|
|
});
|
|
|
|
})));
|
2018-08-13 08:54:56 +00:00
|
|
|
|
|
|
|
console.log(keywords);
|
|
|
|
|
|
|
|
const keyword = keywords[Math.floor(Math.random() * keywords.length)];
|
|
|
|
|
2018-08-13 09:17:38 +00:00
|
|
|
const text = serifs.KEYWORD
|
|
|
|
.replace('{word}', keyword[0])
|
|
|
|
.replace('{reading}', keyword[8])
|
|
|
|
|
|
|
|
if (msg) {
|
|
|
|
msg.reply(text);
|
|
|
|
} else {
|
|
|
|
this.ai.post(text);
|
|
|
|
}
|
2018-08-13 08:54:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public onMention = (msg: MessageLike) => {
|
|
|
|
if (msg.user.isAdmin && msg.text && msg.text.includes('なんか言って')) {
|
2018-08-13 09:17:38 +00:00
|
|
|
this.say(msg);
|
2018-08-13 08:54:56 +00:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|