misskey/src/tools/analysis/extract-user-keywords.ts

155 lines
4.7 KiB
TypeScript
Raw Normal View History

2017-09-07 07:15:40 +00:00
const moji = require('moji');
2017-09-07 05:52:07 +00:00
const MeCab = require('./mecab');
2017-09-07 04:19:14 +00:00
import Post from '../../api/models/post';
import User from '../../api/models/user';
2017-09-07 05:52:07 +00:00
import parse from '../../api/common/text';
2017-09-07 07:15:40 +00:00
process.on('unhandledRejection', console.dir);
2017-09-07 05:52:07 +00:00
const stopwords = [
'ー',
'の', 'に', 'は', 'を', 'た', 'が', 'で', 'て', 'と', 'し', 'れ', 'さ',
'ある', 'いる', 'も', 'する', 'から', 'な', 'こと', 'として', 'い', 'や', 'れる',
'など', 'なっ', 'ない', 'この', 'ため', 'その', 'あっ', 'よう', 'また', 'もの',
'という', 'あり', 'まで', 'られ', 'なる', 'へ', 'か', 'だ', 'これ', 'によって',
'により', 'おり', 'より', 'による', 'ず', 'なり', 'られる', 'において', 'ば', 'なかっ',
'なく', 'しかし', 'について', 'せ', 'だっ', 'その後', 'できる', 'それ', 'う', 'ので',
'なお', 'のみ', 'でき', 'き', 'つ', 'における', 'および', 'いう', 'さらに', 'でも',
'ら', 'たり', 'その他', 'に関する', 'たち', 'ます', 'ん', 'なら', 'に対して', '特に',
'せる', '及び', 'これら', 'とき', 'では', 'にて', 'ほか', 'ながら', 'うち', 'そして',
'とともに', 'ただし', 'かつて', 'それぞれ', 'または', 'お', 'ほど', 'ものの', 'に対する',
2017-09-07 07:15:40 +00:00
'ほとんど', 'と共に', 'といった', 'です', 'とも', 'ところ', 'ここ', '感じ', '気持ち',
2017-09-07 09:15:24 +00:00
'あと', '自分', 'すき', '()',
2017-09-07 05:52:07 +00:00
'about', 'after', 'all', 'also', 'am', 'an', 'and', 'another', 'any', 'are', 'as', 'at', 'be',
'because', 'been', 'before', 'being', 'between', 'both', 'but', 'by', 'came', 'can',
'come', 'could', 'did', 'do', 'each', 'for', 'from', 'get', 'got', 'has', 'had',
'he', 'have', 'her', 'here', 'him', 'himself', 'his', 'how', 'if', 'in', 'into',
'is', 'it', 'like', 'make', 'many', 'me', 'might', 'more', 'most', 'much', 'must',
'my', 'never', 'now', 'of', 'on', 'only', 'or', 'other', 'our', 'out', 'over',
'said', 'same', 'see', 'should', 'since', 'some', 'still', 'such', 'take', 'than',
'that', 'the', 'their', 'them', 'then', 'there', 'these', 'they', 'this', 'those',
'through', 'to', 'too', 'under', 'up', 'very', 'was', 'way', 'we', 'well', 'were',
'what', 'where', 'which', 'while', 'who', 'with', 'would', 'you', 'your', 'a', 'i'
];
2017-09-07 04:19:14 +00:00
const mecab = new MeCab();
function tokenize(text: string) {
2017-09-07 05:52:07 +00:00
if (text == null) return [];
// パース
const ast = parse(text);
const plain = ast
// テキストのみ(URLなどを除外するという意)
.filter(t => t.type == 'text' || t.type == 'bold')
.map(t => t.content)
.join('');
const tokens = mecab.parseSync(plain)
2017-09-07 04:19:14 +00:00
// キーワードのみ
.filter(token => token[1] == '名詞' && (token[2] == '固有名詞' || token[2] == '一般'))
2017-09-07 07:15:40 +00:00
// 取り出し(&整形(全角を半角にしたり大文字を小文字で統一したり))
.map(token => moji(token[0]).convert('ZE', 'HE').convert('HK', 'ZK').toString().toLowerCase())
// ストップワードなど
.filter(word =>
stopwords.indexOf(word) === -1 &&
word.length > 1 &&
word.indexOf('') === -1 &&
word.indexOf('!') === -1 &&
word.indexOf('') === -1 &&
word.indexOf('?') === -1);
2017-09-07 04:19:14 +00:00
return tokens;
}
// Fetch all users
User.find({}, {
fields: {
_id: true
}
}).then(users => {
let i = -1;
const x = cb => {
if (++i == users.length) return cb();
2017-09-07 07:15:40 +00:00
extractKeywordsOne(users[i]._id).then(() => x(cb), err => {
console.error(err);
setTimeout(() => {
i--;
x(cb);
}, 1000);
});
2017-09-07 04:19:14 +00:00
};
x(() => {
console.log('complete');
});
});
2017-09-07 07:15:40 +00:00
function extractKeywordsOne(id) {
return new Promise(async (resolve, reject) => {
process.stdout.write(`extracting keywords of ${id} ...`);
2017-09-07 04:19:14 +00:00
2017-09-07 07:15:40 +00:00
// Fetch recent posts
const recentPosts = await Post.find({
user_id: id,
text: {
$exists: true
}
}, {
sort: {
_id: -1
},
limit: 10000,
fields: {
_id: false,
text: true
}
});
2017-09-07 04:19:14 +00:00
2017-09-07 07:15:40 +00:00
// 投稿が少なかったら中断
if (recentPosts.length < 300) {
process.stdout.write(' >>> -\n');
return resolve();
}
2017-09-07 04:19:14 +00:00
2017-09-07 07:15:40 +00:00
const keywords = {};
2017-09-07 04:19:14 +00:00
2017-09-07 07:15:40 +00:00
// Extract keywords from recent posts
recentPosts.forEach(post => {
const keywordsOfPost = tokenize(post.text);
2017-09-07 04:19:14 +00:00
2017-09-07 07:15:40 +00:00
keywordsOfPost.forEach(keyword => {
if (keywords[keyword]) {
keywords[keyword]++;
} else {
keywords[keyword] = 1;
}
});
2017-09-07 04:19:14 +00:00
});
2017-09-07 07:15:40 +00:00
// Sort keywords by frequency
const keywordsSorted = Object.keys(keywords).sort((a, b) => keywords[b] - keywords[a]);
2017-09-07 04:19:14 +00:00
2017-09-07 07:15:40 +00:00
// Lookup top 10 keywords
const topKeywords = keywordsSorted.slice(0, 10);
2017-09-07 04:19:14 +00:00
2017-09-07 07:15:40 +00:00
process.stdout.write(' >>> ' + topKeywords.join(', ') + '\n');
2017-09-07 04:19:14 +00:00
2017-09-07 07:15:40 +00:00
// Save
User.update({ _id: id }, {
$set: {
keywords: topKeywords
}
}).then(() => {
resolve();
}, err => {
reject(err);
});
2017-09-07 04:19:14 +00:00
});
}