Improve AI

This commit is contained in:
syuilo 2018-08-31 17:24:07 +09:00
parent f014e310e6
commit fc6d491bee
8 changed files with 111 additions and 15 deletions

View file

@ -4,6 +4,7 @@ import MessageLike from '../../message-like';
import serifs from '../../serifs'; import serifs from '../../serifs';
import Friend from '../../friend'; import Friend from '../../friend';
import getDate from '../../utils/get-date'; import getDate from '../../utils/get-date';
import includes from '../../utils/includes';
function zeroPadding(num: number, length: number): string { function zeroPadding(num: number, length: number): string {
return ('0000000000' + num).slice(-length); return ('0000000000' + num).slice(-length);
@ -107,8 +108,6 @@ export default class CoreModule implements IModule {
} }
private greet = (msg: MessageLike): boolean => { private greet = (msg: MessageLike): boolean => {
if (!msg.text) return false;
const incLove = () => { const incLove = () => {
//#region 1日に1回だけ親愛度を上げる //#region 1日に1回だけ親愛度を上げる
const today = getDate(); const today = getDate();
@ -124,19 +123,25 @@ export default class CoreModule implements IModule {
//#endregion //#endregion
}; };
if (msg.text.includes('こんにちは')) { if (includes(msg.text, ['こんにちは'])) {
msg.reply(serifs.core.hello(msg.friend.name)); msg.reply(serifs.core.hello(msg.friend.name));
incLove(); incLove();
return true; return true;
} }
if (msg.text.includes('おはよ')) { if (includes(msg.text, ['こんばんは'])) {
msg.reply(serifs.core.helloNight(msg.friend.name));
incLove();
return true;
}
if (includes(msg.text, ['おはよ', 'お早う'])) {
msg.reply(serifs.core.goodMorning(msg.friend.name)); msg.reply(serifs.core.goodMorning(msg.friend.name));
incLove(); incLove();
return true; return true;
} }
if (msg.text.includes('おやすみ')) { if (includes(msg.text, ['おやすみ', 'お休み'])) {
msg.reply(serifs.core.goodNight(msg.friend.name)); msg.reply(serifs.core.goodNight(msg.friend.name));
incLove(); incLove();
return true; return true;
@ -146,8 +151,7 @@ export default class CoreModule implements IModule {
} }
private nadenade = (msg: MessageLike): boolean => { private nadenade = (msg: MessageLike): boolean => {
if (!msg.text) return false; if (!includes(msg.text, ['なでなで'])) return false;
if (!msg.text.includes('なでなで')) return false;
// メッセージのみ // メッセージのみ
if (!msg.isMessage) return true; if (!msg.isMessage) return true;
@ -175,8 +179,7 @@ export default class CoreModule implements IModule {
} }
private kawaii = (msg: MessageLike): boolean => { private kawaii = (msg: MessageLike): boolean => {
if (!msg.text) return false; if (!includes(msg.text, ['かわいい', '可愛い'])) return false;
if (!msg.text.includes('かわいい') && !msg.text.includes('可愛い')) return false;
msg.reply(msg.friend.love >= 5 ? serifs.core.kawaii2 : serifs.core.kawaii1); msg.reply(msg.friend.love >= 5 ? serifs.core.kawaii2 : serifs.core.kawaii1);

View file

@ -2,6 +2,7 @@ import 藍 from '../../ai';
import IModule from '../../module'; import IModule from '../../module';
import MessageLike from '../../message-like'; import MessageLike from '../../message-like';
import serifs from '../../serifs'; import serifs from '../../serifs';
import includes from '../../utils/includes';
const hands = [ const hands = [
'👏', '👏',
@ -124,7 +125,7 @@ export default class EmojiModule implements IModule {
public install = (ai: ) => { } public install = (ai: ) => { }
public onMention = (msg: MessageLike) => { public onMention = (msg: MessageLike) => {
if (msg.text && (msg.text.includes('絵文字') || msg.text.includes('emoji'))) { if (includes(msg.text, ['絵文字', 'emoji'])) {
const hand = hands[Math.floor(Math.random() * hands.length)]; const hand = hands[Math.floor(Math.random() * hands.length)];
const face = faces[Math.floor(Math.random() * faces.length)]; const face = faces[Math.floor(Math.random() * faces.length)];
const emoji = Array.isArray(hand) ? hand[0] + face + hand[1] : hand + face + hand; const emoji = Array.isArray(hand) ? hand[0] + face + hand[1] : hand + face + hand;

View file

@ -3,6 +3,7 @@ import IModule from '../../module';
import MessageLike from '../../message-like'; import MessageLike from '../../message-like';
import serifs from '../../serifs'; import serifs from '../../serifs';
import * as seedrandom from 'seedrandom'; import * as seedrandom from 'seedrandom';
import includes from '../../utils/includes';
const omikujis = [ const omikujis = [
'大大吉', '大大吉',
@ -30,9 +31,7 @@ export default class FortuneModule implements IModule {
public install = (ai: ) => { } public install = (ai: ) => { }
public onMention = (msg: MessageLike) => { public onMention = (msg: MessageLike) => {
if (msg.text == null) return false; if (includes(msg.text, ['占', 'うらな', '運勢', 'おみくじ'])) {
if (msg.text.includes('占') || msg.text.includes('うらな') || msg.text.includes('運勢') || msg.text.includes('おみくじ')) {
const date = new Date(); const date = new Date();
const seed = `${date.getFullYear()}/${date.getMonth()}/${date.getDay()}@${msg.userId}`; const seed = `${date.getFullYear()}/${date.getMonth()}/${date.getDay()}@${msg.userId}`;
const rng = seedrandom(seed); const rng = seedrandom(seed);

View file

@ -4,6 +4,7 @@ import IModule from '../../module';
import MessageLike from '../../message-like'; import MessageLike from '../../message-like';
import serifs from '../../serifs'; import serifs from '../../serifs';
import getCollection from '../../utils/get-collection'; import getCollection from '../../utils/get-collection';
import includes from '../../utils/includes';
export default class GuessingGameModule implements IModule { export default class GuessingGameModule implements IModule {
public readonly name = 'guessingGame'; public readonly name = 'guessingGame';
@ -28,7 +29,7 @@ export default class GuessingGameModule implements IModule {
} }
public onMention = (msg: MessageLike) => { public onMention = (msg: MessageLike) => {
if (msg.text && (msg.text.includes('数当て') || msg.text.includes('数あて'))) { if (includes(msg.text, ['数当て', '数あて'])) {
const exist = this.guesses.findOne({ const exist = this.guesses.findOne({
userId: msg.userId, userId: msg.userId,
isEnded: false isEnded: false

View file

@ -9,6 +9,7 @@ import * as WebSocket from 'ws';
import Friend from '../../friend'; import Friend from '../../friend';
import getDate from '../../utils/get-date'; import getDate from '../../utils/get-date';
import { User } from '../../misskey/user'; import { User } from '../../misskey/user';
import includes from '../../utils/includes';
export default class ReversiModule implements IModule { export default class ReversiModule implements IModule {
public readonly name = 'reversi'; public readonly name = 'reversi';
@ -45,7 +46,7 @@ export default class ReversiModule implements IModule {
} }
public onMention = (msg: MessageLike) => { public onMention = (msg: MessageLike) => {
if (msg.text && (msg.text.includes('リバーシ') || msg.text.includes('りばーし') || msg.text.includes('オセロ') || msg.text.includes('おせろ') || msg.text.toLowerCase().includes('reversi'))) { if (includes(msg.text, ['リバーシ', 'オセロ', 'reversi', 'othello'])) {
if (config.reversiEnabled) { if (config.reversiEnabled) {
msg.reply(serifs.reversi.ok); msg.reply(serifs.reversi.ok);

View file

@ -8,6 +8,8 @@ export default {
hello: name => name ? `こんにちは、${name}` : `こんにちは♪`, hello: name => name ? `こんにちは、${name}` : `こんにちは♪`,
helloNight: name => name ? `こんばんは、${name}` : `こんばんは♪`,
goodMorning: name => name ? `おはようございます、${name}` : 'おはようございます!', goodMorning: name => name ? `おはようございます、${name}` : 'おはようございます!',
goodNight: name => name ? `おやすみなさい、${name}` : 'おやすみなさい!', goodNight: name => name ? `おやすみなさい、${name}` : 'おやすみなさい!',

10
src/utils/includes.ts Normal file
View file

@ -0,0 +1,10 @@
import { hiraganaToKatagana, zenkakuToHankaku, hankakuToZenkaku } from './japanese';
export default function(text: string, words: string[]): boolean {
if (text == null) return false;
text = hankakuToZenkaku(hiraganaToKatagana(text));
words = words.map(word => hiraganaToKatagana(word));
return words.some(word => text.includes(word));
}

79
src/utils/japanese.ts Normal file
View file

@ -0,0 +1,79 @@
// Utilities for Japanese
const kanaMap: string[][] = [
['ア', 'ア'], ['イ', 'イ'], ['ウ', 'ウ'], ['エ', 'エ'], ['オ', 'オ'],
['カ', 'カ'], ['キ', 'キ'], ['ク', 'ク'], ['ケ', 'ケ'], ['コ', 'コ'],
['サ', 'サ'], ['シ', 'シ'], ['ス', 'ス'], ['セ', 'セ'], ['ソ', 'ソ'],
['タ', 'タ'], ['チ', 'チ'], ['ツ', 'ツ'], ['テ', 'テ'], ['ト', 'ト'],
['ナ', 'ナ'], ['ニ', 'ニ'], ['ヌ', 'ヌ'], ['ネ', 'ネ'], ['', 'ノ'],
['ハ', 'ハ'], ['ヒ', 'ヒ'], ['フ', 'フ'], ['ヘ', 'ヘ'], ['ホ', 'ホ'],
['マ', 'マ'], ['ミ', 'ミ'], ['ム', 'ム'], ['メ', 'メ'], ['モ', 'モ'],
['ヤ', 'ヤ'], ['ユ', 'ユ'], ['ヨ', 'ヨ'],
['ラ', 'ラ'], ['リ', 'リ'], ['ル', 'ル'], ['レ', 'レ'], ['ロ', 'ロ'],
['ワ', 'ワ'], ['ヲ', 'ヲ'], ['ン', 'ン'],
['ガ', 'ガ'], ['ギ', 'ギ'], ['グ', 'グ'], ['ゲ', 'ゲ'], ['ゴ', 'ゴ'],
['ザ', 'ザ'], ['ジ', 'ジ'], ['ズ', 'ズ'], ['ゼ', 'ゼ'], ['ゾ', 'ゾ'],
['ダ', 'ダ'], ['ヂ', 'ヂ'], ['ヅ', 'ヅ'], ['デ', 'デ'], ['ド', 'ド'],
['バ', 'バ'], ['ビ', 'ビ'], ['ブ', 'ブ'], ['ベ', 'ベ'], ['ボ', 'ボ'],
['パ', 'パ'], ['ピ', 'ピ'], ['プ', 'プ'], ['ペ', 'ペ'], ['ポ', 'ポ'],
['ヴ', 'ヴ'], ['ヷ', 'ヷ'], ['ヺ', 'ヺ'],
['ァ', 'ァ'], ['ィ', 'ィ'], ['ゥ', 'ゥ'], ['ェ', 'ェ'], ['ォ', 'ォ'],
['ッ', 'ッ'], ['ャ', 'ャ'], ['ュ', 'ュ'], ['ョ', 'ョ'],
['ー', 'ー']
];
/**
*
* @param str
* @returns
*/
export function katakanaToHiragana(str: string): string {
return str.replace(/[\u30a1-\u30f6]/g, match => {
const char = match.charCodeAt(0) - 0x60;
return String.fromCharCode(char);
});
}
/**
*
* @param str
* @returns
*/
export function hiraganaToKatagana(str: string): string {
return str.replace(/[\u3041-\u3096]/g, match => {
const char = match.charCodeAt(0) + 0x60;
return String.fromCharCode(char);
});
}
/**
*
* @param str
* @returns
*/
export function zenkakuToHankaku(str: string): string {
const reg = new RegExp('(' + kanaMap.map(x => x[0]).join('|') + ')', 'g');
return str
.replace(reg, match =>
kanaMap.find(x => x[0] == match)[1]
)
.replace(/゛/g, '゙')
.replace(/゜/g, '゚');
};
/**
*
* @param str
* @returns
*/
export function hankakuToZenkaku(str: string): string {
const reg = new RegExp('(' + kanaMap.map(x => x[1]).join('|') + ')', 'g');
return str
.replace(reg, match =>
kanaMap.find(x => x[1] == match)[0]
)
.replace(/゙/g, '゛')
.replace(/゚/g, '゜');
};